Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set and update consent mode according to google official docs on CMP with react-ga4? #57

Open
Andrea-Dispe opened this issue Oct 16, 2023 · 4 comments

Comments

@Andrea-Dispe
Copy link

Not a real issue but I am trying to figure out how to set and update google CMP (Consent Management Platform) with react-ga4.
According to the official google doc official google doc on consent mode this script:

gtag('consent', 'default', {
  'ad_storage': 'denied',
  'analytics_storage': 'denied'
}); 

will be added to the head and then the consent mode banner will have to update it with this:

gtag('consent', 'update', {
  'ad_storage': 'granted'
});

How to achieve this just by using react-ga4?

@ReyasHey
Copy link

ReyasHey commented Nov 11, 2023

I started using the library recently and ran into the same roadblock

I'm assuming these settings would go in the "gtagOptions" when initializing at least

ReactGA.initialize([
  {
    trackingId: "your GA measurement id",
    gtagOptions: {...}, // <-----
  }
]);

Sadly fiddling with its "any" type doesn't sound very appealing to me

So I've resorted to manually removing the cookies on consent denied by the user kinda like this article from dev.to says
As well as initializing without an ID when consent is denied, then initializing correctly when consent is granted

Would be awesome if this could be done directly with the library or is on the roadmap!

@ouroboroscoding
Copy link

@ReyasHey that example doesn't really accomplish the goal though, it's a very strict on or off. My understanding of the google consent policy is we can still use analytics even if the user denies access, it's just that google will not store cookies, and will strip out all the user identifying information. We really need a way to enable/disable this in react-ga4 without having to completely turn off analytics.

@PaloSP
Copy link

PaloSP commented Jan 27, 2024

I think we can use this before initialization:

 ReactGA.gtag("consent", "default", {
    ad_storage: "denied",
    ad_user_data: "denied",
    ad_personalization: "denied",
    analytics_storage: "denied",
  });

and later

 ReactGA.gtag("consent", "update", {
      analytics_storage: "granted",
    });

@ouroboroscoding
Copy link

@PaloSP that worked, thanks for the info!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants