Has anyone had any success getting the google analytics code to work? When I try to insert the code from dagomar's post I get this error:
Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.
Comments
Comment #1
Miszel commentedI'd suggest trying to disable GA by setting: window['ga-disable-UA-XXXXXX-Y'] = true; as described at he bottom of this page: https://developers.google.com/analytics/devguides/collection/gajs/
Please let us know if it works.
Comment #2
Boffy31 commentedThat has fixed it thanks, the full code I added to the Advanced Settings->Custom JS in google analytics was:
In addition, I needed to change the JS position in the module to header rather than footer to get it to render. I don't know why (perhaps an option for this change could be included in the module admin page?)
Comment #3
Miszel commentedComment #4
joco_sp commented#2 worked form me. I put the code in 'Code snippet (before)' and 'JavaScript scope' in Footer.
Comment #5
pat51 commented@Boffy31: I followed your instructions (#2) and everything works fine. Thank you.
Comment #6
pietrocap commentedHi, I'm struggling trying to follow suggestions on this page.
I tryed putting the code in 'Code snippet (before)' and also in 'Code snippet (after)'.
I get a javascript error generated by google analytics:
Uncaught TypeError: Cannot read property 'hasAgreed' of undefined
I can't do as in #4 because 'JavaScript scope' is no more supported in google analytics module options.
Comment #7
Anonymous (not verified) commentedI too have difficulties making it works with Google Analytics module. The ga.js file is printed in the
<head>but the eu_cookie_compliance.js file is loaded at the bottom of the page. So calling toDrupal.eu_cookie_compliance.hasAgreed()function before it's loaded will fail, the problem is that I need to load GA in the document<head>for statistics accuracy purpose.Comment #8
pietrocap commentedHi LaAaZ,
I've just modified eu_cookie_compliance.module and now eu_cookie_compliance.js file is loaded in the header.
In google analytics settings i've pasted
the javascript error disapeared and it seems that the GA cookie it's loaded only after the user has agreed.
The eu cookie compliance module seems to work well as before.
If you want you can't try it at http://www.pietrocappai.info
Comment #9
svenryen commentedGreat, Pietro! Can you please submit a patch for your changes?
Comment #10
androe commentedsvenryen I don't know how to submit a patch but in eu_cookie_compliance.module at line 126 you should edit
and replace footer with header. It works!
Comment #11
pietrocap commentedHi,
it is what I did and said in #8. I'll try to submit a patch.
Comment #12
Anonymous (not verified) commentedGreat, thank you very much !
I spotted the line in #10 but I wasn't sure of what I was doing. Do you have a tutorial on how to submit a patch ? That will be handy someday.
Comment #13
pietrocap commentedI reopen the issue because it's not fixed with the latest version of google analytics and 7.x-2.x version as said in #11. I upload a patch.
Comment #14
svenryen commented@LaAaZ: https://www.drupal.org/patch and https://www.drupal.org/node/707484
Comment #15
pietrocap commentedHello svenryen,
I followed all the steps at https://www.drupal.org/node/707484 but the patch I'm uploading now seems to be the same as the previous one at #13. If there is something wrong please tell me what to do.
Comment #16
Anonymous (not verified) commentedThank you svenryen, I'll check this out.
Comment #17
francoud commentedI often do not use external modules to add the "google analytics" stuff, I just add the script in the page.tpl.php file in my theme (ok it's not clean but it works ;) Something like:
This script works only if I also apply the footer/header patch in #15. So if possible I vote for apply the patch to the -dev version at least :)
Comment #18
svenryen commentedThanks for the patch, @pietrocap - I'll take a look at it some time soon.
Comment #19
gaëlgHere's a patch for Drupal 8. If it's not right to add this logic into the module, it could easily be a separate submodule.
Comment #20
svenryen commentedComment #21
svenryen commented@pietrocap, thanks you again for the patch. I tried applying it and it doesn't apply to the latest beta release. Do you think you could re-roll it, and I'll take a look an review it?
Comment #22
svenryen commentedComment #23
svenryen commented@GaëlG Do you think you can open a separate issue with your 8.x patch?
Comment #24
gaëlg@svenryen Done: #2827860: Prevent Google Analytics from adding cookies until the user accept cookies
Comment #25
svenryen commentedI have added an option to -dev to place the script in the header rather than the footer. It's in the Advanced fieldset. This can make it easier for people that want to use `hasAgreed()` to interact with other scripts and block tracing from working until the user has accepted cookies.
Comment #27
svenryen commentedComment #28
svenryen commentedComment #29
abaier commentedI tried to implement the solution of #2, placing the disable-code inside the google_analytics advanced settings (code before) and setting the EUCC script scope to header – but also got the Uncaught TypeError.
Using the latest dev of EUCC (7.x-1.23+13-dev) I saw that eu_cookie_compliance.module now places its script into the JS_THEME group including weight 100. It works if I remove these settings, which then places the script in JS_DEFAULT, I think, loading before the googleanalytics.js to make hasAgreed() available.
What was the purpose of changing the group?
----
EDIT
----
Finally solved it like this
Comment #30
svenryen commentedI don't recall the purpose of changing the group, I'll have to take a look once the GDPR features are implemented. I also have some scheduled vacation after that, so the earliest would be late June.
Comment #31
abaier commentedNo problem, I can imagine the workload these days … At least I achieved to stop GA tracking before consent was given, for now.
Comment #32
svenryen commentedRegarding a permanent fix for GA, you can watch #2933614: [2.x?] How to block cookies until the user accepts, for example Google Analytics cookies?
Comment #33
abaier commentedThanks, I am already following the issue. Currently I was searching for a quick solution for a D7 project.
Comment #34
anybodyI think we can close this as duplicate of #2971644: Eu cookie compliance support now.
Also see #3060312: Better GDPR compliance: Fix Do not track & EU Cookie Privacy integration for a full featured EU Cookie Compliance solution (with Admin UI) in Google Analytics - no more hacks needed!
Comment #35
b10zgr commentedwhen i try to add to the custom JS i get this message "Uncaught ReferenceError: Drupal is not defined".
google_analytics:
google_analytics:
version: VERSION
js:
js/google_analytics.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
google_analytics.debug:
version: VERSION
js:
js/google_analytics.debug.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
google_analytics.admin:
version: VERSION
js:
js/google_analytics.admin.js: {}
dependencies:
- core/jquery
- core/drupal
google_analytics.test:
version: VERSION
js:
src/Tests/GoogleAnalyticsJavaScriptTest.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
here are my dependencies of google analytics. Any idea where is the problem?
Comment #36
b10zgr commentedthe problem seems to be that the code is located on the header but because i cant find a way to put google analytics on the bottom, i figured out another way. Please let me know if i''ve done something wrong.
At google analytics->Advanced->Custom JS
There, i set @before "/*" and @after "*/" in order to comment ga("send", "pageview"); that's between.
Then simply i edited my html.html.twig file and inserted the following code @body
and now, even if i still get __ga cookie and __id cookie without consent there is no traffic on google analytics.
i was wondering if i could somehow to disable completely these cookies.
(Drupal version 8.6 with the current google_analytics)
Comment #37
rcls_ commentedIt's been a while since I actually had to develop a Drupal website, but since I was asked to add a cookie consent notice to a website that used it I decided to look for a pre-made module to help with the job. So I came across this one, but Google Analytics was still getting set after declining and I had to read the module description and issues for help. I tried implementing all of the suggestions above only to hit a
TypeErrorwherehasAgreedis undefined.Looking at the source code my guess is that the
deferattribute in loading the eu cookie consent JS file is actually making the code load AFTER analytics has done executing, sohasAgreedis not available when Analytics is called.To get around this I did what @wowDrpl suggested on #36 and commented out the
ga("send", "pageview")portion, and I added the piece of code tohtml.tpl.phpjust before the closing body element. However, I did make a minor adjustment and that was to call it only after the page is loaded using jQuery's `ready()` method. Like so:This waits for the script to load and code to execute after that. You could potentially add this to the Google Analytic's module Custom JS portion too, without the
ga("send", "pageview");line and it should work too.Comment #38
pillona commentedTo be able to use the hasAgreed() function, I relied on the observer pattern provided by the eu_cookie_compliance library
It can be done this way in order to declare a function which will be called after the cookie value has been loaded and the hasAgreed function works.
(beware, it is actually called twice, so you may want to protect your function against being called twice)
Comment #39
Shantina commentedHello everyone,
I got this task to make our web site from work GDPR compliant.
if (!Drupal.eu_cookie_compliance.hasAgreed()){ window['ga-disable-UA-XXXXXXXX-1'] = true; } ga("send", "pageview");So here we are, they gave me the drupal user and I need to find solution.
First time I work with Drupal and I try already with days to fix the issue.
As i dont understand where I have to put the above functionalities for disabling the Google Analytics ,
can some one point me where exactly I need to put the code?
I am trying in the Header Footer Scripts Settings in the configuration of Drupal,
and here where the google analytics is set, under neath I am putting this code:
But still when i try to check the web site, the report is coming back for Google Analytics:
Blocked until accepted by user: No
I would much appreciate any help cause I am already stuck with days and I need to fix this issue asap.
Thank you