Adding two Google Analytics accounts to one page

There are times when you would like to track your page visits using Google Analytics and also someone else wants to do the same. We have had this situation where the client wants to have the data in their analytics account and we also would like to track the site using our account. We have also had external SEO companies that want to track the site using their account.

I have found a number of posts, forum entries and blogs that give examples of how this is achieved. Some are longwinded and some just plain wrong. To add tracking for two accounts using the asynchronous code that is placed before the closing head tag google describes doing this in its usage guide. All I have done here is show the full code snippet.

<script type=“text/javascript”>
var _gaq = _gaq || [];
  _gaq.push(
  [‘_setAccount’, ‘UA-XXXXA-X’],
  [‘_trackPageview’],
  [‘b._setAccount’, ‘UA-XXXXB-X’],
  [‘b._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);
})();
</script>

You can just copy the above code and replace UA-XXXXA-X and UA-XXXXB-X with the two account codes that you want to use to track the site.

This article was posted on 06/05/11 in Code, Google, Javascript, Snippets

Try a sample pack of 10 Business Cards for free from MOO

Upload images for the first side of your card. Choose up to 10 different images or designs. We accept high resolution JPEG, PNG, GIF or PDF files. Free standard shipping is included.

comments

add comment

What you have had to say about all this...

how about subsequent numbers of accounts ?

use

[‘c._setAccount’, ‘UA-XXXXC-X’],
  [‘c._trackPageview’]

and so forth ?

- Jan

Nice development. Will try this code.

- barristers

We have a CMS that adds the GA site wide (in the footer) however a supplier has provided GA for us to put on a single page and I only have the content area I can use to add this in the CMS. How do you advise adding the second tracking code?

- Stuart
x
Step 1. : Login details

about you.

Step 2. : Password

your say.

Step 3. : Email details

preferences.


That's the end of this article. I hope you found it useful. If you're enjoyed this article why don't you have a look around the archives, where you can find some more tutorials, tips and general ramblings.