1

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 Feb 22 at 9:13
5

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 his consent:

Drupal.eu_cookie_compliance.hasAgreed()

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:

document.getElementsByClassName('thirdPartyFormWrapper');

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
        return;
      }
      // 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.

|improve this answer|||||
  • 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 Feb 23 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 Feb 23 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. – hotwebmatter Feb 23 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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