Apply 2 Google Analytics tracking IDs

0

I need to apply 2 Google Analytics tracking IDs on a site. I already generated the first ID. I was reading, that if I just put one above the other, it will not work. What to do?

<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-00000000-00', 'auto');
  ga('send', 'pageview');

</script>
    
asked by anonymous 06.07.2017 / 20:13

1 answer

3

According to Google support you can usually use only not must repeat the instance of the ga.js file, this can cause conflicts. You can also give a name for each ID and use it this way:

<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-00000000-0', 'auto');
ga('create', 'UA-00000000-1', 'auto', 'xyz');
ga('send', 'pageview');
ga('xyz.send', 'pageview');
</script>

Notice that xyz is the name assigned to the second ID instance, so it uses the send function differently on the second call.

    
06.07.2017 / 20:34