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
100 stars 69 forks source link

GA user source always "Direct" #19

Closed marco910 closed 3 years ago

marco910 commented 4 years ago

Hi,

I'm using this plugin with Google Analytics on my Gatsby-website. When a user entries the site for the first time the cookiebanner while be shown and the user have to choose whether he wants to accept or reject cookies. After the user selected one option the banner disapears and the page reloads (to call the onClientEntry hook) and there's my problem: Now I only see in Google Analytics, that the source of the user is "Direct" and not "Organic" or something else.

How can I fix this to get the "real source" of the users?

andrezimpel commented 3 years ago

Hi @marco910 do you have an example for this?

marco910 commented 3 years ago

Hi @andrezimpel !

Unfortunately, it is difficult to give a concrete example of this. But I can describe the issue more in detail.

On my Gatsby site I've integrated the gdpr-cookie-plugin in combination with my cookie-banner. This appears when the users visit the website. If the users accepts cookies, the GA-cookie will be set. Otherwise, only the cookie-banner diapers. But now I have to reload the page because GA is only initialized onClientEntry and I see no other way to do that. And I guess this is the point where the issue comes in. In most cases I only see in GA a "Direct" source. If the user has already consented in the past, I see the source, but not, if it's the first visit for 30 days.

Is there a way to initialize GA without reloading the page when the user consents?

andrezimpel commented 3 years ago

@marco910 The plugin does in fact try to initialize GA on every route update if the cookie is set to true (since v1.0.10). I'm still working on how to expose the initialization to the cookie banner so also just visiting one page and accepting the banner without a route update will trigger the event to be sent.

marco910 commented 3 years ago

@andrezimpel I now have updated the plugin on my Gatsby site to v1.0.10 (it was still on v1.0.7) and removed the page reload when clicking on consent on the cookie banner. Now I will check if all works in production and the source is displayed in GA and give you feedback in a few days.

marco910 commented 3 years ago

Hello @andrezimpel As mentioned above I have removed the page reload when clicking a button on the cookie banner. After that the GA tracking didn't work. That's why I put it back in. Do you have an idea why it isn't working?

zamson commented 3 years ago

@marco910 The plugin does in fact try to initialize GA on every route update if the cookie is set to true (since v1.0.10). I'm still working on how to expose the initialization to the cookie banner so also just visiting one page and accepting the banner without a route update will trigger the event to be sent.

Any updates on this? Would be great if the tracking was initialised without a page refresh

marco910 commented 3 years ago

@zamson @andrezimpel I've set up a new Gatsby site with the plugin installed and a new Google Analytics 4 property to test the behavior. In development and production on Vercel I have tested it with a button to set the GDPR cookie - that worked. After a route update the GA cookies were set, but I didn't saw a user in the real time view of GA.

andrezimpel commented 3 years ago

Hi @marco910 and @zamson

I just updated the plugin to expose a initializeAndTrack(location) method. Using this after setting the cookies should start tracking the user immediately without having to reload.

https://github.com/andrezimpel/gatsby-plugin-gdpr-cookies#initialize-and-track