Open modelesque opened 3 years ago
I stumbled upon the same problem when trying to implement this. I could not get the code to work, but I did not bother to find a fix because I did not like this approach anyways. It seemed way to complicated for such a simple problem: If users consent to a service, send an event to Google Tag Manager.
Here is the implementation I settled with:
function announceConsent (consentGiven, service) {
const name = service.name
const consentGivenPart = consentGiven ? 'accepted' : 'rejected'
sendEvent(`consent.${consentGivenPart}.${name}`)
}
function sendEvent (name, data = {}) {
window.dataLayer.push({
'event': name,
...data,
})
}
window.klaroConfig = {
version: 1,
...
services: [
{
name: 'googleAnalytics',
title: 'Google Analytics',
purposes: ['analytics'],
cookies: [
/^_ga(_.*)?/
],
callback: announceConsent,
},
],
}
After that, you just need to add a trigger in Google Tag Manager that listens for the consent.googleAnalytics.accepted
event and hook it up to the configuration for Google Analytics.
I also want to mention that most of the time, you really do not need Google Tag Manager. You can just as easily hook up Google Analytics via Klaro directly:
window.klaroConfig = {
version: 1,
...
services: [
{
name: 'googleAnalytics',
title: 'Google Analytics',
purposes: ['analytics'],
cookies: [
/^_ga(_.*)?/
],
},
],
}
In your HTML, just do:
<script
type="text/plain"
data-type=""
data-name="googleAnalytics"
data-src="https://www.googletagmanager.com/gtag/js?id=<MEASUREMENT_ID>></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<MEASUREMENT_ID>');
</script>
This approach of importing Google Analytics directly is documented here.
Still works in 2023 – thanks @fjahn
Attention: One thing has to be adjusted, if someone uses the Google Tag Manager implementation from @fjahn.
Either use consent.accepted.googleAnalytics
(instead of consent.googleAnalytics.accepted
) in Google Tag Manager or change the implementation of the announceConsent
function so it produces the correct name:
- sendEvent(`consent.${consentGivenPart}.${name}`)
+ sendEvent(`consent.${name}.${consentGivenPart}`)
@fjahn Maybe you could edit your original Post?
Hi,
We have implemented Google Consent Mode according to your documentation. We fire our GTM tags based on an event trigger like
klaro-googleTagManager-accepted
. It does fire the tag, however, the tag itself does not function properly. In the case of Google Analytics, we have no page views.The
klaro-google-analytics-accepted
event fires fairly late. It fires after the page view has been generated, and also thedataLayer
within theklaro-google-analytics-accepted
event doesn't contain apageView
. So it does fire the tag, but it doesn't generate apageView
. I don't see my website activity in the real-time view of Google Analytics.My full config file:
Any help would greatly be appreciated!