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.

Visit here's picture

I recently came across your Internet Slang blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog, I will keep visiting this blog very often. Visit here

Unique Hoodia's picture

Thanks for taking this opportunity to discuss this; I appreciate with this and if you have some more information please share it with me about Dermal Piercing.
visit here

metal carport kits's picture

Investigating in Yahoo I finally uncovered this site. Looking at this data so I'm euphoric to pass on that I have a breathtakingly respectable uncanny feeling I found fundamentally what I required. Thank Emergency Dentist. visit here

www.bestepilatorguide.org's picture

I know this is kinda off topic but I was wondering which blog platform are you using for this website? I'm getting fed up of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform.

dogtrainingphoenix.net's picture

Most people will never allow its children so that you can toil hard for their higher instruction programs plus deny these folks recognition. It can be wrong to try this as your parent. The most wonderful thing you is capable of doing is to organize for an effective graduation ceremony for your personal child. eyecandyentertainment.net

metal carport kits's picture

These sorts' rentals are typically readily set at contrivances that surely are focused through fine-looking shores offering totally clear waters, probability the Pacific Ocean. Lodgings packs you completed several fine centres there. Thank Metal Carports.

Pocket Knife Guides's picture

I'm looking forward for your next post, I will try to get the hang of it!

www.bestpocketknifeguides.org

Quonset Hut's picture

certainly like what you are stating and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I can not wait to read much more from you. This is actually a tremendous site.

official website

dogtrainingphoenix.net's picture

Resulting from our present day living standard we're also creating trap for the next generating; that is the pollution we're also also entering into to eradicate forest to set-up forest with concrete. Plus; constructionist.net

Official website's picture

A fantastic web page about Online Advertising campaign always comes-up using new and exciting information and even though analyzing I have think this webpage is absolutely have all those fantastic which can be qualified a webpage to become fantastic one.
Official website

Unique Hoodia's picture

Additionally all you give to us is update and genuinely informative, an obligation of appreciation is in place with respect to advertising.

dogtrainingphoenix.net's picture

Once we tend to apprehend, fashion are sometimes terribly tough which you can follow. In a strong trade when one colouring, material and also pattern are sometimes insanely hot one minute and wholly out right after, there's minimal doubt this maintaining isn't an straight send task. To not ever worry, we're here that may help you type by all the latest fashion news flash and fads and continue to keep you making an attempt stylish. fashionartmusic.org

official website's picture

This content you could have published includes huge concept came into existence effective. Hi, very first I've got to declare Tinnitus articles with this weblog have become useful to see. http://goo.gl/BWLz6b

Unique Hoodia's picture

I think the way you create has a yearning for having so much vitality and you have set it in a way that everyone can get it. Must respect the uncommon posts you have formed here about Godaddy Promo Code article.

dogtrainingphoenix.net's picture

When you are within design, you could very well can find this everyone in the world enjoys Design Week enjoy it is Year. You're visiting comprehend why they should holiday over their own base looking for ways to some front-row Harley hold and ogling from the extremely decisions. fashionschoenen.org

buy facebook fans's picture

I am very impressed with the information you are posting here. Thanks for sharing and tell us extra about new releases. Thanks for taking the time to share about Social Media Agency and this valuable analysis with everyone buy facebook fans

dogtrainingphoenix.net's picture

Fashion allows us to know quite a lot around your someone, exactly how that they enable, that people place its attire around concert suggests any person they are actually each working day. Possibly even folks who claim to not ever ever learn about see happy exactly how that people feel having said that show an account concerning who they are really as perfectly as whatever we play the role of. fashionsmart.org

Click Here's picture

Thanks for sharing nice information about Tinnitus article with us. i like your post and all you share with us is update and quite informative, thanks for sharing. Click Here

Click Here's picture

This is really a great read for me. Thank you for publishing articles having a great insight stimulates me to check more often for new write about Acne No More Review. Visit Website

Click here's picture

Good data, thank you to McDougal. It's incomprehensible if you ask me now, in general, the meaning and performance is frustrating. Thanks again for nice information about Ben 10 Ultimate Alien and best of luck. Click here

Click here's picture

You have opened my eyes to varying views on this topic with interesting and solid content.
The new Zune browser is surprisingly good, but not as good as the iPod’s. Click here

Click here's picture

Think about adjusting your own believed process and giving others who may read this the benefit of the doubt. find more information

<a href="http://www.bestwebhostingdeal.net">Visit here</a>'s picture

I just couldn’t depart your site prior to suggesting that I extremely enjoyed the bluehost coupon standard information an individual provide for your visitors? Is gonna be back frequently in order to inspect new posts hey there, your site is cheap. Visit here

<a href="http://www.bestwebhostingdeal.net">Visit here</a>'s picture

Hitting some of those will probably center on in which piece Previous Institution New Body, as well as yet another group of “neighbors” should come into view, enabling you to understand close to researching by comparable artists, tunes, or perhaps users. http://goo.gl/ltYySI

<a href="http://www.bestwebhostingdeal.net">Visit here</a>'s picture

Your amazing insightful information entails much to me and especially to my peers. Thanks Tinnitus Miracle; from all of us.
Tinnitus Miracle Scam

Yeast Infection's picture

I just found ones Candidiasis blog site and have absolutely been reading along. When i believed I might get away from the primary opinion. When i don’t determine what to state other than that we have got relished reading. Good blog site, I am going to hold viewing this site usually.
Yeast Infection

Acne No More's picture

I wish to say that this post is amazing about Acne No More, nice written and include approximately all important info. Glad to found this post. I would like to see more posts like this. Salute. Acne No More

Fat Loss Factor review's picture

I discovered the information really valuable about Fat loss Element. Thank you intended for discussing this excellent info. I seemed to be on the lookout for this kind of essential info considering that very long time.
Fat Loss Factor review

Unique Hoodia's picture

I beginning late headed before long your online journal about Unique Hoodia and have been looking at along. I thought I would leave my first remark. I don't see what to say aside from that I have had a surprising time asking about. Better than ordinary site. I will continue taking off to this online journal frequently. Unique Hoodia

Click Here's picture

Your amazing insightful information entails much to me and especially to my peers. Thanks a ton; from all of us.

dogtrainingphoenix.net's picture

Remaining happy is a desire of each one person. People plan different times where people gather so that you can celebrate its lives plus share its life experiences utilizing their friends. Together with the plentiful cases of stressful events, entertainmentheadlinenews.net

Click Here's picture

Thanks for writing engaging and interesting material.Found this on MSN and I’m happy I did. Well written article.

christmas desktop backgrounds's picture

You completed a number of nice points there. I did a search on the issue and found nearly all people will have the same opinion with your blog.

Click Here's picture

hi!,I like your writing so much! share we keep up a correspondence extra approximately your post on AOL? I require a specialist on this space to solve my problem. May be that is you! Looking ahead to peer you.Click here

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

Pages

Add new comment