You are here

function google_analytics_reports_api_gafeed in Google Analytics Reports 7.3

Same name and namespace in other branches
  1. 8.3 google_analytics_reports_api/google_analytics_reports_api.module \google_analytics_reports_api_gafeed()

Instantiate a new GoogleAnalyticsReportsApiFeed object.

Return value

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

5 calls to google_analytics_reports_api_gafeed()
google_analytics_reports_api_admin in google_analytics_reports_api/google_analytics_reports_api.admin.inc
Menu callback - admin form for OAuth and other settings.
google_analytics_reports_api_profiles_list in google_analytics_reports_api/google_analytics_reports_api.admin.inc
Google Analytics reports profiles for current authorized user.
google_analytics_reports_api_report_data in google_analytics_reports_api/google_analytics_reports_api.module
Request report data.
google_analytics_reports_api_revoke in google_analytics_reports_api/google_analytics_reports_api.module
Programmatically revoke token.
google_analytics_reports_form_google_analytics_reports_api_admin_alter in ./google_analytics_reports.module
Implements hook_form_BASE_FORM_ID_alter().

File

google_analytics_reports_api/google_analytics_reports_api.module, line 71
Implements the API through which Google Analytics data can be accessed.

Code

function google_analytics_reports_api_gafeed() {

  // If the access token is still valid, return an authenticated
  // GoogleAnalyticsReportsApiFeed.
  if (variable_get('google_analytics_reports_api_access_token', NULL) && time() < variable_get('google_analytics_reports_api_expires_at', NULL)) {
    return new GoogleAnalyticsReportsApiFeed(variable_get('google_analytics_reports_api_access_token', NULL));
  }
  else {

    // If the site has an access token and refresh token, but the access
    // token has expired, authenticate the user with the refresh token.
    if (variable_get('google_analytics_reports_api_refresh_token', NULL)) {
      $client_id = variable_get('google_analytics_reports_api_client_id', NULL);
      $client_secret = variable_get('google_analytics_reports_api_client_secret', NULL);
      $refresh_token = variable_get('google_analytics_reports_api_refresh_token', NULL);
      try {
        $gafeed = new GoogleAnalyticsReportsApiFeed();
        $gafeed
          ->refreshToken($client_id, $client_secret, $refresh_token);
        variable_set('google_analytics_reports_api_access_token', $gafeed->accessToken);
        variable_set('google_analytics_reports_api_expires_at', $gafeed->expiresAt);
        return $gafeed;
      } catch (Exception $e) {
        drupal_set_message(t('There was an authentication error. Message: @message.', array(
          '@message' => $e
            ->getMessage(),
        )), 'error', FALSE);
        watchdog('google analytics reports api', 'There was an authentication error. Message: @message.', array(
          '@message' => $e
            ->getMessage(),
        ), WATCHDOG_ERROR);
        return NULL;
      }
    }
    else {

      // If there is no access token or refresh token and client is returned
      // to the config page with an access code, complete the authentication.
      if (isset($_GET['code'])) {
        $client_id = variable_get('google_analytics_reports_api_client_id', NULL);
        $client_secret = variable_get('google_analytics_reports_api_client_secret', NULL);
        $redirect_uri = variable_get('google_analytics_reports_api_redirect_uri', NULL);
        try {
          $gafeed = new GoogleAnalyticsReportsApiFeed();
          $gafeed
            ->finishAuthentication($client_id, $client_secret, $redirect_uri);
          variable_set('google_analytics_reports_api_access_token', $gafeed->accessToken);
          variable_set('google_analytics_reports_api_expires_at', $gafeed->expiresAt);
          variable_set('google_analytics_reports_api_refresh_token', $gafeed->refreshToken);
          variable_del('google_analytics_reports_api_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: @message.', array(
            '@message' => $e
              ->getMessage(),
          )), 'error', FALSE);
          watchdog('google analytics reports api', 'There was an authentication error. Message: @message.', array(
            '@message' => $e
              ->getMessage(),
          ), WATCHDOG_ERROR);
          return NULL;
        }
      }
    }
  }
}