I am using this module to allow visitors to opt out of receiving cookies from Google Analytics.
I followed this method https://www.drupal.org/node/1648286#comment-6145800 but it did not work because the javascript for GA was loaded before this one.
I noticed that in the eu_cookie_compliance.module file, js/eu_cookie_compliance.js was being loaded in the JS_THEME group. This group of scripts gets loaded last. I changed this line to use the JS_DEFAULT group instead, and I also changed the weight to 0, my thought being that we want this script to load before others so that it has a chance to block them.
This worked.
I think this should be a permanent change to the code and I'm happy to submit a PR but is there any reason why such a change might cause problems?
Drupal 7
Please review and test patch in #7
Drupal 8
Blocked by #2391025: Add support for inline JS/CSS with #attached, see comment #12
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | eu_cookie_compliance-load_script_earlier-2986131-7.patch | 528 bytes | bwaindwain |
| #4 | load_script_earlier-2986131-#1.patch | 806 bytes | hoegrammer |
Comments
Comment #2
hoegrammer commentedComment #3
svenryen commentedI don't know. Guess we'll just have to test it and see how it performs. Nice if you can provide a patch.
Comment #4
hoegrammer commentedComment #5
svenryen commentedThanks. I will take a look, but probably not before Sunday.
Comment #6
szeidlerI recently encountered the same issue and was also wondering about the JS_THEME setting being too late. I fixed the issue with the same approach as the patch in #4.
I'm wondering if there would be a scenario, where the change could cause a regression on existing sites?
Comment #7
bwaindwain commentedHere's the same patch updated for v1.28
Comment #8
qazema commentedI think we have the same problem in the D8 version, Google Analytics is not unset even if we specify the js file in "disable javascript". Is there already an issue posted about the D8 version of this problem ?
Comment #9
anybodyPatch from #7 works great! Confirming RTBC, couldn't see any negative impacts.
This patch is also be needed for a eu_cookie_compliance integration in googleanalytics module proposed here: #3060312: Better GDPR compliance: Fix Do not track & EU Cookie Privacy integration
Comment #10
anybodyI agree with @Qazema, we need the same patch for Drupal 8. Any volunteers?
Comment #11
svenryen commentedA Drupal 8 version of the patch is required before we can commit.
Comment #12
anybodyTL;DL:
We should RTBC the Drupal 7 version
We should create a follow-up issue for Drupal 8 because 8.x Google Analytics JS weight is blocked by #2391025: Add support for inline JS/CSS with #attached since years... and I guess it will still need years ...
Google Analytics tracking code is put as one of the first elements in head so we will NEVER cleanly be able to put our code before that, until that core issue is fixed.
More info:
Well... I just had a look and played around a lot... I can't see any solution for Google Analytics in D8 currently to move the JavaScript below the libraries (and especially after eu_cookie_compliance) like in D7. So I'd suggest to RTBC this for Drupal 7 as this is a 7.x blocker for #3060312: Better GDPR compliance: Fix Do not track & EU Cookie Privacy integration
Drupal 8 also needs this solution for #3060312: Better GDPR compliance: Fix Do not track & EU Cookie Privacy integration to work, but #2391025: Add support for inline JS/CSS with #attached blocks this and there's no solution.
So all in all I predict we won't have a Drupal 8 solution in the near future and block the Drupal 7 version from being updated, which makes no logical sense to me for this 7.x issue.
For Drupal 8 I suggest we need to push a solution for #2391025: Add support for inline JS/CSS with #attached to put the Google Analytics Script a lot lower like @hass already wrote there several times. I completely agree with him.
There is a long history in GA 8.x issues like #2890438: Clean way to avoid inline js?, #2753215: Put ga script lower in head tags and others, wich all end up in that core issue.
Comment #13
svenryen commentedSetting this to RTBC and scheduling it for committing to D7 branch following the request in #12.
Comment #15
svenryen commentedComment #16
anybodyGreat!! :) Thank you @svenryen
Looking forward to 7.x-1.30 ! :)
Comment #17
criscomWe tested the patch. It works in conjunction with https://www.drupal.org/files/issues/2019-07-10/googleanalytics-7.x-2.x-b... when set to Opt-in. Don't track visitors unless they specifically give consent. (GDPR compliant)
It didn't work with Opt-out. Track visitors by default, unless they choose to opt out. The Google Analytics cookies were not removed after clicking Decline.
Comment #18
anybodyThank you @criscom,
I guess it's not the fault of this patch. Please comment the Google Analytics issue accordingly instead. This patch does what it should.
Comment #19
criscomOk, thanks for the heads-up. I added it to one of the issues in the GA issue queue where you contributed as well: https://www.drupal.org/project/google_analytics/issues/3060312#comment-1...