Support Google Analytics consent mode

Created on 2 November 2020, about 4 years ago
Updated 4 April 2024, 7 months ago

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.

Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

🇫🇮Finland heikkiy Oulu

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇧🇪Belgium lorenzs

    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.

  • Status changed to Needs work 10 months ago
  • 🇺🇸United States japerry KVUO

    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.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 10 months ago
    54 pass
  • Status changed to Needs review 10 months ago
  • 🇺🇸United States japerry KVUO

    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.

  • 🇫🇮Finland heikkiy Oulu

    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.

  • Pipeline finished with Skipped
    9 months ago
    #88972
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 9 months ago
    54 pass
    • japerry committed c8056086 on 2.0.x
      Issue #3180185: Support Google Analytics consent mode
      
  • Status changed to Fixed 9 months ago
  • 🇺🇸United States japerry KVUO
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • 🇬🇧United Kingdom newaytech

    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?

  • 🇦🇹Austria nodestroy

    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.

  • 🇬🇧United Kingdom newaytech

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

    seeing this:

  • 🇩🇪Germany Anybody Porta Westfalica

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

  • 🇺🇸United States japerry KVUO

    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.

  • 🇬🇧United Kingdom newaytech

    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.

  • 🇩🇪Germany Anybody Porta Westfalica

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

  • 🇺🇸United States wells Seattle, WA

    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.

  • 🇩🇪Germany Anybody Porta Westfalica

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

    The functionality is currently broken!

Production build 0.71.5 2024