Hello everyone,

First, this is a WONDERFUL module, thank you for all your hard work.

Second, Google now recommends inserting a try/catch around the google analytics code, per the specification on this page.

http://code.google.com/apis/analytics/docs/gaTrackingOverview.html

Note the try/catch wrapping in the sample, below. I'm wondering if we could add this try/catch block into the module so it uses Google's recommendation.

Thanks!

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
} catch(err) {}</script>

Comments

hass’s picture

Thank you very much for this hint. This seems to be a *very* new update to this documentation pages... As it's an official recommendation now - I will add this.

trumpcar’s picture

Thank you for the quick reply and action. Please let me know where I could grab and test. :)

hass’s picture

Status: Active » Fixed
trumpcar’s picture

So quick.. thanks team for making these changes so quickly. I just tested and it looks great.

hass’s picture

Status: Fixed » Needs work

Ups, I missed the onclick tracking in googleanalytics.js...

Tiaan’s picture

In addition to the remaining update on the "googleanalytics.js" file, you might also want to change the code for the "segmentation variable" in the "googleanalytics.module" file. My own patch a while back looked something like the following for the two files, but I haven't verified the "onclick tracking" yet...

Index: googleanalytics.module
===================================================================
--- googleanalytics.module	(revision 256)
+++ googleanalytics.module	(revision 292)
@@ -109,7 +109,7 @@
       // Only show segmentation variable if there are specified fields.
       $segmentation = '';
       if (count($fields) > 0) {
-        $segmentation = 'pageTracker._setVar('. drupal_to_js(implode(':', $fields)) .');';
+        $segmentation = 'if (pageTracker) pageTracker._setVar('. drupal_to_js(implode(':', $fields)) .');';
       }
     }
 
@@ -149,14 +149,14 @@
     $codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');
 
     // Build tracker code for footer.
-    $script = 'var pageTracker = _gat._getTracker('. drupal_to_js($id) .');';
+    $script = 'var pageTracker = null; try { pageTracker = _gat._getTracker('. drupal_to_js($id) .'); ';
     if (!empty($segmentation)) {
       $script .= $segmentation;
     }
     if (!empty($codesnippet_before)) {
       $script .= $codesnippet_before;
     }
-    $script .= 'pageTracker._trackPageview('. $url_custom .');';
+    $script .= 'pageTracker._trackPageview('. $url_custom .'); } catch (exception) {}';
     if (!empty($codesnippet_after)) {
       $script .= $codesnippet_after;
     }

Index: googleanalytics.js
===================================================================
--- googleanalytics.js	(revision 256)
+++ googleanalytics.js	(revision 292)
@@ -4,6 +4,10 @@
 
   // Attach onclick event to all links.
   $('a', context).click( function() {
+    if (!pageTracker) {
+      // Skip tracking if the tracker could not be loaded.
+      return;
+    }
     var ga = Drupal.settings.googleanalytics;
     // Expression to check for absolute internal links.
     var isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i");

hass’s picture

Status: Needs work » Fixed

Fixed the JS files.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.