Skip to content
Advertisement

Multiple Google Analytics gtag tracking IDs on same page

I have two property tracking IDs in my Google Analytics account for the same website. I basically want to have the same set of data inserted into both properties/views. Currently, this is how I have it set up on the site:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXX-1');
  gtag('config', 'UA-XXXXXXXX-3');
</script>

However, I have come to realize this is not actually working correctly. I believe the proper solution to this is this (two sets of script tags, one for each property), but I am not 100% sure:

<!-- Global site tag (gtag.js) 1 - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXX-1');
</script>

<!-- Global site tag (gtag.js) 2 - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-3"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXX-3');
</script>

Advertisement

Answer

After testing the solution in my question, I can confirm that is the proper way to solve this:

<!-- Global site tag (gtag.js) 1 - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXX-1');
</script>

<!-- Global site tag (gtag.js) 2 - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-3"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXX-3');
</script>
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement