-
Notifications
You must be signed in to change notification settings - Fork 0
SURF-1415 feat(store): first-touch UTM persistence in the Surface tag #67
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
Open
paragmore
wants to merge
2
commits into
main
Choose a base branch
from
feat/first-touch-utm
base: main
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,129 @@ | ||
| import { | ||
| FIRST_TOUCH_COOKIE_NAME, | ||
| FIRST_TOUCH_COOKIE_MAX_AGE, | ||
| } from "../constants"; | ||
| import { getCookie, setCookie } from "../utils/cookies"; | ||
| import { getUrlParams } from "../utils/url"; | ||
| import { getJourneyCookieDomain } from "./journey-cookies"; | ||
|
|
||
| // Attribution params persisted from the landing URL. | ||
| const ATTRIBUTION_PARAMS = [ | ||
| "utm_source", | ||
| "utm_medium", | ||
| "utm_campaign", | ||
| "utm_term", | ||
| "utm_content", | ||
| "gclid", | ||
| "fbclid", | ||
| "li_fat_id", | ||
| "msclkid", | ||
| "ttclid", | ||
| ] as const; | ||
|
|
||
| // A visit only counts as a first touch when it carries a source/medium or an | ||
| // ad click-id. utm_content alone is commonly used to tag internal CTAs | ||
| // (e.g. ?utm_content=home_homepage-hero) and must not claim the slot. | ||
| const QUALIFYING_PARAMS = [ | ||
| "utm_source", | ||
| "utm_medium", | ||
| "gclid", | ||
| "fbclid", | ||
| "li_fat_id", | ||
| "msclkid", | ||
| "ttclid", | ||
| ] as const; | ||
|
|
||
| const MAX_VALUE_LENGTH = 256; | ||
| // Browsers cap a cookie (name + encoded value + attributes) at ~4093 bytes; | ||
| // stay well under so the write is never silently rejected. | ||
| const MAX_ENCODED_COOKIE_BYTES = 3500; | ||
| const FALLBACK_VALUE_LENGTH = 64; | ||
|
|
||
| interface FirstTouchRecord { | ||
| params: Record<string, string>; | ||
| url: string; | ||
| referrer: string; | ||
| at: string; | ||
| } | ||
|
|
||
| // Sites sometimes append utm-style params to internal links; a same-origin | ||
| // referrer means this navigation is not a real inbound touch. | ||
| function isInternalNavigation(): boolean { | ||
| if (!document.referrer) return false; | ||
|
|
||
| try { | ||
| return new URL(document.referrer).origin === window.location.origin; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Persist the landing page's attribution params in a first-party cookie so | ||
| * they survive navigation to whichever page hosts the form. Write-once: an | ||
| * existing unexpired record is never overwritten (strict first-touch, | ||
| * matching how HubSpot computes Original Source). | ||
| */ | ||
| export function captureFirstTouch(): void { | ||
| if (getCookie(FIRST_TOUCH_COOKIE_NAME)) return; | ||
| if (isInternalNavigation()) return; | ||
|
|
||
| const urlParams = getUrlParams(); | ||
| if (!QUALIFYING_PARAMS.some((key) => urlParams[key])) return; | ||
|
|
||
| const params: Record<string, string> = {}; | ||
| ATTRIBUTION_PARAMS.forEach((key) => { | ||
| const value = urlParams[key]; | ||
| if (value) params[key] = value.slice(0, MAX_VALUE_LENGTH); | ||
| }); | ||
|
|
||
| const record: FirstTouchRecord = { | ||
| params, | ||
| url: window.location.href.slice(0, MAX_VALUE_LENGTH), | ||
| referrer: document.referrer.slice(0, MAX_VALUE_LENGTH), | ||
| at: new Date().toISOString(), | ||
| }; | ||
|
|
||
| let serialized = JSON.stringify(record); | ||
| if (encodeURIComponent(serialized).length > MAX_ENCODED_COOKIE_BYTES) { | ||
| // Degrade rather than let the browser reject the oversized cookie: | ||
| // attribution params matter more than the diagnostic url/referrer. | ||
| record.url = ""; | ||
| record.referrer = ""; | ||
| Object.keys(record.params).forEach((key) => { | ||
| record.params[key] = record.params[key].slice(0, FALLBACK_VALUE_LENGTH); | ||
| }); | ||
| serialized = JSON.stringify(record); | ||
| if (encodeURIComponent(serialized).length > MAX_ENCODED_COOKIE_BYTES) return; | ||
| } | ||
|
|
||
| const options = { | ||
| maxAge: FIRST_TOUCH_COOKIE_MAX_AGE, | ||
| sameSite: "lax" as const, | ||
| }; | ||
|
|
||
| setCookie(FIRST_TOUCH_COOKIE_NAME, serialized, { | ||
| ...options, | ||
| domain: getJourneyCookieDomain(), | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| // Public-suffix hosts (e.g. *.github.io) and IP hosts reject a base-domain | ||
| // attribute outright; retry host-only so the cookie still lands. | ||
| if (!getCookie(FIRST_TOUCH_COOKIE_NAME)) { | ||
| setCookie(FIRST_TOUCH_COOKIE_NAME, serialized, options); | ||
| } | ||
| } | ||
|
|
||
| export function getFirstTouchParams(): Record<string, string> { | ||
| const raw = getCookie(FIRST_TOUCH_COOKIE_NAME); | ||
| if (!raw) return {}; | ||
|
|
||
| try { | ||
| const record = JSON.parse(raw) as FirstTouchRecord; | ||
| return record?.params && typeof record.params === "object" | ||
| ? record.params | ||
| : {}; | ||
| } catch { | ||
| return {}; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
MAX_ENCODED_COOKIE_BYTES. This branch then returns without writing any cookie, so later pages receive no first-touch params at all. That preserves the attribution loss this size guard is meant to avoid; the fallback should keep degrading or preserve a minimal qualifying attribution record instead of silently dropping the capture.