|
| 1 | +export const UTM_STORAGE_KEY = "blog_utm_params"; |
| 2 | +export const CONSOLE_HOST = "console.prisma.io"; |
| 3 | + |
| 4 | +export type UtmParams = Record<string, string>; |
| 5 | + |
| 6 | +function sanitizeUtmParams(input: unknown): UtmParams { |
| 7 | + if (!input || typeof input !== "object" || Array.isArray(input)) { |
| 8 | + return {}; |
| 9 | + } |
| 10 | + |
| 11 | + return Object.fromEntries( |
| 12 | + Object.entries(input).filter( |
| 13 | + ([key, value]) => |
| 14 | + key.startsWith("utm_") && typeof value === "string" && value.length > 0, |
| 15 | + ), |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +export function getUtmParams(searchParams: URLSearchParams): UtmParams { |
| 20 | + const utmParams: UtmParams = {}; |
| 21 | + |
| 22 | + for (const [key, value] of searchParams.entries()) { |
| 23 | + if (key.startsWith("utm_") && value) { |
| 24 | + utmParams[key] = value; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + return utmParams; |
| 29 | +} |
| 30 | + |
| 31 | +export function hasUtmParams(utmParams: UtmParams) { |
| 32 | + return Object.keys(utmParams).length > 0; |
| 33 | +} |
| 34 | + |
| 35 | +export function syncUtmParams(url: URL, utmParams: UtmParams) { |
| 36 | + let updated = false; |
| 37 | + |
| 38 | + for (const key of Array.from(url.searchParams.keys())) { |
| 39 | + if (key.startsWith("utm_") && !(key in utmParams)) { |
| 40 | + url.searchParams.delete(key); |
| 41 | + updated = true; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + for (const [key, value] of Object.entries(utmParams)) { |
| 46 | + if (url.searchParams.get(key) !== value) { |
| 47 | + url.searchParams.set(key, value); |
| 48 | + updated = true; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return updated; |
| 53 | +} |
| 54 | + |
| 55 | +export function readStoredUtmParams() { |
| 56 | + if (typeof window === "undefined") { |
| 57 | + return {}; |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + const storedUtmParams = window.sessionStorage.getItem(UTM_STORAGE_KEY); |
| 62 | + |
| 63 | + if (!storedUtmParams) { |
| 64 | + return {}; |
| 65 | + } |
| 66 | + |
| 67 | + return sanitizeUtmParams(JSON.parse(storedUtmParams)); |
| 68 | + } catch { |
| 69 | + return {}; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export function writeStoredUtmParams(utmParams: UtmParams) { |
| 74 | + if (typeof window === "undefined") { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const validUtmParams = sanitizeUtmParams(utmParams); |
| 79 | + |
| 80 | + if (!hasUtmParams(validUtmParams)) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + try { |
| 85 | + window.sessionStorage.setItem(UTM_STORAGE_KEY, JSON.stringify(validUtmParams)); |
| 86 | + } catch { |
| 87 | + // Ignore storage failures in restricted environments. |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export function clearStoredUtmParams() { |
| 92 | + if (typeof window === "undefined") { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + window.sessionStorage.removeItem(UTM_STORAGE_KEY); |
| 98 | + } catch { |
| 99 | + // Ignore storage failures in restricted environments. |
| 100 | + } |
| 101 | +} |
0 commit comments