First of all thanks a lot for this great module, which we're using since Drupal 6 with so many wonderful options.

Since GDPR is a major topic in EU countries, we're lacking a proper solution for several points when using Google Analytics with this module, for example:

  • No proper integration with EU Cookie Compliance
  • Do-Not-Track only works for logged in users and without static caching (doesn't work with boost e.g.)
  • Cookies were set and (with local caching for GA Tracking code disabled) the tracking code was loaded from the Google servers if we used workarounds for GDPR compliance.

That reflects in a lot of issues in this module with many hacky workarounds posted, but no well working solution for end-users.

Drupal 8 issues:

#2917905: Add JS-function / method to set ga-disable-...
#2805127: DoNotTrack privacy feature

Drupal 7 issues:

#2173433: No GA code with alternative page caching engines and DNT header
#2971644: Eu cookie compliance support

This issue is a try to finally fix these typical problems with two patches, because it makes sense to combine the solution in a runtime JavaScript solution to allow static caching to work and also make interaction with other modules like eu_cookie_compliance possible. Because eu_cookie_compliance is widely used, I decided to also add it to the admin UI to have a better user experience for non-devs.

Here
Drupal 7:

Drupal 8:

So please help to push #2986131: JS loads too late to affect Google Analytics for Drupal 8!

Please test, review and comment the patch and please help adding tests so that we can roll this out for Drupal 8 and Drupal 7 as soon as possible.

Thank you all!

Comments

Anybody created an issue. See original summary.

anybody’s picture

Category: Bug report » Feature request
anybody’s picture

Title: Fix Do not track, EU Cookie Privacy integration & GDPR compliance » Better GDPR compliance: Fix Do not track & EU Cookie Privacy integration
anybody’s picture

Patch for Drupal 7.x-2.x attached. Requires #2986131: JS loads too late to affect Google Analytics to work with EU Cookie Compliance (if used), until that issue is fixed.

anybody’s picture

Status: Active » Needs review
Issue tags: -Needs tests
StatusFileSize
new13.84 KB
new13.67 KB

First patch attempt for Drupal 7 and 8 attached. Please test, review and comment. The Drupal 7 version was tested a lot manually, the Drupal 8.x-2.x version was NOT tested manually yet, it is a code based upgrade from 7.x.

I'm not planning to create the 8.x-3.x release and like the idea from the matomo module to use a twig template for the JavaScript code a lot: #2948060: Implement twig template for tracking code customization which would mean major changes. If someone is willing to create a patch for 8.x-3.x you're welcome. Otherwise I'd like to wait if something happens into that direction. :)

The last submitted patch, 5: googleanalytics-7.x-2.x-better_gdpr_dnt_eucc-3060312-5.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 5: googleanalytics-8.x-2.x-better_gdpr_dnt_eucc-3060312-5.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

anybody’s picture

Fixed tests.

The last submitted patch, 8: googleanalytics-7.x-2.x-better_gdpr_dnt_eucc-3060312-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 8: googleanalytics-8.x-2.x-better_gdpr_dnt_eucc-3060312-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

anybody’s picture

Last one for today... Does anyone have an idea how we can check https://developer.mozilla.org/en-US/docs/Web/API/Navigator/doNotTrack?

Status: Needs review » Needs work

The last submitted patch, 11: googleanalytics-7.x-2.x-better_gdpr_dnt_eucc-3060312-11.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

anybody’s picture

StatusFileSize
new13.99 KB
anybody’s picture

anybody’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 14: googleanalytics-8.x-2.x-better_gdpr_dnt_eucc-3060312-13.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

anybody’s picture

New patch which respects the eu_cookie_compliance "Automatic" setting (over DNT) and additionally fixed bugs in 8.x-2.x

"Automatic. Respect the DNT (Do not track) setting in the browser, if present. Uses opt-in when DNT is 1 or not set, and consent by default when DNT is 0."

Status: Needs review » Needs work

The last submitted patch, 17: googleanalytics-8.x-2.x-better_gdpr_dnt_eucc-3060312-17.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

anybody’s picture

Status: Needs work » Needs review
StatusFileSize
new14.16 KB

Status: Needs review » Needs work

The last submitted patch, 19: googleanalytics-8.x-2.x-better_gdpr_dnt_eucc-3060312-19.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

anybody’s picture

Status: Needs work » Needs review
StatusFileSize
new14.16 KB

Status: Needs review » Needs work

The last submitted patch, 21: googleanalytics-8.x-2.x-better_gdpr_dnt_eucc-3060312-21.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

anybody’s picture

StatusFileSize
new13.99 KB
anybody’s picture

Status: Needs work » Needs review
StatusFileSize
new13.99 KB
anybody’s picture

Yeah, 7.x and 8.x are finally passing... sorry for the many comments... Now please review :) I think this is a big step for GDPR in Google Analytics and EU Cookie Compliance.

anybody’s picture

Issue summary: View changes

See updated issue description, especially to see why 8.x CAN NOT work. Please test and review 7.x. It's working and we're already using it in production.

For Drupal 8 we could discuss to proceed with the patch in #24 from #2917905: Add JS-function / method to set ga-disable-... and improve it to fit @hass's requirements. It works differently so that the Drupal 8 core issue is workarounded.

anybody’s picture

Issue summary: View changes
criscom’s picture

Thanks a lot for your efforts and the hard work you put in for this. This is very much appreciated.

We have tested the patch for D7 thoroughly in conjunction with the patch in https://www.drupal.org/project/eu_cookie_compliance/issues/2986131

We noticed that following:

In order for this feature [0] to work in conjunction with EU Cookie Compliance, the EU Cookie Compliance Extras module and the Google Analytics checkbox "EU Cookie Compliance integration" must be enabled [1].

[0]: https://screenshot.sunlime.at/d79663f36815b99c316683b84e28a5b0
[1]: https://screenshot.sunlime.at/a57e81fdf776e9fdba921ecd7a076b92

However, [0] only works with the Opt-in setting in the EU Cookie Compliance module: Opt-in. Don't track visitors unless they specifically give consent. (GDPR compliant) [2]

[2]: https://screenshot.sunlime.at/03bef05e36ca0dedeb6216a6ea27945a

If the Opt-out option in the EU Cookie Compliance module is selected [3], the GA code is initially not loaded and the users are not tracked until they click the "OK, I agree" button in the banner. See the video (open the link in Chrome to view the video) in [4]: the GA code is loaded after the consent button is clicked (not before as is expected).

[3]: https://screenshot.sunlime.at/3d9796eafe48a0ebb98fb0bd0eef3a69
[4]: https://screenshot.sunlime.at/a015a3f7eb949359e919be9f67ecd294

Not sure whether this is relevant for this issue or if this belongs to the EU Cookie Compliance issue queue. Thanks again for providing this patch as it helped a lot.

hass’s picture

Is there any progress in implementing a generic API in EU cookie compliance module where all modules around can integrate with so that all are using the same and there is no module specific code. EU cookie compliance need to provide the API from my point of view to implement a clean solution.

IckZ’s picture

For the moment it looks like that this works perfect for my usecase (the new "EuGh Urteil")

Thank you a lot!

wranvaud’s picture

Issue summary: View changes
4kant’s picture

Hi Anybody,

your patch #17 for D7 gave me green light on every online-compliance checker. They say that the website is GDPR compliant.
I use EU Cookie Compliance module, version 7.x-1.31 - there is no patch necessary anymore.

Thanks a lot!

anybody’s picture

@4kant and everyone else here: You may also want to try the EUCCX module for D7 with Google Analytics integration.

francescbassas’s picture

patch #17 applied on a D7, with EU Cookie Compliance module at 7.x-1.32, and worked as described

+1 to push this to a stable version. Thanks @anybody for the great work!

anybody’s picture

@hass RE#29:

Is there any progress in implementing a generic API in EU cookie compliance module where all modules around can integrate with so that all are using the same and there is no module specific code. EU cookie compliance need to provide the API from my point of view to implement a clean solution.

Could you perhaps have a look and provide feedback on #3130015: Write & document public JS API for actions & events
Some progress over there ;) ;)

phma’s picture

After I applied patch #17 on our D7 site, I'm getting an error if googleanalytics_trackadsense is set to TRUE by the user. It has to do with the order the scripts are loaded.

    if (variable_get('googleanalytics_trackadsense', FALSE)) {
      // Custom tracking. Prepend before all other JavaScript.
      // @TODO: https://support.google.com/adsense/answer/98142
      // sounds like it could be appended to $script.
      drupal_add_js($googleanalytics_adsense_script, array('type' => 'inline', 'group' => JS_LIBRARY-1, 'requires_jquery' => FALSE));
    }

The group is set to -101, so the Drupal object is not available in JavaScript yet. I have yet to find out if support for AdSense is a requirement for us or not. Just be aware of the limitation.

EDIT: Seems like advagg_mod can help to work around this.

felubra’s picture

I was unable to make this integration work with drupal/eu_cookie_compliance > 7.x-1.32. The Google Analytics script is always disabled because `eccHasAgreed` is always false even if the user agreed before.

drdam’s picture

Same problème

tfranz’s picture

Same here – i can't get it to work ...

tfranz’s picture

As felubra pointed out, "eccHasAgreed" does not seem to work.
Instead I tried to read the value of the ecc-cookie "cookie-agreed" – that seems to work:

googleanalytics.module, patched with (Patch), lines 380ff:

     // Define eu_cookie_compliance conditions check (https://www.drupal.org/project/eu_cookie_compliance)
     if (variable_get('googleanalytics_eu_cookie_compliance', 0) && module_exists('eu_cookie_compliance')) {
       $script_prefix .= 'if (typeof Drupal.eu_cookie_compliance === "undefined") {console.warn("googleanalytics: googleanalytics eu_cookie_compliance integration option is enabled, but eu_cookie_compliance javascripts seem to be loaded after googleanalytics, which may break functionality."); }';
-      $script_prefix .= 'var eccHasAgreed = (typeof Drupal.eu_cookie_compliance !== "undefined" && Drupal.eu_cookie_compliance.hasAgreed());';
+      $script_prefix .= "eccCookieValue = (document.cookie.match('(^|; )'+'cookie-agreed'+'=([^;]*)')||0)[2];";
+      $script_prefix .= 'var eccHasAgreed = (typeof Drupal.eu_cookie_compliance !== "undefined" && (Drupal.eu_cookie_compliance.hasAgreed() || eccCookieValue == 2));';
       $script_prefix .= 'Drupal.googleanalytics.ga_disable = Drupal.googleanalytics.ga_disable || !eccHasAgreed;';
     }
mile23’s picture

Currently using EUCC 7.x-1.37 with google_analytics 7.x-2.6, patched with #17 to good effect.

poker10’s picture

Are there any chances this will be commited anytime soon? We have new e-Privacy directive comming into the force in 2022 and this directive strictly requires you to obtain the explicit consent from end-users before using cookies. So this issue will be crucial for all sites in EU in the next months.

See: https://www.cookiebot.com/en/eprivacy-regulation/ or https://www.lexology.com/library/detail.aspx

gngn’s picture

Status: Needs review » Needs work

I just had to check an old D7 project using #17 - and encountered PHP notice:
Undefined variable: disabled in googleanalytics_form_user_profile_form_alter()

Taking a look I found that #17 removes the lines setting $disabled (from googleanalytics_form_user_profile_form_alter()):

-    // Disable tracking for visitors who have opted out from tracking via DNT (Do-Not-Track) header.
-    $disabled = FALSE;
-    if (variable_get('googleanalytics_privacy_donottrack', 1) && !empty($_SERVER['HTTP_DNT'])) {
-      $disabled = TRUE;
-
-      // Override settings value.
-      $account->data['googleanalytics']['custom'] = FALSE;
-
-      $description .= '<span class="admin-disabled">';
-      $description .= ' ' . t('You have opted out from tracking via browser privacy settings.');
-      $description .= '</span>';
-    }

and keeps the code directly after:

    $form['googleanalytics']['custom'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable user tracking'),
      '#description' => $description,
      '#default_value' => isset($account->data['googleanalytics']['custom']) ? $account->data['googleanalytics']['custom'] : ($custom == 1),
      '#disabled' => $disabled,
    );

With that $disabled is used without ever assigning a value.

I think if we really do not need to disable the 'Enable user tracking' checkbox at all - than we should also remove the line
'#disabled' => $disabled,.
Any thoughts?

csmdgl’s picture

Re-roll #17 for 7.x-2.8

mvc’s picture

This patch generally makes sense but is not compatible with the eu_cookie_compliance module's category feature. If the user has defined different categories (eg. required, performance, marketing) then Drupal.eu_cookie_compliance.hasAgreed() will always return true. We need to allow the user to specify which category includes GA, then check that the user has accepted that particular category by calling Drupal.eu_cookie_compliance.hasAgreed('category_name') instead.

summit’s picture

Hi, It seems this is the god-issue about getting a noticeable effect on the cookies with any setting. In my case without a patch on latest eu_cookie module, It also does not reload the page after rejecting cookies. What to do please? And I am on Drupal 10 now?
Sorry for earlier making https://www.drupal.org/project/eu_cookie_compliance/issues/3425103, but after further investigation I found more issues about this.
Thanks for your reply in advance, greetings,