'Set Cloudflare Cookie Based on GET Parameter
I am attempting to set the cloudflare cookie based on the GET Parameter. In this scenario, the GET Parameter is utm_source. Demo URL (worker is currently live): https://ellasbubbles.com/product/cultured-marble-trench-drain-shower-base-36x60-and-30x60/?utm_source=test
I was able to get a head start with this documentation, but cannot seem to get this to work properly. I am using inspect element on chrome to search for cookie and it is unfound - https://www.mikestreety.co.uk/blog/using-cloudflare-workers-to-set-a-cookie-based-on-a-get-parameter-or-path/
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
async function fetchAndApply(request) {
let response = await fetch(request);
let url = new URL(request.url),
refer = url.searchParams.get('utm_source');
response = new Response(response.body, response);
if(refer) {
let partnerCookie = `source=${refer}; path=/; domain=https://ellasbubbles.com; secure; HttpOnly; SameSite=None`;
response.headers.set('Set-Cookie', partnerCookie);
}
return response;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
