Adding multiple Google Analytics tracking codes to webpage

Let's start

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' : 'https://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' : 'https://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' : 'https://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script>

Don't miss out when new resources launch

Our customer analytics experts share wisdom only once a month

Share now
We are customer-analytics consultancy that transforms messy data into actionable insights that will help you grow your company and make better data-backed decisions.