Reputation: 17
I´m confused with Google Tag Manager and gtag.js.
Can I use gtag.js tracking GA and use GTM tracking GA4? I tried adding: `
<script async src="https://www.googletagmanager.com/gtag/js?id=Property ID GA"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'Property ID GA');
</script>
<!-- Google Tag Manager -->
<script>
window.customDataLayer = window.customDataLayer || [];
</script>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','customDataLayer','google tag manager');</script>
<!-- End Google Tag Manager -->
`
GA4 didn't track event page_view. What is wrong?
Upvotes: 1
Views: 2540
Reputation: 479
If your site already has GTM, the best way to add GA4 might be just create the GA4 Configuration Tag, update the measurement id from the GA4 property's data stream's measurement id, then create a trigger to all pages, once you publish your GTM changes, your site will start tracking GA4 default automatic events.
Once you create the GA4 Configuration Tag via GTM, your site will include gtag.js from GTM.
Upvotes: 1
Reputation: 8081
You have to read your ga4 installation instructions carefully: See those two options? You're not meant to mix them. It's either the frist option if you don't want to use GTM for some reason. Or the second option.
Remove your gtag related code. GTM does everything you need as you go. Just make a GA4 tag in GTM and it will load all dependencies seamlessly.
Make a separate quesion if your GTM doesn't do what you expect after you've cleaned up code, made tags, triggers and filled the measurement ids as needed.
Upvotes: 0