Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Google Analytics Cookies and EU e-privacy law

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?

like image 949
Marco Mirabile Avatar asked Jun 01 '15 16:06

Marco Mirabile


2 Answers

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;';
}
like image 161
Radoslav Stoyanov Avatar answered Nov 08 '22 04:11

Radoslav Stoyanov


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

like image 22
locropulenton Avatar answered Nov 08 '22 06:11

locropulenton