How to Add Multiple Google Analytics Tracking Codes

How to Add Multiple Google Analytics Tracking Codes

You can add multiple google analytics tracking codes to the same page. In order to make sure that both are unique and track their respective events you just need to make little modifications to your code. Your primary code will look like this:

<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','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-11111111-1', 'auto');
  ga('send', 'pageview');

</script>

 

It is a standard code provided by Google. In order to add additional code to your site you need to add tracker name to your code. For that,  you will need to modify last few lines of your code.

<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','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-11111111-1', 'auto');

 ga('create', 'UA-22222222-2', 'auto', 'myTracker');


 ga('send', 'pageview');

   ga('myTracker.send', 'pageview');

</script>


Code should be added with in all Google Analytics tracking codes on site. For example, for event tracking our code will be as follows:

/primary tracking code for acc UA-11111111-1
ga('send', 'event', 'Videos', 'play', 'Fall Campaign');

//secondary tracking code for acc UA-22222222-2
ga('myTracker.send', 'event', 'Videos', 'play', 'Fall Campaign');



Note: Adding tracker name may result in loss of tracking from plugins present on site that automatically sends data to your GA account by locating code on site. For example, Justuno marketing plugin sends data directly to GA by locating GA code on site and then pushing event code within it. However, it fails if you have modified your tracker name.

If this isn't helpful click here to read more.