问题
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