0

I want to delete a cookie named "_ga" which is a cookie of Google Analytics programatically. * jQuery is not an option this time.

I understand we can delete a cookie by 1) setting a past expiration date 2) setting max-age=0. (reference)

However, when I tried on Chrome, it doesn't delete the cookie. To test its behavior, I set another cookie with the same name with null value.

document.cookie = "_ga=;";

I assume it will overwrite the existing cookie with the same name, but both cookies exist.

>document.cookie
<- "_ga=GA1.3.1150830279.1524399211; _ga="

Is it an expected behavior? If yes, how can I delete the cookies?

Steve
  • 29
  • 3

1 Answers1

1

Go to Application Tab inside chrome developer tools. Go to Cookies. Get the domain name for the _ga cookie. Now reset that with JS

document.cookie = "_ga=;domain=<domainName>"

When you set it without specifying the domain, it would be setting including the subdomain e.g. www.domain.com where as google would be setting it on domain.com

GSSwain
  • 5,787
  • 2
  • 19
  • 24