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:)

23 thoughts on “Two or more Google Analytics accounts in one page: How-to

  1. 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. 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! :-)

    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.

    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,

    …I am confused about this whole concept of a profile – it only seems to pertain to an entire domain.

    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. 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. 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.

  5. @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! :-)

  6. 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();

  7. 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();

  8. i am still slightly at a loss why going through this trouble if you can easily setup a second profile within the same account and use filters such as tracking traffic only for a sub domain for example. what am i missing here?

  9. This is an excellent post. To reply to David: In my Company, we have several verticals, each acting as a separate company with it’s own website. My predecessors created profiles for each website and not a separate account.

    In my scenario we want to link our PPC data with Analytics data. However since there is a one to one relationship between our PPC account and Analytics account, we can only get PPC data for one of our websites (profile).

    We are currently in the process of revamping one of our websites and the the method mentioned above allows me to keep already existing data for the website but allows me to have greater flexibility with the new account (ie: adding PPC data).

  10. I am glad I found this. I am a developer and want to track my clients and I also wanted them to be able to have their own account with google. Thanks!

  11. Thanks for posting this – exactly what I needed. My two profiles were showing radically different data for the same site, so I will try this solution.

  12. @Nora Brown:

    Google have since upgraded their code a few times. This method will work fine with a bit older version of the GoogleAnalytics code… So, yes, this will work, but make sure you’re using the older version of the code! :)

  13. Hi,

    thanks for sharing your experience. I need to do a similar thing. I need to put 2 or may be 3 google analytics account in one domain.

    How can I do this with the current google analytics code? As far as I understood, the code you pasted above is “old”.

    Can you please help me finding an updated code? thanks

    Alessandro

  14. @Alessandro:

    There were some suggestions in the comments above, as to how can be used newer versions of GA code. My solution, indeed, is for the older versions of the Google Analytics code, and I never looked into updating it. I think modern Google Analytics code can be used in many ways and perhaps you can try to use two versions of the code inside one page, more easily…

    Sorry that I can’t help more.

Leave a Reply

Your email address will not be published. Required fields are marked *