How To Track Website With Multiple Google Analytisc Accounts
Posted by in Tips And Tricks September 13, 2011 Leave a comment

I work for a world wide digital agency and I build a lot of websites or campaigns for my clients. I always create a new Google Analytics account for every website/campaign which is used internally by my company to generate traffic (visitors, pageview, etc) and goals report.

As you may know, setting up a Google Analytics tracking code on your website is pretty easy as you just need to copy & paste the code from Google and place it somewhere (above </body> tag) of your website. However, if client also want to track their site by themselves, the process is a little bit complex because you have to track in parallels. It means you need to setup multiple Google Analytics tracking codes on your website.

Below is a piece of code I usually use to track 2 GA tracking codes in my web pages:

<script type="text/javascript"> 
 
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXX-Y']);
  _gaq.push(['_trackPageview']);
 
  _gaq.push(['_setAccount', 'UA-XXXXXXXX-YY']);
  _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);
  })();
 
</script>

And a function use to track events or custom function calling within Flash or Ajax:

<script type="text/javascript"> 
 
	function trackGAPageView(s)
	{
		_gaq.push(['_setAccount', 'UA-XXXXXX-Y']);
		_gaq.push(['_trackPageview', "/" + s]);
 
		_gaq.push(['_setAccount', 'UA-XXXXXXXX-YY']);
		_gaq.push(['_trackPageview', "/" + s]);
	}
 
</script>

Note: please update UA-XXXXXX-Y and UA-XXXXXXXX-YY with your real Google Analytics tracking code.

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com