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

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

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

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

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

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

Brendon George's picture

My company used to work on WordPress. But there was a recent shift to Drupal because of some server financial issues. I was a complete newbie when I visited this site for the first time. Your website really helped me a lot. Thanks for sharing such educational articles.
omnitech support

steave's picture

Thank you for sharing this about Samsung refrigerator reviews. It’s aware me about this. I’m going to share this on to my friends. visit this website

phill's picture

Your work is phenomenal and I like you and jumping for some more significant posts about Medical Assistance. It's grateful you for giving noteworthy data to us. visit this website

Mike Cooper's picture

A commitment of gratefulness is set up with respect to giving this fascinating online journal to us about Medical Assistance. It is my pleasure to being here on your web journal. I need to return here for new post from your site.
Click This Site

Stephen Bishop's picture

I think the way you compose craves having so much ardor and you have placed it in a manner that everybody can get it. Click To Read More

Stephen Bishop's picture

I am amazingly euphoric to investigate about Learning English. This is the sort of manual that needs to be given and not the fitful confusion that is at trade destinations. Official Website

nlcvchsc393's picture

http://www.kobbilligenewbalance.org
middle finger.Jack GranthamKnoxvillePhoto AP Photo / Brett FlashnickCrew members for Regan Smith, celebrate after winning the NASCAR Sprint Cup Series Showtime Southern at Darlington Raceway Saturday, May , ,in Darlington, S.C.Smithwood Baptist Church in Fountain City is holding a Trunkortreat in the church parking lot from p.m., Sunday, Oct. . The event will be inside in the event of rain.Stop for fun and candy!Smithwood Baptist Church in Fountain City is offering a PowerPoint computer class from p.m. on Nov. , , and . The co
http://www.kobbilligenewbalance.org
it's not leadership. We were sent here to serve our citizens, not our ambitions. So let's show the American people that we can do it together. This week, I'll be addressing a meeting of the House Republicans. And I would to begin monthly meetings with both the Democratic and Republican leadership. I know you can't wait.Throughout our history, no issue has united this country more than our security. Sadly, some of the unity we felt after / has dissipated. We can argue all we want about who's to blame for this, but I am not inte
http://www.kobbilligenewbalance.org
equipment failure, human error and bad weather during its nineyear operating history, according to official records.Spring Fling : Day FourDETROITThe delegates attending the United Auto Workers' threeday bargaining convention in Detroit were treated to a rousing, inspirational message Wednesday from rap star Eminem.In a moving, threeminute video, Eminem implored UAW members to never give up.You took our country from its infancy into industry, Eminem said.The EmmittNershi Bandconcentrateson bluegrassrather thanrockorientedmusic
http://www.kobbilligenewbalance.org
,.Photo Bob FowlerWith the entire student body of Briceville Elementary School listening inside the historic Briceville Church, entertainer Tony Thomas, left, weaves a tall tale during dedication ceremonies Friday.CLINTONA Briceville area man was charged with seconddegree murder Thursday in the Wednesday night shooting death of his neighbor, the Anderson County Sheriff's Office said.Authorities allege Larry Joshua Ramsey, , killed Bob Allen Turpin, , after Turpin accused Ramsey's son of stealing a pocket knife. Police say Rams

nlcvchsc393's picture

http://www.kobbilligenewbalance.org
middle finger.Jack GranthamKnoxvillePhoto AP Photo / Brett FlashnickCrew members for Regan Smith, celebrate after winning the NASCAR Sprint Cup Series Showtime Southern at Darlington Raceway Saturday, May , ,in Darlington, S.C.Smithwood Baptist Church in Fountain City is holding a Trunkortreat in the church parking lot from p.m., Sunday, Oct. . The event will be inside in the event of rain.Stop for fun and candy!Smithwood Baptist Church in Fountain City is offering a PowerPoint computer class from p.m. on Nov. , , and . The co
http://www.kobbilligenewbalance.org
it's not leadership. We were sent here to serve our citizens, not our ambitions. So let's show the American people that we can do it together. This week, I'll be addressing a meeting of the House Republicans. And I would to begin monthly meetings with both the Democratic and Republican leadership. I know you can't wait.Throughout our history, no issue has united this country more than our security. Sadly, some of the unity we felt after / has dissipated. We can argue all we want about who's to blame for this, but I am not inte
http://www.kobbilligenewbalance.org
equipment failure, human error and bad weather during its nineyear operating history, according to official records.Spring Fling : Day FourDETROITThe delegates attending the United Auto Workers' threeday bargaining convention in Detroit were treated to a rousing, inspirational message Wednesday from rap star Eminem.In a moving, threeminute video, Eminem implored UAW members to never give up.You took our country from its infancy into industry, Eminem said.The EmmittNershi Bandconcentrateson bluegrassrather thanrockorientedmusic
http://www.kobbilligenewbalance.org
,.Photo Bob FowlerWith the entire student body of Briceville Elementary School listening inside the historic Briceville Church, entertainer Tony Thomas, left, weaves a tall tale during dedication ceremonies Friday.CLINTONA Briceville area man was charged with seconddegree murder Thursday in the Wednesday night shooting death of his neighbor, the Anderson County Sheriff's Office said.Authorities allege Larry Joshua Ramsey, , killed Bob Allen Turpin, , after Turpin accused Ramsey's son of stealing a pocket knife. Police say Rams

nlcvchsc393's picture

http://www.kobbilligenewbalance.org
middle finger.Jack GranthamKnoxvillePhoto AP Photo / Brett FlashnickCrew members for Regan Smith, celebrate after winning the NASCAR Sprint Cup Series Showtime Southern at Darlington Raceway Saturday, May , ,in Darlington, S.C.Smithwood Baptist Church in Fountain City is holding a Trunkortreat in the church parking lot from p.m., Sunday, Oct. . The event will be inside in the event of rain.Stop for fun and candy!Smithwood Baptist Church in Fountain City is offering a PowerPoint computer class from p.m. on Nov. , , and . The co
http://www.kobbilligenewbalance.org
it's not leadership. We were sent here to serve our citizens, not our ambitions. So let's show the American people that we can do it together. This week, I'll be addressing a meeting of the House Republicans. And I would to begin monthly meetings with both the Democratic and Republican leadership. I know you can't wait.Throughout our history, no issue has united this country more than our security. Sadly, some of the unity we felt after / has dissipated. We can argue all we want about who's to blame for this, but I am not inte
http://www.kobbilligenewbalance.org
equipment failure, human error and bad weather during its nineyear operating history, according to official records.Spring Fling : Day FourDETROITThe delegates attending the United Auto Workers' threeday bargaining convention in Detroit were treated to a rousing, inspirational message Wednesday from rap star Eminem.In a moving, threeminute video, Eminem implored UAW members to never give up.You took our country from its infancy into industry, Eminem said.The EmmittNershi Bandconcentrateson bluegrassrather thanrockorientedmusic
http://www.kobbilligenewbalance.org
,.Photo Bob FowlerWith the entire student body of Briceville Elementary School listening inside the historic Briceville Church, entertainer Tony Thomas, left, weaves a tall tale during dedication ceremonies Friday.CLINTONA Briceville area man was charged with seconddegree murder Thursday in the Wednesday night shooting death of his neighbor, the Anderson County Sheriff's Office said.Authorities allege Larry Joshua Ramsey, , killed Bob Allen Turpin, , after Turpin accused Ramsey's son of stealing a pocket knife. Police say Rams

Stephen Bishop's picture

I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will locate that extremely useful about IPage.

Stephen Bishop's picture

I was able to find the details that I was searching for. I must thank you for the initiatives you have made in writing IPage article. I am expecting the similar best efforts from you in the future as well.

Stephen Bishop's picture

We're more than ten years in custom writing business about IPage review. Hence, you should buy research paper help and get higher grades. Iipage reviews 2014

Stephen Bishop's picture

We're more than ten years in custom writing business about IPage review. Hence, you should buy research paper help and get higher grades. Iipage reviews 2014

Stephen Bishop's picture

I found the information quite helpful about Hostgator. Thanks for sharing this excellent information. I was in search of this vital information since long time. visit here

byxqakiweC9592's picture

szug http://www.classicnflteams.com
, les Argentins ont gagné leur trentequatrième victoire dans le classique sudaméricain, contre trentetrois à leur grand rival auriverde. C'est la première fois que l'Argentine s'impose face au Brésil en six confrontations depuis . Le Qatar est candidat à la Coupe du monde , dont l'organisateur sera désigné par la Fifa le décembre.La communauté des ingénieurs spécialisés dans les télécommun
http://www.classicnflteams.com
ire de sécurisation des parcours professionnels FPSPP avance à petits pas. Son entrée en vigueur ne devrait pas intervenir avant le mois de mars. En attendant, les partenaires sociaux ont élaboré, le janvier, un accord visant à déterminer l'affectation de ses ressources. Première nouveauté, le fonds d'urgence formation, lancé en avril et doté de millions d'euros issus du FPSPP, sera prolongé po

byxqakiweC9592's picture

szug http://www.classicnflteams.com
, les Argentins ont gagné leur trentequatrième victoire dans le classique sudaméricain, contre trentetrois à leur grand rival auriverde. C'est la première fois que l'Argentine s'impose face au Brésil en six confrontations depuis . Le Qatar est candidat à la Coupe du monde , dont l'organisateur sera désigné par la Fifa le décembre.La communauté des ingénieurs spécialisés dans les télécommun
http://www.classicnflteams.com
ire de sécurisation des parcours professionnels FPSPP avance à petits pas. Son entrée en vigueur ne devrait pas intervenir avant le mois de mars. En attendant, les partenaires sociaux ont élaboré, le janvier, un accord visant à déterminer l'affectation de ses ressources. Première nouveauté, le fonds d'urgence formation, lancé en avril et doté de millions d'euros issus du FPSPP, sera prolongé po

byxqakiweC9592's picture

szug http://www.classicnflteams.com
, les Argentins ont gagné leur trentequatrième victoire dans le classique sudaméricain, contre trentetrois à leur grand rival auriverde. C'est la première fois que l'Argentine s'impose face au Brésil en six confrontations depuis . Le Qatar est candidat à la Coupe du monde , dont l'organisateur sera désigné par la Fifa le décembre.La communauté des ingénieurs spécialisés dans les télécommun
http://www.classicnflteams.com
ire de sécurisation des parcours professionnels FPSPP avance à petits pas. Son entrée en vigueur ne devrait pas intervenir avant le mois de mars. En attendant, les partenaires sociaux ont élaboré, le janvier, un accord visant à déterminer l'affectation de ses ressources. Première nouveauté, le fonds d'urgence formation, lancé en avril et doté de millions d'euros issus du FPSPP, sera prolongé po

click here's picture

It is Pleasant details and numbers, many thanks for describe about Fat Loss Factor Review. It is incomprehensible to me now, but in general, the usefulness and implication is swamping. Thanks afresh and good luck, click here

Grace's picture

I found the entire Software Tailor team to very helpful and knowledgeable. You help me understand the differences between the other systems on the market and the Software Tailor system.
Software Development

SonjitKumar's picture

Thank you for this posting. I think it would be helpful for all. Personally I like this post. Fat Loss Factor Review

SonjitKumar's picture

Nice offer. This offer is really attractive to all. personally I like Mi40X Reviewit.

Pages

Add new comment