You are here

function google_analytics_counter_new_gafeed in Google Analytics Counter 7.3

Same name and namespace in other branches
  1. 7.2 google_analytics_counter_auth.inc \google_analytics_counter_new_gafeed()

Instantiate a new GoogleAnalyticsCounterFeed object.

Return value

object GoogleAnalyticsCounterFeed object to authorize access and request data from the Google Analytics Core Reporting API.

3 calls to google_analytics_counter_new_gafeed()
google_analytics_counter_auth_admin in ./google_analytics_counter_auth.inc
Menu callback - admin form for OAuth and other settings.
google_analytics_counter_report_data in ./google_analytics_counter_data.inc
Request report data.
google_analytics_counter_revoke in ./google_analytics_counter_auth.inc
Programatically revoke token.

File

./google_analytics_counter_auth.inc, line 32
Provides the GAFeed object type and associated methods.

Code

function google_analytics_counter_new_gafeed() {
  module_load_include('inc', 'google_analytics_counter', 'google_analytics_counter_oauth2.lib');
  if (variable_get('google_analytics_counter_access_token', NULL) && time() < variable_get('google_analytics_counter_expires_at', NULL)) {

    // If the access token is still valid, return an authenticated GAFeed.
    return new GoogleAnalyticsCounterFeed(variable_get('google_analytics_counter_access_token', NULL));
  }
  elseif (variable_get('google_analytics_counter_refresh_token', NULL)) {

    // If the site has an access token and refresh token, but the access
    // token has expired, authenticate the user with the refresh token.
    $client_id = variable_get('google_analytics_counter_client_id', NULL);
    $client_secret = variable_get('google_analytics_counter_client_secret', NULL);
    $refresh_token = variable_get('google_analytics_counter_refresh_token', NULL);
    try {
      $gafeed = new GoogleAnalyticsCounterFeed();
      $gafeed
        ->refreshToken($client_id, $client_secret, $refresh_token);
      variable_set("google_analytics_counter_access_token", $gafeed->access_token);
      variable_set("google_analytics_counter_expires_at", $gafeed->expires_at);
      return $gafeed;
    } catch (Exception $e) {
      drupal_set_message(t("There was an authentication error. Message: " . $e
        ->getMessage()), 'error', FALSE);
      return NULL;
    }
  }
  elseif (isset($_GET['code'])) {

    // If there is no access token or refresh token and client is returned
    // to the config page with an access code, complete the authentication.
    $client_id = variable_get('google_analytics_counter_client_id', NULL);
    $client_secret = variable_get('google_analytics_counter_client_secret', NULL);
    $redirect_uri = variable_get('google_analytics_counter_redirect_uri', NULL);
    try {
      $gafeed = new GoogleAnalyticsCounterFeed();
      $gafeed
        ->finishAuthentication($client_id, $client_secret, $redirect_uri);
      variable_set('google_analytics_counter_access_token', $gafeed->access_token);
      variable_set('google_analytics_counter_expires_at', $gafeed->expires_at);
      variable_set('google_analytics_counter_refresh_token', $gafeed->refresh_token);
      variable_del('google_analytics_counter_redirect_uri');
      drupal_set_message(t("You have been successfully authenticated."), 'status', FALSE);
      drupal_goto($redirect_uri);
    } catch (Exception $e) {
      drupal_set_message(t("There was an authentication error. Message: " . $e
        ->getMessage()), 'error', FALSE);
      return NULL;
    }
  }
  else {
    return NULL;
  }
}