03 Apr
By making some modifications to the GA code it is possible to send data to two Analytics accounts from one web page.
For urchin.js:
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-XXXXXXX-1"; urchinTracker(); _uff = 0; // Reset flag to allow for second account _uacct = "UA-XXXXXXX-2"; </script>
For ga.js:
<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src=’" + gaJsHost + "google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E")); </script> <script type="text/javascript"> var firstTracker = _gat._getTracker("UA-XXXXXXX-1"); firstTracker._initData(); firstTracker._trackPageview(); var secondTracker = _gat._getTracker("UA-XXXXXXX-2"); secondTracker._setDomainName("none"); secondTracker._setAllowLinker(true); secondTracker._initData(); secondTracker._trackPageview(); </script>
Please note: this applies to XHTML Transitional doctype written in utf-8 encoding. You may also download this post as a plain text file for easy copy & pasting.
Enjoy!
27 Responses
Gabe Hoggarth
04|Apr|2008 1Thanks, works great! Just had to fix the ” marks when I copied and pasted.
del.icio.us bookmarks for chrisjroos
14|Apr|2008 2Tracking Data for Multiple Google Analytics Accounts on One Page by SEOjeff…
Blog post detailing the changes required to the Urchin/GA javascript to allow you to record visits in multiple GA accounts. Possibly useful if you have multiple Adwords accounts that you want to link to Analytics accounts….
admin
14|Apr|2008 3Gabe, thanks for the heads-up. I’ve edited the post and have provided a plain txt version.
-Jeff
Anti Wrinkle Cream Guy
12|Jun|2008 4Thanks for the code.
I get an error in IE 7 though…
Says ” ‘_gat’ is undefined”
Tks again
Steve
11|Nov|2008 5Hey thanks for the script,
on ga.js I was getting the same error as ‘Anti Wrinkle Cream Guy’ but I removed your top script and replaced it with the one supplied by google. This worked which lead me to notice that when I had pasted the script the apostrophes were weird slanted ones. Something people should look out for as I think this will break the script.
Thanks Again,
Steve
DigitalPixie
17|Dec|2008 6Does anyone know why Google recommends against tracking a page to multiple accounts? See the last line on this page: http://code.google.com/apis/analytics/docs/gaConceptsCookies.html#cookiesSet
thanks for the post!
Anders
17|Apr|2009 7“Does anyone know why Google recommends against tracking a page to multiple accounts?”
Some browsers have a 20 cookie/domain policy, with two google accounts (with a maximum of 12 cookies together) – this would mean big FAIL, if you would have more than 8 cookies of your own on the client for that domain > failing to meet the 20 cookie/domain maximum.
Markus
08|Jun|2009 8One reason the ga.js may not work properly is that just copy and pasting it from the page leaves at least two invalid character spaces between characters in the *”google-analytics.com/ga.js’* and *”https://ssl.”* urls.
e.g. “google -analytics.com/ga.js”
EstebanD
12|Jun|2009 9Jeff thanks for the tip.
I have some newbie questions (Before hand I apologise if I am too literal):
1) You mean multiple accounts OR multiple profiles?
Because based on google’s documentation and assuming XXXXXXX is a constant. The making -1 means profileId =1 and -2 means profileId =2. based on this i assume you can use this trick for multiple profiles. Is this correct?
2) Please help me understand the ProfileId usage.
If I setup the account google will provide the code with the accountId: (“UA-XXXXXXX-1″)
Because my site has multiple branding I create a new profile for brand A that has profileId =YYYYYYYY (8 characters)
On the code when I sense a visitor is on brand A I put the account as follows: (“UA-XXXXXXX-YYYYYYYY”) with the goal of having a profile with strictly the data of brand A.
My concern is. Does the main profile that tracks all data keeps getting data even when using the tracker of profile Brand A??
Thank you very much in advance.
EstebanD
admin
12|Jun|2009 10#1: It doesn’t matter. When I implemented this myself it was to supply data to profiles across 2 accounts.
#2: Each profile collects data independently. You would never want to set your accountid to (UA-XXXXXXXX-YYYYYYY), that will break the code. The reason for multiple profiles under one account is so that you may login to one Analytics account and view stats for multiple websites.
Ryan McDermott
19|Jun|2009 11Seems to work for me, can anyone tell me what the drawback would / could be?
Mart
14|Aug|2009 12Unfortunately I dont think this appears to work for legacy urchin any more.
The results i am receiving between the two account profiles are significantly different.
danny
18|Feb|2010 13Hi guys,
I know i’m doing this wrong and you’ve got a nice solution.
But will the following code i’m using result in any incorrect data?
try {
var pageTracker = _gat._getTracker(“UA-xxxx-1″);
pageTracker._setDomainName(“mydomainname”);
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
pageTracker._trackPageview();
} catch(err) {}
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
try {
var pageTracker = _gat._getTracker(“UA-xxxx-2″);
pageTracker._setDomainName(“none”);
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
} catch(err) {}
ionuke
21|Feb|2010 14Hello,
your code mentions UA-XXXXXXX-1 and UA-XXXXXXX-2 for the two accounts. Is this for a better understanding of the script or should i modify the ending -1 for one of the trackers to -2, as both my account identifiers end with -1.
thanks for the sollution.
admin
21|Feb|2010 15No, those are just example account numbers. You can use any account number in either spot.
admin
21|Feb|2010 16I have no idea. Test it out and let us know.
Vijaya Kumar S
03|Mar|2010 17Hey,
Thanks. It’s works for me… Your article helped me in rite time. Thanks once again.
Andy
15|Apr|2010 18I want to use also 2 different Google Analytics code in one domain.
I believe that the (new) code below is ok, any comments?
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
try {
var pageTracker = _gat._getTracker(“UA-xxxxxx5-1″);
pageTracker._initData();
pageTracker._trackPageview();
var pageTracker2 = _gat._getTracker(“UA-xxxxxx8-1″);
pageTracker2._initData();
pageTracker2._trackPageview();
} catch(err) {}
Kortingen
11|Jun|2010 19I want to combine all my analytics reports (I have +/- 4 accs with 20+ websites) in one analytics report. Is that also possible? Without changing the code then.. but from the inside or with an app.
Loki
02|Jul|2010 20What about the newest code? How to set up multiple code tracking using this one?
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX']);
_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);
})();
Chuck
07|Jul|2010 21I’m a little confused by this. I have a client who has a parent sites and sub-sites. I need to track aggregate data for all sites, aggregate data for the parent sites and then individual tracking for the sub-sites. Initially I thought this might be the way to go, however, I’m not so sure now.
1. Do I need to have 3 separate accounts for the client, parent sites and sub-sites?
2. If so, do I need to track data in the way mentioned above to gather aggregate data on each level?
admin
07|Jul|2010 22Yes, you will need an account (or profile) for each site and you can use this method to accomplish your goal.
NJR
18|Aug|2010 23I would very much appreciate if anyone comes up with a multiple account solution ‘UA-XXXXXX’, ‘UA-YYYYYY’ as Loki are looking for in #20
Terence Hersbach
23|Sep|2010 24Note that since the new tracker Google allows multiple accounts for one single page.
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-1']);
_gaq.push(['_setDomainName', '.domain.com');
_gaq.push(['_trackPageview']);
_gaq.push(['b._setAccount', 'UA-XXXXX-2']);
_gaq.push(['b._trackPageview']);
_gaq.push([‘b._setDomainName’, ‘.domain.com’);
see the manual for more: http://code.google.com/intl/nl/apis/analytics/docs/tracking/asyncUsageGuide.html#MultipleCommands (one Push, multiple commands)
Ashley
20|May|2011 25I am not a coder so I apologize for my lack of knowledge but I need to track multiple accounts on one page AND enable subdomain tracking . Does anyone know how to customize the script with subdomain tracking?
Korting
25|Feb|2012 26Thanks for sharing this info. I was looking for this solution a long time.
Ven Francis
26|Feb|2012 27#25 Ashley,
refer to #24 …it answers your question
also if you go to GA and get your code, you’ll have the option for subdomain tracking….but bear in mind that you’ll need to set what profile the subdomain tracking will be used to….
Leave a reply
Search
My Twitter Feed
Recent Posts
Categories
© 2011 site by Jeffrey Nichols