magento2: Google Universal Analytics does not track when Cookie Restriction is enabled

Steps to reproduce

  1. Install Magento v2.1 from composer.
  2. Configure the Admin Panel with a valid Google analytics ID and set mode to ‘Universal Analytics’ under ‘Stores => Configuration => Sales => Google Api => Google Analytics’
  3. Set cookie restriction mode to ‘yes’ under 'Stores => Configuration => General => Web => Default Cookie Settings => Cookie Restriction Mode.
  4. Clear all caches
  5. View any front end page
  6. Click the ‘Allow Cookies’ in the cookie restriction banner
  7. Wait for page to reload and use Google Analytics debug plugin to look for beacons sent to GA. (https://chrome.google.com/webstore/detail/google-analytics-debugger/jnkmfdileelhofjcijamephohjechhna?hl=en) or view live tracking results in the Google analytics control panel.

Expected result

  1. Tracking Beacons are sent to GA (visible in the browser console is using the GA dEbug tool ) e.g.
Initializing Google Analytics.
analytics_debug.js:9 Running command: ga("create", "UA-<yourGAid>", "auto")
analytics_debug.js:9 Creating new tracker: t0
analytics_debug.js:9 New visitor. Generating new clientId
analytics_debug.js:9 Auto cookieDomain found: "sitedomain.com"
analytics_debug.js:9 Running command: ga("send", "pageview")
analytics_debug.js:9 Setting throttling cookie: "_gat"
analytics_debug.js:9 
Sent beacon:
v=1&_v=j44d&a=1955779100&t=pageview&_s=1&dl=http%3A%2F%2Fsiteurl%2F&ul=en-us&de=UTF-8&dt=Home%20Page&sd=24-bit&sr=1050x1680&vp=1050x1050&je=0&fl=22.0%20r0&_u=AEAAAEABI~&jid=1985252792&cid=1406517672.1468336046&tid=UA-<yourGAid>&_r=1&z=1708727464


analytics_debug.js:9 _j1              (&jid) 1985252792
analytics_debug.js:9 adSenseId        (&a)   1955779100
analytics_debug.js:9 apiVersion       (&v)   1
analytics_debug.js:9 clientId         (&cid) 1406517672.1468336046
analytics_debug.js:9 encoding         (&de)  UTF-8
analytics_debug.js:9 flashVersion     (&fl)  22.0 r0
analytics_debug.js:9 hitType          (&t)   pageview
analytics_debug.js:9 javaEnabled      (&je)  0
analytics_debug.js:9 language         (&ul)  en-us
analytics_debug.js:9 location         (&dl)  http://####SITEURL###/
analytics_debug.js:9 screenColors     (&sd)  24-bit
analytics_debug.js:9 screenResolution (&sr)  1050x1680
analytics_debug.js:9 title            (&dt)  Home Page
analytics_debug.js:9 trackingId       (&tid) UA-<yourGAid>
analytics_debug.js:9 viewportSize     (&vp)  1050x1050
analytics_debug.js:9 Registered new plugin: ga(provide, "render", Function)

and

Live Page tracking statistics to change in Google Analytics Web Console

Actual result

  1. No Beacon sent, shown in debug console or recorded in Google analytics.

NB. This case is a duplicate of a closed but unresolved case: https://github.com/magento/magento2/issues/2518

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 32 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Confirmed here. We need the cookie allow feature thats why using true doesn’t work for us. The root cause for this problem lies in module-cookie/view/frontend/web/js/notices.js, where the cookieValue is not saved as a JSON string. After confirming cookies the value just gets saved as “[object Object]”, which cannot be interpreted by _getAcceptedSaveCookiesWebsites in module-cookie/Helper/Cookie.php

auswahl_052 auswahl_053

A quick fix is to copy the file: vendor/magento/module-google-analytics/view/frontend/templates/ga.phtml to your own theme: app/design/frontend/{Vendor}/{theme}/Magento_GoogleAnalytics/templates/ga.phtml

And then replace this line:

<?php if (!$this->helper('Magento\Cookie\Helper\Cookie')->isUserNotAllowSaveCookie()): ?>

with:

<?php if (true): // !$this->helper('Magento\Cookie\Helper\Cookie')->isUserNotAllowSaveCookie()): ?>

Watch out, this will always output the GA code, even if the user hasn’t accepted the cookie message. I’m ok with this, but you should probably check with your legal departement if this is ok to do or not.

Hi Guys i have edited a little bit the fix of @hostep to be 100% sure that your Customers will not see Google Analytics if they have not accepted cookies. Unfortunately this helper doesn’t work: $this->helper('Magento\Cookie\Helper\Cookie')->isUserNotAllowSaveCookie() And the result is always “true” despite you have accepted or not cookies.

An easy fix is to copy the file: vendor/magento/module-google-analytics/view/frontend/templates/ga.phtml to your own theme: app/design/frontend/{Vendor}/{theme}/Magento_GoogleAnalytics/templates/ga.phtml

And then replace this line:

<?php if (!$this->helper('Magento\Cookie\Helper\Cookie')->isUserNotAllowSaveCookie()): ?> with:

<?php if (isset($_COOKIE["user_allowed_save_cookie"])): // !$this->helper('Magento\Cookie\Helper\Cookie')->isUserNotAllowSaveCookie()): ?>

Under google analytics <script>...</script>, inside php if condition, you can also put other external javascript scripts that use cookies, for example a livechat that need the cookie policy accepted.

@SchumacherFM: can you please stop spreading such news, this is definitely not true. 2.1 still receives bug fixes, just look at what’s going on on the 2.1.8-preview branch, you’ll see a lot of bug fixes. They just need to start releasing it very soon as it has been almost 4 months since the last bugfix release…

It will never be fixed in any future 2.1 release. 2.1 receives only security fixes. You shall upgrade to 2.2 … which has lots of breaking changes but it’s worth.