? .svn
? cache_51.patch
? reports/.svn
? reports/translations/.svn
? tests/.svn
? translations/.svn
Index: google_analytics_api.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics_api/google_analytics_api.module,v
retrieving revision 1.25
diff -u -p -r1.25 google_analytics_api.module
--- google_analytics_api.module	9 Jan 2010 01:34:37 -0000	1.25
+++ google_analytics_api.module	28 Apr 2010 22:43:22 -0000
@@ -1,10 +1,5 @@
 <?php
-// $Id: google_analytics_api.module,v 1.25 2010/01/09 01:34:37 jkitching Exp $
-
-/**
- * @file
- * Implements the API through which Google Analytics data can be accessed.
- */
+// $Id: google_analytics_api.module,v 1.21 2009/08/23 22:02:44 jkitching Exp $
 
 require 'gapi.class.php';
 
@@ -13,12 +8,12 @@ require 'gapi.class.php';
  */
 function google_analytics_api_menu() {
   $items['admin/settings/google-analytics-api'] = array(
-    'title' => 'Google Analytics API settings',
-    'description' => 'Manage the currently logged in Analytics account.',
+    'title' => t('Google Analytics API settings'),
+    'description' => t('Manage the currently logged in Analytics account'),
     'page callback' => 'drupal_get_form',
     'file' => 'google_analytics_api.admin.inc',
     'page arguments' => array('google_analytics_api_admin'),
-    'access arguments' => array('administer google analytics settings'),
+    'access arguments' => array('access administration pages'),
     'type' => MENU_NORMAL_ITEM,
   );
 
@@ -29,16 +24,16 @@ function google_analytics_api_menu() {
  * Implementation of hook_perm().
  */
 function google_analytics_api_perm() {
-  return array('administer google analytics settings');
+  return array('administer Google Analytics settings');
 }
 
 /**
  * Return current token string.
  */
 function google_analytics_api_current_token($silent = FALSE) {
-  $token = variable_get('google_analytics_api_token', NULL);
+  $token = variable_get('google_analytics_api_token', null);
   if (!$token && !$silent) {
-    drupal_set_message(t('A Google Analytics API method has been called that requires access to a Google account. Please visit the <a href="@admin">administration page</a> to grant access.', array('@admin' => url('admin/settings/google_analytics_api'))));
+    drupal_set_message(t('A Google Analytics API method has been called that requires access to a Google account.  Please visit the ' . l('administration page', 'admin/settings/google_analytics_api') . ' to grant access.'));
   }
   return $token;
 }
@@ -47,9 +42,9 @@ function google_analytics_api_current_to
  * Return current profile ID.
  */
 function google_analytics_api_current_profile_id($silent = FALSE) {
-  $profile_id = variable_get('google_analytics_api_profile_id', NULL);
+  $profile_id = variable_get('google_analytics_api_profile_id', null);
   if (!$profile_id && !$silent) {
-    drupal_set_message(t('A Google Analytics API method has been called that requires the selection of a Google Analytics profile. Please visit the <a href="@admin">administration page</a> to set one.', array('@admin' => url('admin/settings/google_analytics_api'))));
+    drupal_set_message(t('A Google Analytics API method has been called that requires the selection of a Google Analytics profile.  Please visit the ' . l('administration page', 'admin/settings/google_analytics_api') . ' to set one.'));
   }
   return $profile_id;
 }
@@ -64,7 +59,7 @@ function google_analytics_api_current_pr
       return $profile;
     }
   }
-  return NULL;
+  return null;
 }
 
 /*
@@ -83,8 +78,8 @@ function google_analytics_api_logout() {
   }
   catch (Exception $e) {
   }
-  variable_set('google_analytics_api_token', NULL);
-  variable_set('google_analytics_api_profile_id', NULL);
+  variable_set('google_analytics_api_token', null);
+  variable_set('google_analytics_api_profile_id', null);
 }
 
 /*
@@ -93,7 +88,7 @@ function google_analytics_api_logout() {
 function google_analytics_api_gapi() {
   static $gapi;
 
-  if (user_access('administer google analytics settings')) {
+  if (user_access('administer Google Analytics settings')) {
     if ($gapi) {
       return $gapi;
     }
@@ -129,41 +124,71 @@ function google_analytics_api_account_da
  * @param $request['#end_date']    optional [default=today]
  * @param $request['#start_index'] optional [default=1]
  * @param $request['#max_results'] optional [default=10,000]
+ * 
+ * @param $cid                     required 
+ *   A unique cid for this request, you can re-use the same
+ *   cid if you are making the same query in more than
+ *   one place and want to re-use the results.
+ * @param $expire                  optional
+ *   The time to expire this cid, defaults to CACHE_NORMAL.
+ * @param $refresh                 optional
+ *   Use to force a fresh request even if there is cached data.
  */
-function google_analytics_api_report_data($request) {
-  // Make sure parameters are all there with a default value.
-  $params = array(
-    '#dimension' => NULL,
-    '#metrics' => NULL,
-    '#sort_metric' => NULL,
-    '#filter' => NULL,
-    '#start_date' => NULL,
-    '#end_date' => NULL,
-    '#start_index' => 1,
-    '#max_results' => 10000,
-  );
-  foreach ($params as $name => $value) {
-    if (!isset($request[$name])) {
-      $request[$name] = $value;
-    }
-  }
+function google_analytics_api_report_data($request, $cid = NULL, $expire = NULL, $refresh = FALSE) {
 
   $gapi = google_analytics_api_gapi();
   if (!$gapi) {
     return FALSE;
   }
 
-  // TODO: Insert a check to see if we have this query cached.
-  try {
-    $data = google_analytics_api_gapi()->requestReportData(
-      variable_get('google_analytics_api_profile_id', 0),
-      $request['#dimensions'], $request['#metrics'], $request['#sort_metric'],
-      $request['#filter'], $request['#start_date'], $request['#end_date'],
-      $request['#start_index'], $request['#max_results']);
+  // Use the request object to generate a cache id. This lets us cache per request.
+  if (!$cid) { 
+    // Recursively iterate over the array to flatten it
+    $flattened = array();
+    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($request));
+    foreach ($iterator as $value) { 
+      $flattened[] = $value;
+    }
+    $cid = md5(implode('', $flattened));
   }
-  catch (Exception $e) {
-    drupal_set_message(t('Google Analytics API: @message', array('@message' => $e->getMessage())));
-    return FALSE;
+
+  $cache = cache_get($cid);
+  if (!$refresh && isset($cache) && !empty($cache->data)) {
+    $data = $cache->data;
+  }
+  else { 
+    try {
+      // Make sure parameters are all there with a default value.
+      $params = array(
+        '#dimension' => null,
+        '#metrics' => null,
+        '#sort_metric' => null,
+        '#filter' => null,
+        '#start_date' => null,
+        '#end_date' => null,
+        '#start_index' => 1,
+        '#max_results' => 10000,
+      );
+      foreach ($params as $name => $value) {
+        if (!isset($request[$name])) {
+          $request[$name] = $value;
+        }
+      }
+      $data = google_analytics_api_gapi()->requestReportData(
+        variable_get('google_analytics_api_profile_id', 0),
+        $request['#dimensions'], $request['#metrics'], $request['#sort_metric'],
+        $request['#filter'], $request['#start_date'], $request['#end_date'],
+        $request['#start_index'], $request['#max_results']);
+        if (empty($expire)) {
+          $expire = CACHE_NORMAL;
+        }
+        cache_set($cid, $data, 'cache', $expire);
+    }
+    catch (Exception $e) {
+      watchdog('google_analytics_api', t('Google Analytics API: cid @cid @message', array('@cid' => $cid, '@message' => $e->getMessage())));
+      return FALSE;
+    }
   }
+
   return $data;
 }
