-
Improving Integration of gtag and gtm with partytown in a React Application
Hey there! I’m trying to get a grip on using google analytics, gtag and gtm scripts and it’s proving to be a real head-scratcher. Let me walk you through what I’ve done so far.
I’ve got this partytown snippet generated in webpack, with some debug utility and fields to forward certain types of data. I also have a bit where I’m setting up proxy scripts for ‘www.google-analytics.com’ and ‘www.googletagmanager.com’.
Then, I’m plopping this snippet into my index.html file with a simple script tag.
My app calls for 3 scripts, and that’s where I’m running into trouble. For the google analytics script, I have some code that sets up a window.dataLayer and some function definitions for window.gtag and window.ga. I load the script via another script tag with async, defer, and fetchpriority attributes.
In one of my react components, I initialize google analytics and it seems to work like a charm – I can see the UA4 requests popping up in the network tab. But when it comes to loading the gtag and gtm scripts, that’s where the wheels come off. I’m trying to load them in the same way but none of the events are being sent for gtag and GTM.
It’s crucial for me to have ga, gtag, and gtm all 3 working in harmony. I’ve hit a roadblock and I could really use your insight to figure this out.
Despite checking out the official partytown docs and many online blogs, I’m still stuck. Do you have any thoughts on what might be going wrong here?
Log in to reply.