andrezimpel / gatsby-plugin-gdpr-cookies

Gatsby plugin to add Google Analytics (V4 is supported), Google Tag Manager, Facebook Pixel, TikTok Pixel and Hotjar in a GDPR form to your site.
Apache License 2.0
101 stars 69 forks source link

G tag code commented out? #70

Open arnars opened 3 years ago

arnars commented 3 years ago

Thanks for this plugin. Made my life a lot easier.

I was looking at the GTAG code and I was wondering why the main methods were commented out. Is that on purpose or is it by mistake? It might be me that are misunderstanding how things works, but the other services seems to have the code active.

Best, Anders

kb1995 commented 2 years ago

@arnars Were you able to find a solution? Did you fork the repo or used another approach?

gabriardi commented 2 years ago

I also don't understand why the code is commented out. @andrezimpel ? In fact i forked the repo to include the commented code and it works for me.

kb1995 commented 2 years ago

@gabriardi I ended up doing the same thing (forked your fork) and it looks like it working fine now.

I wonder what's the reasoning behind the commenting out of a working code

andrezimpel commented 2 years ago

@arnars & @gabriardi When I was testing the code I ended up receiving duplicate events because somehow gtag was ready to fire them trough the initialization. If both of you got it working by including it again I would be really happy if you could create a pull request. I'd merge it asap.

Thank you!

gonzalo-massa commented 2 years ago

Hi, I was affected by this issue too: Gatsby wasn't firing the routeChangeEvent event because all the code is commented, but by doing a little research, I think I know why this was commented and why @andrezimpel was receiving duplicate events.

There is a new version of Google Analytics (GA4) that has an Enhanced measurement for Page views, and what it does is to detect changes on the browser's history object. Having this option enabled (it's on by default) tracks every change on the browser history (every change on the address bar) as a page_view event.

Meaning if you have GA4 with enhanced measurement, you don't need the routeChangeEvent event nor the custom event trigger on GTM, you just have to set "All pages" as a trigger for the GA4 tag.

Having said that, there will be people without enhanced measurement, or with an old version of GA, so I think it would be convenient if the user has the option to control if the plugin should fire this event or not, maybe configuring routeChangeEvent to an empty string?

andrezimpel commented 2 years ago

@gonzalo-massa yea man. that's been the reason it was commented you but as it turns out a lot of people use it differently that's why I merged #72. Any idea on how to fix that tho?

gonzalo-massa commented 2 years ago

@gonzalo-massa yea man. that's been the reason it was commented you but as it turns out a lot of people use it differently that's why I merged #72. Any idea on how to fix that tho?

My suggestion is to give the developer the control over this, if they want to fire the routeChangeEvent or not, maybe setting routeChangeEvent: false (or an empty string to maintain consistency with the rest of the options). Then the plugin will check that before firing the event.

I could try to submit a PR for that, but I don't know the code very well.