-
Notifications
You must be signed in to change notification settings - Fork 917
feat(site): utm persistence #7780
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
560fe89
feat(site): utm persistence
mhartington fdad040
fix(site): failing build
mhartington b9c01d4
fix(site): harden persisted utm propagation
mhartington 1d45680
fix(site): apply persisted utms to console links
mhartington 8e586db
fix(site): preserve exact utm payloads
mhartington c0a1870
fix(site): hydrate homepage console ctas safely
mhartington 4466ed5
fix(site): narrow console cta button link props
mhartington bae6107
refactor(site): reuse homepage default utm config
mhartington 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
Some comments aren't visible on the classic Files Changed page.
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
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,94 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect } from "react"; | ||
| import { usePathname, useSearchParams } from "next/navigation"; | ||
| import { | ||
| getUtmParams, | ||
| hasUtmParams, | ||
| mergeUtmParams, | ||
| readStoredUtmParams, | ||
| writeStoredUtmParams, | ||
| } from "@/lib/utm"; | ||
|
|
||
| export function UtmPersistence() { | ||
| const pathname = usePathname(); | ||
| const searchParams = useSearchParams(); | ||
|
|
||
| useEffect(() => { | ||
| const currentUtmParams = getUtmParams(new URLSearchParams(searchParams.toString())); | ||
|
|
||
| if (hasUtmParams(currentUtmParams)) { | ||
| writeStoredUtmParams(currentUtmParams); | ||
| return; | ||
| } | ||
|
|
||
| const storedUtmParams = readStoredUtmParams(); | ||
|
|
||
| if (!hasUtmParams(storedUtmParams)) { | ||
| return; | ||
| } | ||
|
|
||
| const currentUrl = new URL(window.location.href); | ||
|
|
||
| if (!mergeUtmParams(currentUrl, storedUtmParams)) { | ||
| return; | ||
| } | ||
|
|
||
| window.history.replaceState( | ||
| window.history.state, | ||
| "", | ||
| `${currentUrl.pathname}${currentUrl.search}${currentUrl.hash}`, | ||
| ); | ||
| }, [pathname, searchParams]); | ||
|
|
||
| useEffect(() => { | ||
| function handleClick(event: MouseEvent) { | ||
| const anchor = (event.target as HTMLElement).closest<HTMLAnchorElement>( | ||
| "a[href]", | ||
| ); | ||
|
|
||
| if (!anchor) { | ||
| return; | ||
| } | ||
|
|
||
| const href = anchor.getAttribute("href"); | ||
|
|
||
| if ( | ||
| !href || | ||
| href.startsWith("#") || | ||
| href.startsWith("mailto:") || | ||
| href.startsWith("tel:") || | ||
| anchor.target === "_blank" || | ||
| anchor.hasAttribute("download") | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const storedUtmParams = readStoredUtmParams(); | ||
|
|
||
| if (!hasUtmParams(storedUtmParams)) { | ||
| return; | ||
| } | ||
|
|
||
| const targetUrl = new URL(anchor.href, window.location.href); | ||
|
|
||
| if (targetUrl.origin !== window.location.origin) { | ||
| return; | ||
| } | ||
|
|
||
| if (!mergeUtmParams(targetUrl, storedUtmParams)) { | ||
| return; | ||
| } | ||
|
|
||
| anchor.setAttribute( | ||
| "href", | ||
| `${targetUrl.pathname}${targetUrl.search}${targetUrl.hash}`, | ||
| ); | ||
| } | ||
|
|
||
| document.addEventListener("click", handleClick, true); | ||
| return () => document.removeEventListener("click", handleClick, true); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, []); | ||
|
|
||
| return null; | ||
| } | ||
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,52 @@ | ||
| export const UTM_STORAGE_KEY = "site_utm_params"; | ||
|
|
||
| export type UtmParams = Record<string, string>; | ||
|
|
||
| export function getUtmParams(searchParams: URLSearchParams): UtmParams { | ||
| const utmParams: UtmParams = {}; | ||
|
|
||
| for (const [key, value] of searchParams.entries()) { | ||
| if (key.startsWith("utm_") && value) { | ||
| utmParams[key] = value; | ||
| } | ||
| } | ||
|
|
||
| return utmParams; | ||
| } | ||
|
|
||
| export function hasUtmParams(utmParams: UtmParams) { | ||
| return Object.keys(utmParams).length > 0; | ||
| } | ||
|
|
||
| export function mergeUtmParams(url: URL, utmParams: UtmParams) { | ||
| let updated = false; | ||
|
|
||
| for (const [key, value] of Object.entries(utmParams)) { | ||
| if (!url.searchParams.has(key)) { | ||
| url.searchParams.set(key, value); | ||
| updated = true; | ||
| } | ||
| } | ||
|
|
||
| return updated; | ||
| } | ||
|
|
||
| export function readStoredUtmParams() { | ||
| const storedUtmParams = window.sessionStorage.getItem(UTM_STORAGE_KEY); | ||
|
|
||
| if (!storedUtmParams) { | ||
| return {}; | ||
| } | ||
|
|
||
| try { | ||
| return JSON.parse(storedUtmParams) as UtmParams; | ||
| } catch { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| export function writeStoredUtmParams(utmParams: UtmParams) { | ||
| if (hasUtmParams(utmParams)) { | ||
| window.sessionStorage.setItem(UTM_STORAGE_KEY, JSON.stringify(utmParams)); | ||
mhartington marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.