Migrating to Google Analytics Async Tracking Code

Jun 06 2010 Published by under analytics

Google Analytics Asynchronous Tracking Code

Recently Google Analytics moved the asynchronous JavaScript version of their tracking code out of beta; it is now the standard version of the code you will see when you visit your settings in Google Analytics. This is great, as it enhances the speed at which the analytics tracking code is loaded. If you haven’t done any customization to your tracking code, and you don’t have any “advanced” tagging on your site, migrating is as simple as getting the new version of the code from your profile settings page in Google Analytics (see screenshot), placing it in the <head> of your site, and removing your existing code. If you do have customizations, or you are tracking downloads, AJAX or Flash, off-site links, using Event Tracking or Custom Variables, then migration requires some additional work. This post is intended to direct you to right resources for these additional needs.

Customized Tracking Code

Customizations to tracking code can be as simple as modifying your code to track visitors across domains, or as complicated as setting custom variables or sending event tracking requests within the “tag”. Here is a text file displaying the migration of Google Analytics tracking code from the old version to the async version, using the code from this site as the example. In a nutshell, any function that previously was called with the “pageTracker.” prefix, is now wrapped in _gaqPush[] . The code on this site has been modified to trigger an event for every post viewed, to track who the author was. Read this post on tracking authors in WordPress with Google Analytics if you need clarification on the intended functionality. In the example script, you can see that I had to take the code that identifies and records the author, and modify it to use _gaqPush['_trackEvent'] instead of pageTracker._trackEvent(). You’ll also notice that the rest of the code is updated to the new async standards.

Downloads & Off-Site Links

Downloads and off-site links are typically tracked by using an onclick event to call pageTracker._trackPageview. Here is an example of how this has now changed.  The following HTML/JavaScript:

<a target=”_blank” href=”http://www.linkedin.com/in/adamware” onClick=”pageTracker._trackPageview(‘http://www.linkedin.com/in/adamware’);”>connect with me on LinkedIn</a>

Would need to be modified to:

<a target=”_blank” href=”http://www.linkedin.com/in/adamware” onClick=”_gaq.push(['_trackPageview', 'http://www.linkedin.com/in/adamware’]);”>connect with me on LinkedIn</a>

Notice again the the pageTracker code is ditched, and the attributes are now tucked into the gaqPush function with trackPageview.

Flash & AJAX

For requests made in Flash and AJAX, the change is essentially the same. Wrap any function calls (_trackPageview, _trackEvent, _setCustomVar) in the gaqPush function.

Events & Custom Variables

This is probably starting to sound like a broken record: take the existing function call and wrap it in the new gaqPush[] function. Here is an example of code that would be used to designate a visitor as registered using custom variables. It would have been:

trackPageview._setCustomVar(2, ‘RegisteredUser’, ’True’, 1);

but is now:

_gaq.push([‘_setCustomVar’, 2, ‘RegisteredUser’, ’True’, 1]);

Pretty basic modifications here.The important thing is just remembering where all the locations on your site are where you are using custom modifications. Most of our clients are at least using download tracking and off-site link tracking in some way, and many are using events and custom variables. The migration process won’t be immediate for some of them, but hopefully everyone will be using the async code within a couple months. I’d love to hear thoughts on any ways to make this process easier, and certainly on any details I may have left out.  Here is Google Analytics’ official Google Code page on migration.

3 responses so far