admin管理员组

文章数量:1220937

On my site, if user refuses to use cookies (according to the EU e-privacy directive), I block tracking of Google Analytics using the JavaScript ,

window['ga-disable-UA-XXXXXX-X'] = true;

With this command tracking is disabled and seems to work (if I surf on the site, Google Analytics doesn't see any activity).

But I notice that __utma, __utmb,.... cookies are still on my browser (in Chrome), so I tried to delete them with setcookie function of php:

foreach ($_COOKIE as $key => $value) {
setcookie($key, '', time()-1000,'/','.mydomain');
}

But without success! (I inserted this code after the GA monitoring JavaScript) GA cookies are ever on my browser.

So, Can I delete GA cookies?

Or Is enough blocking GA tracking for EU e-Privacy Directive?

On my site, if user refuses to use cookies (according to the EU e-privacy directive), I block tracking of Google Analytics using the JavaScript ,

window['ga-disable-UA-XXXXXX-X'] = true;

With this command tracking is disabled and seems to work (if I surf on the site, Google Analytics doesn't see any activity).

But I notice that __utma, __utmb,.... cookies are still on my browser (in Chrome), so I tried to delete them with setcookie function of php:

foreach ($_COOKIE as $key => $value) {
setcookie($key, '', time()-1000,'/','.mydomain.com');
}

But without success! (I inserted this code after the GA monitoring JavaScript) GA cookies are ever on my browser.

So, Can I delete GA cookies?

Or Is enough blocking GA tracking for EU e-Privacy Directive?

Share Improve this question edited Aug 19, 2016 at 9:00 mtb 1,37016 silver badges32 bronze badges asked Jun 1, 2015 at 16:31 Marco MirabileMarco Mirabile 2651 gold badge5 silver badges14 bronze badges 2
  • Set-cookie commands are executed before client-side javascript through a HTTP header. So the Google tracking script will set the cookie again unless you set window['ga-disable-UA-XXXXXX-X'] = true; BEFORE you load the tracking script. More info: stackoverflow.com/questions/10668292/… – Tom Commented Jun 1, 2015 at 18:33
  • 3 window['ga-disable-UA-XXXXX-Y'] = true works but can I delete existing __utm and _ga cookies? – Marco Mirabile Commented Jun 1, 2015 at 22:00
Add a comment  | 

2 Answers 2

Reset to default 8

Yes, you can delete the cookies. You just have to match the exact Path and Domain parameters as the ones used in those cookies. You can use this code and replace the parameters with yours:

function deleteCookie(name) {
    document.cookie = name + '=; Path=/; Domain=.example.com; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

The Google Analytics Debugger Chrome Extension is very helpful in testing Google Analytics code. The extension outputs the data sent to Google Analytics to the JavaScript Console Window.

Below is an example how to remove analytics.js defaults using js-cookie after disabling the default tracker.

// https://github.com/js-cookie/js-cookie
import Cookies from 'js-cookie';

const disableDefaultTracker = () => {
  // Remove the default tracker.
  if (window.ga) window.ga('remove');
  // Remove the default cookies
  // _ga is used to distinguish users.
  Cookies.remove('_ga', { path: '/', domain: document.domain });
  // _gid is used to distinguish users.
  Cookies.remove('_gid', { path: '/', domain: document.domain });
  // _gat is used to throttle request rate.
  Cookies.remove('_gat', { path: '/', domain: document.domain });
}

See https://developers.google.com/analytics/devguides/collection/analyticsjs/cookie-usage

本文标签: javascriptDelete Google Analytics Cookies and EU eprivacy lawStack Overflow