Multiple Google Analytics Codes on the Same Page and Cross Domain Tracking

Published on Author JF3 Comments

This covers only Google Analytics universal code (analytics.js) and not the old GA code (ga.js). It does not cover GA dashboard or reporting functions.

There are examples for tracking using a single set of GA code for your site, and then when you are using it for a another tracking ID (UA) on the same page.

This also covers the very basic configuration for tracking your website across multiple sub-domains (not multiple TLDs), e.g. www.mysite.com and shopping.mysite.com.

It’s really simple to do:

Step1: Get your tracking code and paste it onto both your WWW site and your SHOPPING site. That’s it. You will need to set up some views and filters in your GA account.

<script>
(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);
ga(‘create’, ‘UA-XXXXXXXX-1′, ‘auto’);
ga(‘send’, ‘pageview’);
</script>

Combining two UA codes on the same page:

Let’s say you have UA-XXXXXXXX-1 and your marketing company has UA-YYYYYYY-1. They want to be able to track a landing page without having access to your GA account.

The trick here is that the code above represents a single named variable called “t0”. If you copy the same code on the page and simply change the UA number it will not work and only fire the first instance of the code.

Changing this is simple.

Make two copies of the exact same code. Then, make a new name for the second instance. I highlighted it below, but here is what was added:

, ‘someTracker’
someTracker.

The name must contain “tracker”, but what precedes it does not matter. It could be called awesomeTracker or sometracker (I would recommend using case to make it easier to read). Just make sure the names precisely the same for each. It doesn’t matter which is first.

<script>
(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);
ga(‘create’, ‘UA-YYYYYYYY-1′, ‘auto’);
ga(‘send’, ‘pageview’);
</script>
</head>

<script>
(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);
ga(‘create’, ‘UA-XXXXXXXX-1′, ‘auto’, ‘someTracker’);
ga(‘someTracker.send’, ‘pageview’);
</script>

Question: can I make the UA-YYYYYYYY-1 code a named tracker? Yes. Either one can be named, or both. Again, the order of the code does not matter either.

That’s it!

References:

https://support.google.com/analytics/answer/1032400
I
t would be nice on the above link if it said something like: “Warning! You need to use trackers in order to distinguish between different UAs”, but it doesn’t.

https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers

More detailed information (be careful – there is some discussion of using ga.js):

This one is definitely advanced moves and probably is the best way to set it up in your GA account:
https://www.optimizesmart.com/using-multiple-trackers-for-cross-domain-tracking-in-universal-analytics/

This one has a ton of other information:
http://www.blastam.com/blog/index.php/2014/11/2-steps-cross-domain-tracking-universal-analytics

 

 

3 Responses to Multiple Google Analytics Codes on the Same Page and Cross Domain Tracking

Leave a Reply to D Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.