I'm using Drupal 8.6.14 (going to upgrade soon) and I am trying to create a cookie consent page that actually works!

I've used many different modules like EU Cookie Compliance, GDPR Compliance and several others, but all of them fail to block Google Analytics cookies.

I even used a couple of patches on Google Analytics that I found in the community in order to support EU Cookie Compliance module, but all of them fail.

To be exact, EU Cookie Compliance seems to load the cookies as normal, then deleting them as the dialog of consent appears and then loading them all again once I pick any option.

All the other solutions seem not to work at all (I get the cookies any time the page loads). BTW, all the solutions I've found are for <8 versions.

Any idea how I can get this done? Am I missing something?

  • Welcome to Drupal Answers! 👋 This request sounds a little bit broad and I'm also unsure if this now is a support request for the EU Cookie Compliance module or if you are asking how this can be done generally? Maybe also tell us how exactly you are placing GTM on your page and what exact string you've put in the library exclude section of EU Cookie Compliance settings. Are you aware that this module also offers a function that lets you determine if a user accepted or rejected cookies? Please edit your question for clarification. Many thanks
    – leymannx
    Commented Feb 22, 2020 at 9:13

3 Answers 3


From the EU Cookie Compliance module's project page on drupal.org:

If you want to conditionally set cookies in your module, there is a JavaScript function provided that returns TRUE if the user has given their consent:


For example, if you want to disable Google Analytics, you could do the following: http://drupal.org/node/1648286#comment-6145800

Another example of how it can be used with Google Analytics is provided by dagomar in this comment.

Here is another example showing how to set cookies after user clicks the accept button: http://drupal.org/node/1624876#comment-6111688

I've personally used this to prevent third-party code snippets from loading until after compliance is granted, by making a custom module and including the call to hasAgreed() in my module's JavaScript behaviors.

But as the first example above shows, a custom module is not necessary for Google Analytics in particular. You can simply add the following code to the Advanced Settings->Custom JS in Google Analytics:

if (!Drupal.eu_cookie_compliance.hasAgreed()){
window['ga-disable-UA-xxxx-1'] = true;

EDIT (2020-02-23):

It seems that you want to enforce extremely strict GDPR compliance, and prevent the cookies from being loaded at all unless consent has been granted.

You can do this with a custom module, leveraging the same hasAgreed() JavaScript behavior provided by the EU Cookie Compilance module.

First you need to remove the code snippet from your site completely and replace it with a placeholder.

If it's just one script in your HTML head, you might just edit html.html.twig.

If there are multiple scripts you need to prevent from running, it will be more work.

In my case, I replaced dozens of different third-party forms with an empty <div> element:

<div id="1234" class="thirdPartyFormWrapper"></div>

My custom JavaScript behavior looks for all the DOM elements with that class:


The 4-digit number in the div ID corresponds to the unique third-party form ID for each form.

If consent has not been granted (or if the EU Cookie Compliance module is not installed or enabled, and hasAgreed() is not available) then my custom module uses the 4-digit ID to look up substitute content (saying, e.g., "Please accept cookies to view the Yoyodyne, Inc. Gnomovision Mailing List Subscription Form," or whatever that particular form is.)

If consent has been granted, then my custom module uses the 4-digit ID to inject the JavaScript snippet for each of the third-party forms into the placeholder <div>.

Once the form snippet is injected, you can figure out how to handle the cookies. If consent is withheld, neither snippets nor cookies will load.

You can also use this technique to handle cookies for extremely strict GDPR compliance, following this pattern:

 * Cookie handling for strict GDPR compliance
  Drupal.behaviors.customCookieConsent = {
    attach: function (context, settings) {
      if (!Drupal.eu_cookie_compliance || !Drupal.eu_cookie_compliance.hasAgreed()) {
        // Bail out if cookie consent not given
      // If cookie consent is given, set cookie (unless already set)
      if (!$.cookie('customConsentTime')){
        var consentTime = new Date();
        $.cookie('customConsentTime', consentTime.toISOString(), {expires: 3650});

This approach is a bit more work, but it will prevent scripts and cookies from being loaded at all before consent is granted.

Finally, I should note that based on your comments here and on Drupal.org, I think you should do some research on "Drupal Behaviors" for your JavaScript. This will help you to avoid the "Uncaught ReferenceError: Drupal is not defined" message you have been seeing.

  • when i insert the custom JS i get this error "Uncaught ReferenceError: Drupal is not defined" even my dependencies on both google analytics and eu cookie compliance module are using core/drupal and core/drupalSettings. any idea of whats going on?
    – b10z
    Commented Feb 23, 2020 at 15:53
  • 1
    i think that i have found what cause the problem and i found a workaround, described here drupal.org/project/eu_cookie_compliance/issues/… But i still see the cookies __id and __ga on my cookies list without consent (even if there is no traffic in my analytics page. Any way to fix this?
    – b10z
    Commented Feb 23, 2020 at 17:43
  • @b10z I have edited my answer to give more details. If you find my approach effective, please let me know by accepting my answer. Commented Feb 23, 2020 at 21:25
  • For me, the EU Cookie Consent .hasAgreed() gives always false and then always true, even when the user has not opted for anything Commented Feb 16, 2022 at 13:55

There is a new module for Drupal 8 and 9: COOKiES. It supports the modules Google Analytics and Google Tag Manager (and more). Maybe that's what you are looking for.


User consent management module for Drupal 8 and 9.

The module enables existing third-party-integration modules continued to be used in compliance with the GDPR (of course without manipulating their code). It solves the basic problem that when using third-party-integration modules, the user must agree to the use of cookies (according to GDPR) before they are installed.

IMPORTANT: No liability is assumed regarding compliance with the GDPR.

The COOKiES module provides (with the Library Cookies JSR) a fully configurable user interface for user decisions. It also supports out-of-the-box some key third-party integration modules (especially those included in the Thunder distribution):

  • Google Analytics module
  • Google Tag Manager
  • Video embed, core:media module
  • Twitter media module
  • Instagram media module

This is how I solved the issue.


  • Drupal 8.9.6
  • Eu Cookie Compliance 8.x-1.x-dev
  • Google Analytics 3.1

Settings under /admin/config/system/eu-cookie-compliance/settings:

  • Consent method: Opt-in. Don't track visitors unless they specifically give consent. (GDPR compliant)
  • Disable Javascripts: https://www.googletagmanager.com/gtag/js

It behaves as expected:

  • Google Analytics cookies are not loaded unless the user accepts cookies
  • If the user rejects cookies, cookie banner is removed and no Google Analytics are placed in any subsequent page loads

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.