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.

Advanced settings

All the way at the bottom, expand Advanced settings.

Custom Javascript code

Then expand Custom Javascript code.

Code snippet (before) and (after)

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:

Correct settings

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

Andy's picture

Yes, quite helpful! Thanks very much.

wjones's picture

Thanks Andy, I'm glad it was helpful! I was hoping to fill some gaps on on the Interwebs.

Attila Hooper's picture

Yep, the code works on my multisite. Just waiting for google to aggregate the data.

Thx Wes

Oprah Day's picture

Wow. That's the result i couldn't even expect. Surprised it is as easy. Thanks.

Julius Rainey, Jr.'s picture

I know that this is an old blog based on the Google Analytics module 6.x-3.0. The current Drupal site I have is using Google Analytics module 7.x -1.2. Can someone help me understand/how to track two different websites with a multi-site installation?

daniel's picture

I never knew about main profile and sub profiles of website. But is it safe?
http://www.ifeel.edu.in

Daniel S's picture

I had no idea that you could install multiple Google analytics codes on 1 websites and that was in 2011. I really am behind technology :)
Anyway, just finished reading this tutorial and it looks very interesting. Maybe a little complicated for me but I will try my best. Thanks for the effort of creating it and last but not least, you really have an awesome website. Continue doing what you are doing becasue it clearly is working very well.

Have a great day
Daniel Sandow
http://www.opzionibinarieitaly.com/

Jimmy S's picture

Awesome tutorial wjones! This was exactly what I was looking for... Thanks a lot
Have an awesome day
Jimmy
http://www.binaereoptionenonline.com/

Christian Louboutin's picture

Les chaussures sont la parfaite pas cher christian louboutin chaussures choix pour visualiser pleinement la qualité des femmes. Les gens disent que les femmes doivent avoir des traits féminins, seuls fous talons hauts, vraiment, Mesdames talons hauts montrent pleinement les vraies qualités. En outre, vous pouvez acheter pas cher Christian Louboutin www.vrluxe.com

Tina Wilsson's picture

Sounds like a project that wil take time to develop in the best way possible.
http://www.anyoptionbewertung.com

Markus's picture

Yep, the code works on my multisite. Just waiting for google to aggregate the data.

Markus @ http://digitalaoptioner.wordpress.com

Anonymous's picture

Google Analytics allows you to measure not only sales and conversions, but also provides information about how visitors use your site, how they got it and what you can do to continue visiting it.
Regards,
http://www.intercore.com.mx

Queen 's picture

Very nice.Thanks for sharing.
Software Development

evanb4s's picture

Thanks for sharing this, I had no idea how to instal multiple tracking codes for Google Analytics.
Regards,
dirección

Marco's picture

This code is not / no longer working.

dh's picture

Google Analytics e-commerce reporting can trail businesss action also stunt. The e-business informations evidences a spot's transactions, receipts, also numerous different industry-analogous metrics.
verbatim transcription services

dh's picture

gsg

Add new comment