Two or more Google Analytics accounts in one page: How-to
Yes, it is possible.
No, it won’t work with a simple “copy-paste” of the two Google Analytics codes.
Read on, if you want to know the solution which I discovered by experimenting! :-)
* * *
Not a long ago I had to play a lot with GoogleAnalytics. I had also the idea of creating one GoogleAnalytics profile for the whole domain, and separate profiles for some of the sections in the website. Thus the statistics for the whole website and its sections could be tracked in an easier way, like if you were watching the statistics for separate websites, while having at the same time an overall look at the traffic for the whole domain. (I believe there are other means of achieving the same goal with GA, but for me the method above was the easiest to implement.)
So, I’ve created separate profiles in Google Analytics (one profile for the whole website and several others for the site sections), copied the HTML codes from GoogleAnalytics on each page, and waited for the results. 24 hours later I discovered that:
1) The first (for the whole domain) GoogleAnalytics profile works and gathers data (the code for it was always first in the html code in the pages).
2) The GA profiles for each section do not work (they show zero traffic), notwithstanding the fact that Google Analytics claims that the GA code is inserted correctly and works (the code for each site section was always after the general profile code).
The things at this moment looked like this:
<!-- GoogleAnalytics code for the whole domain -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-XXXXXXX-X";
urchinTracker();
</script>
<!-- GoogleAnalytics code for a specific site section -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-XXXXXXX-X";
urchinTracker();
</script>
Copy-pasting the GA codes one after the other didn’t work… After some research and googling around, I’ve found the following solution:
First, insert the HTML code for the first Google Analytics profile. Then add:
_uff = false; // Reset for second account
And after that, add the code for the next profile. If you must have more than 2 profiles in one page, simply repeat.
In my case, when I needed two Google Analytics profiles in one page, the whole code looked like this:
<!-- GoogleAnalytics code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-XXXXXXX-X"; // GoogleAnalytics code for the whole domain
(typeof(urchinTracker) == "function") urchinTracker();
_uff = false; // Reset for second account
_uacct = "UA-XXXXXXX-X"; // GoogleAnalytics code for a specific site section
if (typeof(urchinTracker) == "function") urchinTracker();
</script>
There is also a simplified version of the above code. You might have noticed that I used: if (typeof(urchinTracker) == "function") urchinTracker(); (instead of urchinTracker();) — the reason was because it was recommended to do so (some kinds of ad-blocking software block the loading of the urchin.js file, and then urchinTracker becomes an undefined function in JavaScript and throws an error, so first a check is made, and only if urchinTracker exists, it is called).
…So let’s go back to out shorter version of the above example. Here it is:
<!-- GoogleAnalytics code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-XXXXXXX-X"; // GoogleAnalytics code for the whole domain
urchinTracker();
_uff = 0; // Reset for second account
_uacct = "UA-XXXXXXX-X"; // GoogleAnalytics code for a specific site section
urchinTracker();
</script>
Both (longer and shorter version) do exactly the same thing, just the first variant is believed to be more bulletproof than the second one:)
I’ve tested everything, now all Google Analytics counters work normally, all profiles gather data and now I have detailed statistics for the whole domain and separate GA profiles for the sections in the website.
Hope these short GoogleAnalytics tips & tricks will be useful for other enthusiasts, too, decided to ‘hack’ into the code of GA for their specific needs:)
13 comments | View blog reactions


1 November 13th, 2007 at 23:18
So, I have a little trouble finding answers on how to do this sort of segmenting tracking in GA. Your post was the first I found offering a solution, yet I am still a bit confused. When I set up a second segmented profile all I can do is choose the same domain as the main profile. Then I get the message that the tracking code cannot be detected at the domain home page URL. Well, of course not - because I don’t have it there. I only want it in the area of the website I want to target. I was hoping to place the 2nd tracking code in the specific directory I want to target, but I am confused about this whole concept of a profile - it only seems to pertain to an entire domain. I realize I can set up filters, but I don’t get why all profiles expect the tracking code to be at the site home page.
2 November 14th, 2007 at 12:31
Hi, Scott,
I have some experience with Google Analytics, and if you could better explain to me what do you want to achieve exactly, maybe I could help you! :-)
Actually, GA is very flexible. You can track only a section of a website. You can track simultaneously a section of a website and at the same time gather general statistics from the whole website, by placing code for two GA accounts in one page. You can set up goals. There are a lot of possibilities:)
Google Analytics does not expect the tracking code to be on the home page! GA expects it to be where you want to track something (site, section of a site, subdomain, etc.).
Also,
No, it doesn’t.
And don’t mix ACCOUNTS (top right in GA interface) and WEBSITE PROFILES (top left in GA interface).
OK, let me give you a small example — I hope this will help.
Let’s say you are a proud owner of benjerry.com ;-)
Let’s say that you want to set up two Google Analytics profiles for your website. The first will gather data from all of your sections/pages (entire domain), and the second will target only the Scoop Shops section.
How do you proceed in this case?
I would create first an account for http://www.benjerry.com. Fill the website url, timezone, and the account name (usually the account name is the same as the site URL, so it’ll be easier for you to know what’s what).
I think, when you create your first account, simultaneously a new default website profile is created, and all you must do at this moment, is copy the code for it in all of your pages, and the tracking will start soon after that.
Now, second step, you want to track separately a site section, using a new website profile. How to do it?
Click on ADD WEBSITE PROFILE (currently, located in the bottom left corner in the GA interface), then select on the next page the option “Add a Profile for a new domain”, then fill the URL of your site section (in this imaginary case, Scoop Shops), select time zone, next, copy HTML code, and you’re done! :-)
(Later, you can rename your website profile(s) to anything you like.)
Now, you have ONE Google Analytics account (www.benjerry.com) and TWO profiles:
1. http://www.benjerry.com
2. http://www.benjerry.com/scoop_shops/
Include the code for http://www.benjerry.com in all of your pages, and (following the instructions on how to include the code for two GA profiles in one page) include the code for http://www.benjerry.com and http://www.benjerry.com/scoop_shops/ in the pages only in the site section “Scoop Shops”.
Soon after that, you’ll start to gather stats for your whole domain, and also for only the site section you wanted to track separately. You may track as many sections as you want, if you create more website profiles, and carefully include the HTML code in all of them.
Hope this helps, cheers:)
PS I do not pretend that this is the best/most correct way of doing things, but it works — at least, the way I intended this to work. A friend of mine mentioned recently that probably Goals can be used instead of this 2+ profiles way of tracking data, but for me this is easier:)
3 November 19th, 2007 at 21:15
I think the problem was that I was trying to use an external .js file for the tracking code and for some reason Google did not like that. When I switched it back to putting the tracking code directly into a script block within the page, then it worked. Thanks for your help!
4 November 19th, 2007 at 21:24
I’m glad your code now works and that I was (hopefully) of some help! :-)
Cheers!
5 November 20th, 2007 at 05:09
Thank you so much for the tip.
It worked fine right from the beginning. You’re the man!
6 November 22nd, 2007 at 12:37
@Antonel:
Thanks, I’m glad that it worked for you, too! :-)
7 December 13th, 2007 at 09:41
Hi, and thanks for your work on this. Have you seen that Google Analytics has a new JavaScript snippet to embed? It has new variable, “gaJsHost”, as an easily recognised difference. You’ll need to do a new set of tests and fixes, I suspect!
Cheers, JezC.
8 December 13th, 2007 at 11:38
@Jeremy:
Thanks for the update! I checked, yes, now you can embed two kind of Google Analytics codes - old one or new one… I suspect for the new one, to make it work, a change will be needed. I’ll search for it, and will post an update! Thanks again! :-)
9 February 8th, 2008 at 18:12
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"));
var firstTracker = _gat._getTracker("UA-12345-1");firstTracker._initData();
firstTracker._trackPageview();
var secondTracker = _gat._getTracker("UA-67890-1");
secondTracker._initData();
secondTracker._trackPageview();
10 February 14th, 2008 at 15:42
@tariq:
Thanks, is this the new variant for the code of two Google Analytic in one page?…
11 March 26th, 2008 at 19:52
yep. it’s also included in the google analytics manual: http://www.google.com/analytics/InstallingGATrackingCode.pdf
12 March 26th, 2008 at 22:25
@tariq:
Thanks a lot! I’ll read the manual, too! :-)
13 June 2nd, 2008 at 22:20
OK for the new GA tracker here’s what you could do.
for example this is our normal code.
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"));
var pageTracker = _gat._getTracker("UA-111111-1");
pageTracker._initData();
pageTracker._trackPageview();
then we could have two like this
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"));
var 1stTracker = _gat._getTracker("UA-111111-1");
var 2ndTracker = _gat._getTracker("UA-222222-2");
1stTracker._initData();
2ndTracker._initData();
1stTracker._trackPageview();
2ndTracker._trackPageview();