简体   繁体   中英

Overwrite value in document.cookie

I'm running an AB test using Google 360 and I want switch variants in my browser.

I can do this by updating a the value of cookie _gaexp in my browser. For example, this would look like:

"_gaexp: VARIANT_0"`.

If I use the EditThisCookie chrome extension and change the value to VARIANT_0 and refresh my browser, I can see my Variant 0 text in my browser.

However, I want to update this dynamically via JavaScript, (for example, let's say on click).

Looking through console, I can see I have access to document.cookie , which contains the following cookings:

"_gaexp=VARIANT_0; _ga=EXAMPLE_B; _gid=EXAMPLE_C"

If I try and update _gaexp via console to VARIANT_1, my cookie doesn't update. This is what I'm trying:

document.cookie = "_gaexp=VARIANT_1";
document.cookie;
"_gaexp=VARIANT_0; _ga=EXAMPLE_B; _gid=EXAMPLE_C, _gaexp=VARIANT_1"

It seems I'm adding a duplicate cookie by the same name, but I just want to:

  • Replace this cooking via console using JavaScript.
  • And then eventually, I would write addition JS to refresh the browser, (with the cookie updated).

What am I doing wrong?

Thank you.

It's ok, I figured it out!

Just need to change the time stamps, to remove them:

// Delete
document.cookie = '_gaexp=VARIANT_0; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=.mysite.co.uk'

// Add
document.cookie = '_gaexp=VARIANT_1; expires=Fri, 19 Jun 2020 20:47:11 UTC; path=/; domain=.mysite.co.uk'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM