Skip to Content

Drupal

Pixelite: Updating Drupal to use Google Analytics Universal tracking

Planet Drupal - 30 June 2014 - 9:58pm
Facebook Like Google Plus One Linkedin Share Button

So Google Analytics has a new version of Google Analytics dubbed "Universal Analytics", which has a bunch of new features, that could be handy for your website. I would dive into exactly what they are here, as you can read about them on Google's own website.

In this post I will go through the steps to upgrade the Google Analytics 7.x-1.x module to the new 7.x-2.x version that supports Universal Analytics.

Update the Drupal module

If you read the Google Analytics module page you will spot that there are two different branches in use, in order to get the correct version you will need to get the 7.x-2.x version.

You can do this with Drush:

drush dl google_analytics-7.x-2.x drush updb Event tracking

If you have used custom event tracking in your website, a few changes are required.

Instead of

_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', opt_value, opt_noninteraction]);

It is now

ga('send', 'event', 'category', 'action', 'opt_label', opt_value, {'nonInteraction': 1}); Handy grep command

If you want to find the offending lines of code, you can use grep

grep -nrI "_trackEvent" * Custom variables are now dimensions and metrics

If you were using the old style custom variables, these are now completely gone, now replaced with dimensions and metrics. You can read more about these on Google's website.

Instead of

_gaq.push(['_setCustomVar', 1, // Slot 'Customer Type', // Name 'Paid', // Value 1 // Scope (1 = User scope) ]);

It is now

ga('set', 'dimension1', 'Paid'); Drupal support of custom dimensions and metrics

The Drupal module has an active issue that allows you to configure this through the UI, unfortunately this is still only a patch at the moment, but is looking likely to be committed shortly (it maybe already if you are reading this now). For now I patched the Google Analytics module with Drush make

; Google Analytics projects[google_analytics][type] = module projects[google_analytics][subdir] = contrib projects[google_analytics][version] = 2.x ; Implement custom dimensions and custom metrics ; https://www.drupal.org/node/2136031 projects[google_analytics][patch][] = "http://www.drupal.org/files/issues/google_analytics-2136031-1-custom_dimensions.patch" DoubleClick data

If you were using the additional data that DoubleClick integration provided, this is now supported, this is just a tickbox on the admin settings page.

To enable it

variable_set('googleanalytics_trackdoubleclick', 1) Other new features in Universal Analytics UserID tracking

This effectively allows you to track the same user across multiple devices. This comes in handy if your users can login to your Drupal site, and they would likely login on their mobile phones, and tablets etc. You can read more on Google's page about User ID tracking

To enable it

variable_set('googleanalytics_trackuserid', 1) Enhanced Link Attribution feature

Allows Google Analytics to differentiate URLs based on what link the user clicked on, really handy if you have many links pointing at the same page. You can read more on Google's page about User ID tracking

To enable it

variable_set('googleanalytics_tracklinkid', 1) Finally

Run this little gem over your codebase to ensure there are no legacy Google Analytics code lying around.

grep -nrI "_gaq" *

Let me know if you have any tips or tricks in the comments for the new Google Analytics

Tags drupal drupalplanet Google Analytics Source Drupal module for Google Analytics Category Tutorial
Categories: Drupal
Syndicate content


Google+
about seo