Problem/Motivation

We would like to make Google Tag Manager support the new beta Google consent mode.

At the moment the method for adding support for the mode is to include for example the following code snippet on page:

<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('consent', 'default', {
'ad_storage': 'denied',
'analytics_storage': 'denied',
'wait_for_update': 1500 //milliseconds
});
gtag('set', 'ads_data_redaction', true);
</script>

This can be easily be done in HTML template but the problem is that Google tag module seems to attach all the JS code very early on the page and the above code snippet should come before it.

More information: https://developers.google.com/gtagjs/devguide/consent

Currently it seems like both scripts are loaded ok but Google tag manager still fires all the tags before the codes are loaded in wrong order.

Proposed resolution

We would suggest that we add support for this to the module as a setting to support the consent mode. If the setting is enabled, it would add the above code before the Google tag manager default loading code.

User interface changes

Create new setting to enable consent mode.

CommentFileSizeAuthor
#12 consent_mode.png19.81 KBnewaytech

Issue fork google_tag-3180185

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

HeikkiY created an issue. See original summary.

skin’s picture

It would be useful to put also a custom code in addition to the one suggested by Google, for example if you use Cookiebot you need a different code than the one suggested by Google.

anybody’s picture

I agree it would be best and most simple solution to add this as setting. And in terms of GDPR / DSGVO this is quite important in the EU.

lorenzs’s picture

Status: Active » Needs review

I'd suggest to ignore Google's suggestion here if you want to be GDPR compliant.

The only proper way to be compliant is:

  1. include Tagmanager by default (as the module facilitates)
  2. configure Tagmanager to use consent-mode (container settings)
  3. add a 'consent-initialization' tag with your Consent-Management banner (preferably via a template)
  4. configure on each of your other tags requiring a consent an 'additional' consent

f.e. for a GA4-tag add 'analytics_storage' again under 'advanced settings' > 'Require additional consent for tag to fire' (allthough this is also a built-in setting)

Also see this well-written article;

https://brianclifton.com/blog/2022/03/14/google-consent-mode-breaks-privacy-laws/

So this patch or 'solution' from Google is not needed/wanted for GDPR compliancy, on the contrary, it will do the opposite.

japerry’s picture

Version: 8.x-1.x-dev » 2.0.x-dev
Status: Needs review » Needs work

We're currently working with google to implement this in the Google Tag module. (2.x). Note, if this wasn't implemented, analytics would be significantly curtailed on March 6th: https://support.google.com/google-ads/answer/14505993?sjid=1828950924013...

The default configuration will add the 'denied' option by default to all tags. Supposedly the 'wait_for_update' parameter will allow other drupal modules (or third party CMPs) via Javascript to change the setting to accepted when a user consents to tracking.

japerry’s picture

Status: Needs work » Needs review

Add default settings to gtag.js. Its not configurable from Drupal at the moment.

One thing that needs validation is the 'wait_for_update` variable. In this issue it suggested 1500ms. Google suggests 500ms. It'd be good to see if a third party consent manger works properly with this delay.

heikkiy’s picture

The code looks good and it seems to also support the changes in Google Consent Mode v2. There was a good technical documentation available in Cookie information also: https://support.cookieinformation.com/en/articles/8707953-how-to-update-....

One thing I am wondering is that should this still be configurable in the module? We have implemented support for GCM in Cookie information module https://www.drupal.org/project/cookieinformation and it has a module setting to enable the additional tag. I don't see it as an huge issue that it's enabled by default but for example the blog article from Cookie information mentions that you also need to update the cookie popup template to support v2.

  • japerry committed c8056086 on 2.0.x
    Issue #3180185: Support Google Analytics consent mode
    
japerry’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

newaytech’s picture

StatusFileSize
new19.81 KB

Hi folks - just updated to the latest version - and can see that the consent event is fired after the container loaded event. Any further config / GTM config required?

nodestroy’s picture

I can confirm the behaviour from #12. Since the consent mode defaults are set in gtag.js which is loaded after gtm.js (which adds GTM), the order is not correct.

To get this working we would need the consent mode defaults before loading of GTM.

At least this is true if I add a GTM-ID in the container settings.

newaytech’s picture

maybe set the order using dependencies in the libraries.yml?

seeing this:

script src="/modules/contrib/google_tag/js/gtm.js?s9z93m">

script src="/modules/contrib/google_tag/js/gtag.js?s9z93m">

script src="/core/misc/progress.js?v=10.2.3">

script src="/core/assets/vendor/loadjs/loadjs.min.js?v=4.2.0">

script src="/core/misc/debounce.js?v=10.2.3">

script src="/core/misc/announce.js?v=10.2.3">

script src="/core/misc/message.js?v=10.2.3">

script src="/core/misc/ajax.js?v=10.2.3">

script src="/modules/contrib/google_tag/js/gtag.ajax.js?s9z93m">

anybody’s picture

I can also confirm the order. Should this be reopened again perhaps? Or separate issue?

japerry’s picture

Yes, lets create a new issue to track this. Hopefully if there are any other issues related to consent can be thrown together for one more release. Clearly the tests we have aren't tracking this correctly, so before making another release we will want to buff up the tests.

newaytech’s picture

I've just noticed that all of the tracking on on of my sites has gone dark since implementing the consent code.

I ticked the box in the module - hoping that the cookieless GA4 machine learning would have our back. Only on deeper diving into the pre-reqs of the cookieless GA4 world - do I then read that you need a minimum of 1000 users per day in order for the model to "learn". See here: https://support.google.com/analytics/answer/11161109?hl=en-GB&utm_id=ad

I've had to turn the consent settings off for now for my client - as all his reports went dark - due to the Behavioural Modelling having insufficient data to fill in the gaps - which kinda sucks for the smaller GA4 users...

I guess the only alternative for the smaller user is to have consent banner - and then only gain insight on the users that actually says yes to analytics_storage...

Anybody else seeing this - I've seen another comment over on another issue here that talks of their reports going dark after ticking the box - and I guess this is the reason.

I personally think that the tick box should be off by default - as unless we make the pre-reqs clear - a lot of smaller users may lose tracking altogether.

anybody’s picture

@newaytech I guess we're seeing the same on a page. I was still investigating on the reason.

wells’s picture

I'm also dealing with the issue of lost events after updating from 2.0.2.

I'm not sure how the consent flag was set initially but one thing I just noticed is that the "Enforce Privacy Consent Policy" checkbox always loads in the google tag setting form as checked even if consent mode is disabled. So if other unrelated changes are made in the UI it resets consent mode to true.

anybody’s picture

@japerry: Ping - could you please check #3424623: "Enforce Privacy Consent Policy" checkbox default value is checked even when it is disabled? (Major)

The functionality is currently broken!

westlinnsamething’s picture

USER WARNING: THIS PAGE IS FILLED WITH FALSE INFORMATION.

The Module creators used the wrong code in their solution. Google is a supporter of this Module, and Google doesn't even understand what it's talking about. They didn't make it clear there are two ways to initialize consent mode:

You can use the GTAG method that this Module incorrectly implemented, or you can use Google Tag Manager. You should not use both, as spelled out in their own documentation.

Take the following into consideration before implementing consent mode:

If you use Tag Manager and want to maintain your own banner, the recommended approach is loading your banner through the Tag Manager container. To do so, you need to create a consent mode template. Alternatively, you can use a consent mode template from the Community Template Gallery.

If you use gtag.js, make sure you have installed the Google tag on every page of your website. The consent mode code gets added to each page of your website.

And Google's advice to be compliant with GDPR made me laugh so hard that I had tears rolling down my face, but I'l stay on topic.

To sum it up: Consent Mode does NOT need the help of the Google Tag Module to work. In fact, it does quite well without it.

What the module CAN do is add this single line of code for everyone, which goes directly above the Google Tag Manager code insertion:

window.dataLayer = window.dataLayer || [];

This code can be added regardless if Consent Mode is used or not. Apply it to all users. It establishes the dataLayer. It does no harm. There's no need for a Consent Setting in the Google Tag Module.

That's it....this is all I can take in one night...