Script to combine universal analytics and GA4?

╄→尐↘猪︶ㄣ 提交于 2021-01-05 11:25:30

问题


I would like to combine universal analytics and GA4 on my new website.

The documentation for GA4 mentions this tracking code:

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

The documentation for UA mentions this tracking code:

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

The 2 'config' calls look logical to me, but apparently the script is also requested with an 'id' query parameter. How do i properly combine UA and GA4? This is how i have it running now:

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

回答1:


Documentation said to combine these in this way (with UA-XXXXXX-13 in querystring not G-XXXXXXXXXX):

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

  gtag('config', 'UA-XXXXXX-13');
  gtag('config', 'G-XXXXXXXXXX');
</script>

https://developers.google.com/analytics/devguides/collection/ga4/basic-tag?technology=gtagjs



来源:https://stackoverflow.com/questions/64864817/script-to-combine-universal-analytics-and-ga4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!