11

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?

mtb
  • 1,350
  • 16
  • 32
Marco Mirabile
  • 235
  • 1
  • 5
  • 12
  • 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: http://stackoverflow.com/questions/10668292/is-there-a-setting-on-google-analytics-to-suppress-use-of-cookies-for-users-who – Tom Jun 01 '15 at 18:33
  • 3
    `window['ga-disable-UA-XXXXX-Y'] = true` works but can I delete existing __utm and _ga cookies? – Marco Mirabile Jun 01 '15 at 22:00

2 Answers2

7

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;';
}
Radoslav Stoyanov
  • 1,460
  • 5
  • 27
  • 40
  • 1
    This does not work for cookies with a different domain. Unsure if this has changed since your answer – Michael Sherris Caley Jan 08 '21 at 18:05
  • Google Analytics uses first-party cookies[1] (on your domain) so this should work. But @MichaelSherrisCaley is correct when it comes to third-party cookies. [1] https://support.google.com/analytics/answer/11397207?hl=en – Eloff Jan 22 '23 at 13:34
4

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

locropulenton
  • 4,743
  • 3
  • 32
  • 57