Search code examples
javascriptgoogle-analyticsgoogle-tag-managergtag.js

Why does gtag.js specify the same ID twice in the installation code?


The GA4 gtag.js installation code is as follows:

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

Why was G-ZQA9CN2Q3Z mentioned twice?

Is it necessary to load the <script /> tag twice if I want to install two ga4 at the same time?


Solution

  • I believe the one in the URL helps speed things up a bit by loading the specified IDs code initially. So specify your most important ID in the URL and then config all your IDs.

    If you check the network, you will see there is a request per ID.