-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add worker which allows a user's landing UTM to be carried forward in a cookie through their site journey #3
Open
alxbridge
wants to merge
1
commit into
master
Choose a base branch
from
add-utm-cookie-caching-worker
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// NOTE: A 'Cache Level' page rule set to 'Cache Everything' will | ||
// prevent private cookie cache skipping from working, as it is | ||
// applied after this worker runs. | ||
|
||
const PRIVATE_COOKIES = ['wagtailcsrftoken', 'sessionid']; | ||
const UTM_COOKIE = 'initialutm'; | ||
|
||
const STRIP_QUERYSTRING_KEYS = [ | ||
'utm_source', | ||
'utm_campaign', | ||
'utm_medium', | ||
'utm_term', | ||
'utm_content', | ||
]; | ||
|
||
addEventListener('fetch', (event) => { | ||
event.respondWith(main(event)); | ||
}); | ||
|
||
async function main(event) { | ||
const cache = caches.default; | ||
let request = event.request; | ||
|
||
// get value for setting a new UTM cookie | ||
newUtmCookieValue = getUtmCookieValue(request); | ||
|
||
request = stripIgnoredQuerystring(request); | ||
|
||
if (hasPrivateCookie(request)) { | ||
return fetch(request); | ||
} | ||
|
||
let response = await cache.match(request); | ||
if (!response) { | ||
response = await fetch(request); | ||
event.waitUntil(cache.put(request, response.clone())); | ||
} | ||
|
||
// set UTM cookie if required | ||
if (newUtmCookieValue) { | ||
const newResponse = new Response(response.body, response); | ||
newResponse.headers.append( | ||
'Set-Cookie', | ||
`${UTM_COOKIE}=${newUtmCookieValue}; path=/`, | ||
); | ||
return newResponse; | ||
} | ||
|
||
return response; | ||
} | ||
|
||
function hasPrivateCookie(request) { | ||
// Check if the request includes any of the specified 'private' cookies | ||
const cookieString = request.headers.get('Cookie'); | ||
return ( | ||
cookieString !== null && | ||
PRIVATE_COOKIES.some((item) => { | ||
return cookieString.includes(item); | ||
}) | ||
); | ||
} | ||
|
||
function hasUtmCookie(request) { | ||
// Check if the request already has the UTM cookie set | ||
const cookieString = request.headers.get('Cookie'); | ||
return cookieString !== null && cookieString.includes(UTM_COOKIE); | ||
} | ||
|
||
function getUtmCookieValue(request) { | ||
// If there's no existing UTM cookie, and the querystring contains a UTM key, | ||
// return the querystring to save into a new UTM cookie | ||
if (!hasUtmCookie(request)) { | ||
const url = new URL(request.url); | ||
if (STRIP_QUERYSTRING_KEYS.some((key) => url.searchParams.get(key))) { | ||
return url.search; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't you return only the utm searchParams instead of the whole url.search? I have no idea how this cookie will be used so it might not matter too much. |
||
} | ||
} | ||
return null; | ||
} | ||
|
||
function stripIgnoredQuerystring(request) { | ||
// Return a request with specified querystring keys stripped out | ||
const url = new URL(request.url); | ||
const stripKeys = STRIP_QUERYSTRING_KEYS.filter((v) => | ||
url.searchParams.has(v), | ||
); | ||
|
||
if (stripKeys.length) { | ||
stripKeys.forEach((v) => url.searchParams.delete(v)); | ||
|
||
return new Request(url, request); | ||
} | ||
return request; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.