Adding multiple Google Analytics tracking codes to webpage

by Hussain
Share Your Insight on:

aka Tracking Google Analytics to Multiple Web Properties from a single page

So let’s say you want to send your sites analytics data to two google analytics web properties. To do this you would need to place the tracking code from the two properties on every page of the site. But you can’t do it by just pasting both the codes as it is, instead we need to modify one of them to keep collecting data correctly.

To do this copy the tracking code from both web properties.

To get the tracking code go into Web Property > Click Admin > Click Tracking Info tab

First Web Property

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-FIRST-WEBPROPERTY']);
_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);
})();

 

Do the same thing for Second Web Property

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-SECOND-WEBPROPERTY']);
_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);
})();

Now if you copy pasted both of the codes as it is into your pages your tracking would be incorrect as both codes would overwrite analytics cookie that GA uses to track the visitor giving you wrong measurement in both web properties. [I have avoided technical reasoning but end result would be you would have 0 or close to zero bounce rates and a lot of self referrals / direct visits]

To get both of them to work together we need to modify one of the codes.
Let’s change the second code by adding a prefix to all the _gaq.push calls sent to analytics.

This prefix could be anything really. For brevity let’s use b the prefix needs a . (dot) after it for separation. So it becomes b.

Updating our second script we get

<script type=”text/javascript”>
var _gaq = _gaq || [];
_gaq.push(['b._setAccount', 'UA-SECOND-WEBPROPERTY']);
_gaq.push(['b._trackPageview']);

</script>

By modifying the code we are causing it to create a separate set of GA cookies for tracking data to second web property, hence overcoming the overwriting issue we were facing previously.

We need to add prefix ALL calls in tracking script as well as any other event tracking or cross domain calls. Also note every action that you want to track to both properties would need to have tracking from both sets of properties.

For example if you wanted to track click on a link.

<a href="/hello"> Click Me

To add tracking for both properties we would

<a href=”/hello” onclick=”_gaq.push([‘_trackEvent’, ‘Hello Click’, ‘Click’, ‘link’]); _gaq.push([‘b._trackEvent’, ‘Hello Click’, ‘Click’, ‘link’]);”> Hello Click Me</a>

 

Cleaning up:

We can shorten the second script to just this as rest of the code already exists in first script.


<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['b._setAccount', 'UA-SECOND-WEBPROPERTY']);
_gaq.push(['b._trackPageview']);
</script>

In some cases you cant combine the script so you would just place the above script on the pages with the first tracking script.

Ideally you should combine the first and second script in which case it would be written as:


<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-FIRST-WEBPROPERTY']);
_gaq.push(['_trackPageview']);
_gaq.push(['b._setAccount', 'UA-SECOND-WEBPROPERTY']);
_gaq.push(['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>

The following two tabs change content below.

Hussain

Analytics Consultant

Hussain co-founded MarketLytics to turn his passion for web analytics into his dream job. He is a google analytics qualified individual and has been working with web analytics for close to 3 years. He originally got curious about ppc marketing as a 16 year old trying to drum up sales for his dad's travel business.
He currently spends his time simplifying analytics implementations, consulting clients on becoming data driven.
Hussain has an unending fascination with google analytics and has been caught a couple of times reading its api docs as bedtime stories. Over the last year he has also started to give some love to kissmetrics and mixpanel to cover GA's flaws.
In his free time he likes to work his way back through american and british television series (was down to 1980's on last call) and complete his undergrad in marketing.

Tags: , ,
  • Tony Griego

    This is exactly what I was looking for! Thank you for taking the time to write this up.