Install Multiple Tracking Codes for Google Analytics on a Drupal Site
At the Strong, we have 6 separate Drupal sites in a Multisite installation. Before that, we had 3 websites. We have been using Google Analytics for the past few years to track data, especially since the inception of the American Journal of Play in July 2008 and ICHEG in March 2009. Each site had its own profile and tracking code.
Now that we have 6 websites, maintaining 6 profiles in Google Analytics is a bit cumbersome. What if we want to see total visitation for all sites? We have to manually add the 6 values together.
In an ideal world, we should have one master profile, and 6 sub-profiles.
One tracking code can be used in this way and is in fact recommended by Google. You can set up filters in different profiles to display the domain name, and then filter only that domain in a sub-profile. But we would have to start over with a new tracking code, and we have a few years of historical data that we don't want to lose.
What about using two tracking codes? Although it's not recommended (see also this post and this one), it is possible..
There is a post explaining how to do it here, and it appears there are no major problems. But there's no instructions for the new asynchronous Javascript code...
Multiple Tracking Codes with Asynchronous GA Code
The new Google code uses a function called _gaq.push() to queue requests sent to Google. So, essentially, you just need to queue up two blocks of code. Here's the code I found that works:
var _gaq = _gaq || [];
_gaq.push(["_setAccount", "UA-XXXXXXX-1"]);
_gaq.push(["_setDomainName", "none"]);
_gaq.push(["_setAllowLinker", true]);
_gaq.push(["_trackPageview"]);
_gaq.push(["_setAccount", "UA-XXXXXXX-2"]);
_gaq.push(["_setDomainName", "none"]);
_gaq.push(["_setAllowLinker", true]);
_gaq.push(["_trackPageview"]);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
_gaq.push(["_setAccount", "UA-XXXXXXX-1"]); sets the account to the first profile. _setDomainName and _setAllowLinker are suggested to add in this post and are the asynchronous equivalents of that post's code.
These 3 lines are queued but are not sent to Google just yet. _gaq.push(["_trackPageview"]); sends the request, and clears the queue. That's why we can start with _setAccount again and queue up settings. Then the final _trackPageview sends the second bit of code.
Multiple Tracking Codes in Drupal with Google Analytics Module
If you're using Google Analytics with Drupal, you're probably using the Google Analytics module. So, how do we add this code in the settings?
Note: These instructions are for Google Analytics 6.x-3.0.
First, go to Administer -> Site Configuration -> Google Analytics.
All the way at the bottom, expand Advanced settings.
Then expand Custom Javascript code.
So, what to add? What's not clear here is that the Google Analytics module automatically adds two lines of _gaq.push() calls to the tracking code, and doesn't allow you to edit them. So, the information put in Code snippet (before) and Code snippet (after) would be output like so on the page:
_gaq.push(["_setAccount", "UA-XXXXXXX-1"]); /** * code snippet before */ _gaq.push(["_trackPageview"]); /** * code snippet after */
Where UA-XXXXXXX-1 is your first account profile ID, and the value of Web Property ID that you filled out at the top of the settings form.
So to recreate the multiple tracking code in Drupal, we would fill the before and after like so:
Before:
_gaq.push(["_setDomainName", "none"]); _gaq.push(["_setAllowLinker", true]);
After:
_gaq.push(["_setAccount", "UA-XXXXXXX-2"]); _gaq.push(["_setDomainName", "none"]); _gaq.push(["_setAllowLinker', true]); _gaq.push(["_trackPageview"]);
Where UA-XXXXXXX-2 is the second account profile ID. So the Settings form would appear like so:
And there you have it! I've confirmed that this works for our sites.
In theory, you could put 3 tracking codes if you wanted; just repeat the four lines in the "After" snippet again with the third profile ID for _setAccount, but I wouldn't recommend that.
Hope this helps someone out there!




Comments
Yes, quite helpful! Thanks very much.
Thanks Andy, I'm glad it was helpful! I was hoping to fill some gaps on on the Interwebs.
Yep, the code works on my multisite. Just waiting for google to aggregate the data.
Thx Wes
Wow. That's the result i couldn't even expect. Surprised it is as easy. Thanks.
Add new comment