I am implementing Google Analytics with Google Consent Mode.
It works with these two scripts. It will actually track without setting cookies, as meant to.
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag("consent", "default", {
ad_storage: "denied",
analytics_storage: "denied",
wait_for_update: 500
});
gtag('set', 'url_passthrough', true);
</script>
And
<script async src="https://www.googletagmanager.com/gtag/js?id=MYCODE"></script>
<script>
gtag('js', new Date());
gtag('config', 'MYCODE');
</script>
But if I remove the last section and track through GTM instead, it does not work.
It only tracks if I change 'denied' to 'granted'.
Anyone knows how to implement Analytics and Consent Mode through GTM?
Here is my advice.
Create 2 variables that can read the status of your consent cookie. The goal is to have them return a value of granted
or denied
for the marketing and statistics value of your cookie. You can do this through a lookup table or a custom javascript variable, depends on what you like to work with. For instance, Cookiebot has their own variable in the Template Gallery which you can add to your workspace and it will return a value of |preferences|marketing|statistics|
depending on what level of consent is given by a user. It does this by reading their own cookie and parsing it into an easy pipe-separated format.
Once you have added the Cookiebot Consent State variable, go ahead and create a lookup table (regex type) variable. In it, add the recently created Cookiebot Consent State variable as the Input Variable. Add a row and type in marketing
for Pattern and add granted
as the Output value. Enable Set Default Value and add denied
.
View example Tag setup image After you are done, copy the tag and make one for statistics
with the same values.
Now, Install Simo Ahava's Google Consent Mode tag template from within the Template Gallery (gif explaining where to find it: https://imgur.com/kPlFuCG)
Keep the Consent Command dropdown value on 'Default'
Now set the Analytics (corresponds with analytics_storage
) and Ads (corresponds with
ad_storage
) values to the respective lookup table (type regex) variables we just created. View example row settings image
It is advised to have the default consent tag fire as soon as possible on a page, so set the tag firing priority to 1 https://imgur.com/FAxrgwO
Add an All Pages trigger to the tag and then save it.
Now, we need to setup the Update
tag for Google Consent Mode. This will allow you to update the settings whenever a user clicks on a consent banner. If you use Cookiebot, the click on the banner will generate events based on the type of consent given.
Create a Custom Event trigger, fill in the default Cookiebot event values (cookieconsent_marketing|cookieconsent_statistics)
and enable the option Use regex matching
, then save the trigger.
Finally, copy the original Google Consent Mode tag we just created. In the Consent Command
dropdown, select Update
. Check that the correct lookup table variables are set for Advertising and Analytics Google Consent Mode Update settings image and assign it the Custom Event trigger you just created.
Don't forget to update all your Google tags. If you have triggers setup that use a conditional check on cookie consent, you can remove these. Eg. If you have a pageview tag for Google Analytics that has a trigger for All Page where Consent level is XYZ, then remove the consent part from the trigger and just add a regular All Pages. Same goes for any custom event triggers you might use for ie. click tracking (generic events in GA). Keep in mind, this solution only works for Google tags, so make sure you still apply consent conditions in the triggers of tags like those for Facebook, LinkedIn, Twitter etc. If you are unsure about anything, check with a legal advisor.
Test & publish, or ask me any questions you might have.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With