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

Add Google Consent Mode #342

Open
afanjul opened this issue Dec 10, 2020 · 7 comments
Open

Add Google Consent Mode #342

afanjul opened this issue Dec 10, 2020 · 7 comments

Comments

@afanjul
Copy link

afanjul commented Dec 10, 2020

With the release of Google Consent Mode, we could still be tracking users (for Google Ads, Google Analytics, etc.) even without cookies (so complying with the law). Google has launched this mode with the API here: https://developers.google.com/gtagjs/devguide/consent

You could consider to add this mode to the Klaro library.

@jmalinens
Copy link

+1

@adewes
Copy link
Contributor

adewes commented Dec 11, 2020

Hey @afanjul and @jmalinens, we already support Google Consent Mode (GCM) and advanced consent modes for Google Tag Manager (GTM), currently this is only available in our hosted solution as it's enabled via the Klaro service catalog. To be honest I'm still a bit hesitant on whether we should integrate the service catalog (which is currently still open-source) into the OS version, as it takes a lot of effort writing and maintaining it, so we might have to keep that in the paid version only.

However, we have a tutorial that shows how to integrate Klaro with GCM and GTM, which also works for the open-source vesion:

https://kiprotect.com/docs/klaro/tutorials/google_tag_manager

It requires Klaro >= 0.7.10 as it relies on the new callback functions. Let me know what you think.

@afanjul
Copy link
Author

afanjul commented Dec 15, 2020

Thanks @adewes, I did review the code for google_tag_manager, but regarding the Google Consent Mode I think that there is something wrong, or maybe something that I missing, because the Google Consent Mode allows you to precisely always load the google gtag/gtm script code BUT at the same time allows you to configure the consent mode (as long as you set your consent mode before the gtm script code) to avoid use cookies.

The problem with Klaro is that as soon as you declined the Google Service, the gtag/gtm code won't be loaded so you won't use the new Google mode consent (in deed you won't load any google gtag/gtm base code at all).

Do I missing something?

BTW: I think there is a typo using opts.vars.googleAnalyticsName var name instead of opts.vars.googleAnalytics.

@adewes
Copy link
Contributor

adewes commented Dec 15, 2020

Hi @afanjul, thanks for the reply! So there are several ways to go about this:

  • You can mark Google Tag Manager as a required service. In that case, if the user declines consent you would still load the GTM tag and Klaro would make the appropriate calls to enable consent mode for Google Analytics.
  • You can mark GTM as optional, in that case it won't be loaded if the user declines.

Personally I'd prefer to disable all tracking if the user declines consent but I know that the consent mode is specifically designed to be used in such a case (though I don't fully agree with Googles' reasoning), so it's up to you as a publisher on how you want to use this.

So, in a nutshell: If you want to always load GTM, simply change the purposes to ['functional'] and set required: true to ensure it will always be loaded. You can also manage the services configured in GTM via Klaro, for every service that the user accepted Klaro will send a klaro-[service-name]-accepted event to GTM that you can use as a event to trigger the loading of the given tag. Let me know if that solves your problem. Thanks for pointing out the typo btw, I justed fixed it in my branch.

@afanjul
Copy link
Author

afanjul commented Dec 15, 2020

There is a third option that I'm trying to figure out now (and testing it for Google Analytics & Ads global gtag, not GTM by now) that is not to modify the original script (ie. let the type attribute as javascript, etc.) and just add the service with the callback functions.

So the base code would be like this:

<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
<!-- Global site tag (gtag.js)-->
window.dataLayer = window.dataLayer || [];
function gtag() {
    dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('consent', 'default', {
    'ad_storage': 'denied',
    'analytics_storage': 'denied',
    'wait_for_update': 750
});
gtag('set', 'ads_data_redaction', false) //optional?;
gtag('config', 'GA_MEASUREMENT_ID');
</script>

And the GoogleAnalytics (and Ads) services would be like this:

onAccept = "
if (opts.consents['GoogleAnalyticsService'] == true) {
    gtag('consent', 'update', {
        'analytics_storage': 'granted'
    })
}";
onDecline = "
if (opts.consents['GoogleAnalyticsService'] == false) {
    gtag('consent', 'update', {
        'analytics_storage': 'denied'
    })
}
";

You don't need the onInit callback because the init code it is already added before the base script loading, and moreover it won't ever work because onAccept (thrown through a deferred script) always will be thrown AFTER the async google gtag code in head.

@adewes
Copy link
Contributor

adewes commented Dec 15, 2020

Great idea, I didn't think of that! If you configure GTM correctly this is an option as well, just make sure it won't load additional scripts until the user grants consent.

That said I think it's always best to not load any third-party scripts before the user has given consent.

@kocjs
Copy link

kocjs commented Feb 26, 2024

@afanjul I am trying to implement google consent v2, could you please share the solutions you ended up with?

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