Ali_Hassan is experiencing issues with using Google Optimize in a plugin initializer JS. Specifically, they are getting an error saying gtag is not a function when trying to call the gtag function.
hawm suggested that the gtag function needs to be defined or imported, even if it’s enabled in Discourse settings. Ali_Hassan assumed it was defined automatically, but hawm clarified that it needs to be imported or defined manually.
hawm further explained that gtag is defined at runtime under the window object, so it can be called using window.gtag. Ali_Hassan was able to resolve the issue by defining the function on startup, but encountered another problem where the GA tracking code still includes analytics.js even though they are using v4_gtag, potentially causing duplicate events to be sent.
Hey everyone, i have a problem when i enable v4_gtag for analytics instead of v3_analytics, i need GTag for Google Optimize but i keep getting an error that gtag is not a function.
i am calling the gtag function inside a plugin js file, can someone provide more context on why this isn’t working ?
here is a sample of how i’ve written the code, Thank you in advance
i was able to define the function on startup and i think discourse is gonna redefine it when it loads which is currently working ok, but i have another problem now, when i add my ga tracking code in ga universal tracking code it still includes analytics.js even though i am using v4_gtag, is this an expected behavior ? won’t. that make an issue of sending duplicate events ?