You are here

google_analytics_api.module in Google Analytics Reports 6

Same filename and directory in other branches
  1. 7 google_analytics_api.module

Implements the API through which Google Analytics data can be accessed.

File

google_analytics_api.module
View source
<?php

/**
 * @file
 * Implements the API through which Google Analytics data can be accessed.
 */

/* Number of seconds in a day. */
define('GOOGLE_ANALYTICS_REPORTS_DAY', 60 * 60 * 24);

/* Number of seconds in a week. */
define('GOOGLE_ANALYTICS_REPORTS_WEEK', GOOGLE_ANALYTICS_REPORTS_DAY * 7);

/**
 * Implementation of hook_help().
 */
function google_analytics_api_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/google-analytics-api':
    case 'admin/help#google_analytics_api':
      $output = '<p>' . t('Google Analytics Reports and it\'s API module provides site administrators and programmers a rich API to generate graphical reports based on Google Analytics data.  The reports module includes a helpful sitewide report located under <a href="!link">Drupal\'s default administrator reports</a>, and blocks for both sitewide and path-based reports.', array(
        '!link' => url('admin/reports'),
      )) . '</p>';
      $output .= '<p>' . t('Programmers can build complex queries against Google Analytics and display them in any format they like.  To get started coding your own reports, visit the <a href="!link">Google Analytics documentation page on drupal.org</a>.', array(
        '!link' => url('http://drupal.org/node/1138274'),
      )) . '</p>';
      return $output;
      break;
  }
}

/**
 * Implementation of hook_menu().
 */
function google_analytics_api_menu() {
  $items['admin/settings/google-analytics-reports'] = array(
    'title' => 'Google Analytics Reports',
    'description' => 'Configure your account, profiles, and reports.',
    'page callback' => 'drupal_get_form',
    'file' => 'google_analytics_api.pages.inc',
    'page arguments' => array(
      'google_analytics_api_admin',
    ),
    'access arguments' => array(
      'administer google analytics reports',
    ),
  );

  /* OAuth callback from Google */
  $items['google-analytics-reports/oauth'] = array(
    'title' => 'Google Analytics Reports OAuth Callback',
    'access callback' => TRUE,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'google_analytics_reports_oauth_callback',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'google_analytics_api.pages.inc',
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function google_analytics_api_perm() {
  return array(
    'administer google analytics reports',
  );
}

/**
 * Implements hook_theme().
 */
function google_analytics_api_theme() {
  return array(
    'google_analytics_api_profile_label' => array(
      'arguments' => array(
        'profile' => NULL,
      ),
    ),
  );
}

/**
 * Instantiate a new GAFeed object.
 */
function google_analytics_api_new_gafeed() {
  module_load_include('inc', 'google_analytics_api', 'GAFeed.lib');
  $key = variable_get('google_analytics_reports_consumer_key', 'anonymous');
  $secret = variable_get('google_analytics_reports_consumer_secret', 'anonymous');
  $oauth_token = variable_get('google_analytics_reports_oauth_token', NULL);
  $oauth_token_secret = variable_get('google_analytics_reports_oauth_token_secret', NULL);
  return new GAFeed($key, $secret, $oauth_token, $oauth_token_secret);
}

/**
 * Request account data.
 *
 * @param $params
 *   An associative array containing:
 *   - start-index: optional [default=1]
 *   - max-results: optional [default=20]
 * @param $cache_options
 *   An optional associative array containing:
 *   - cid: optional [default=md5 hash]
 *   - expire: optional [default=CACHE_TEMPORARY]
 *   - refresh: optional [default=FALSE]
 */
function google_analytics_api_account_data($params = array(), $cache_options = array()) {
  $GAFeed = google_analytics_api_new_gafeed();
  $GAFeed
    ->queryAccountFeed($params, $cache_options);
  return $GAFeed;
}

/**
 * Request report data.
 *
 * @param $params
 *   An associative array containing:
 *   - profile_id: required [default=variable_get('google_analytics_reports_profile_id')]
 *   - metrics: required.
 *   - dimensions: optional [default=none]
 *   - sort_metric: optional [default=none]
 *   - filters: optional [default=none]
 *   - segment: optional [default=none]
 *   - start_date: optional [default=GA release date]
 *   - end_date: optional [default=today]
 *   - start_index: optional [default=1]
 *   - max_results: optional [default=10,000]
 * @param $cache_options
 *   An optional associative array containing:
 *   - cid: optional [default=md5 hash]
 *   - expire: optional [default=CACHE_TEMPORARY]
 *   - refresh: optional [default=FALSE]
 */
function google_analytics_api_report_data($params = array(), $cache_options = array()) {
  $params_defaults = array(
    'profile_id' => 'ga:' . variable_get('google_analytics_reports_profile_id', 0),
  );
  $params += $params_defaults;
  $GAFeed = google_analytics_api_new_gafeed();
  $GAFeed
    ->queryReportFeed($params, $cache_options);
  return $GAFeed;
}

/*
 * Programatically revoke token.
 */
function google_analytics_api_revoke() {
  $GAFeed = google_analytics_api_new_gafeed();
  $GAFeed
    ->revokeToken();
  variable_del('google_analytics_reports_profile_id');
  variable_del('google_analytics_reports_consumer_key');
  variable_del('google_analytics_reports_consumer_secret');
  variable_del('google_analytics_reports_oauth_token');
  variable_del('google_analytics_reports_oauth_token_secret');
  variable_del('google_analytics_reports_cache_length');
}

/**
 * Sets the expiry timestamp for cached queries.
 *
 * Default is 3 days.
 *
 * @return The UNIX timestamp to expire the query at.
 */
function google_analytics_reports_cache_time() {
  return time() + variable_get('google_analytics_reports_cache_length', 259200);
}

/**
 * Theme the full string label of profiles.
 *
 * @return Themed string label.
 */
function theme_google_analytics_api_profile_label($profile) {
  return $profile['profileName'] . ' - ' . $profile['webPropertyId'];
}

/**
 * Implements hook_domainconf().
 */
function google_analytics_api_domainconf() {
  $profile_id = variable_get('google_analytics_reports_profile_id', 0);
  $form['google_analytics_reports'] = array(
    '#type' => 'fieldset',
    '#title' => t('Google Analytics Reports'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  if ($profile_id) {
    module_load_include('inc', 'google_analytics_api', 'google_analytics_api.pages');
    $admin_form = google_analytics_api_admin();
    $form['google_analytics_reports']['google_analytics_reports_profile_id'] = $admin_form['ga']['google_analytics_reports_profile_id'];
    $form['google_analytics_reports']['google_analytics_reports_profile_id']['#description'] = t('Choose your Google Analytics profile.');
  }
  else {
    $form['google_analytics_reports']['authorize']['#value'] = '<p>' . t('You must <a href="!url">authorize</a> Drupal to use your Analytics account before you can view reports.', array(
      '!url' => url('admin/settings/google-analytics-reports'),
    )) . '</p>';
  }
  return $form;
}

Functions

Namesort descending Description
google_analytics_api_account_data Request account data.
google_analytics_api_domainconf Implements hook_domainconf().
google_analytics_api_help Implementation of hook_help().
google_analytics_api_menu Implementation of hook_menu().
google_analytics_api_new_gafeed Instantiate a new GAFeed object.
google_analytics_api_perm Implementation of hook_perm().
google_analytics_api_report_data Request report data.
google_analytics_api_revoke
google_analytics_api_theme Implements hook_theme().
google_analytics_reports_cache_time Sets the expiry timestamp for cached queries.
theme_google_analytics_api_profile_label Theme the full string label of profiles.

Constants