Files
supabase/packages/common/hooks/useTelemetryCookie.tsx
Ivan Vasilov 1cd1ebfc7f chire: Sort imports in all packages, cms, design-system and ui-library apps (#41610)
Sorted all imports in all packages, `cms`, `design-system` and
`ui-library` apps by running `pnpm format` on them.

All changes in this PR are done by the script.
2026-02-05 13:54:10 +01:00

38 lines
1.2 KiB
TypeScript

import { useEffect } from 'react'
import { LOCAL_STORAGE_KEYS } from '../constants'
import { getSharedTelemetryData, getTelemetryCookieOptions } from '../telemetry-utils'
interface UseTelemetryCookieProps {
enabled: boolean
}
/**
* This hook saves the initial telemetry data to a cookie so we can preserve first-touch attribution
* (e.g. `document.referrer`) until the initial pageview is emitted. If the user never
* consents, this cookie is never used.
*/
export function useTelemetryCookie({ enabled }: UseTelemetryCookieProps) {
const telemetryStorageKey = LOCAL_STORAGE_KEYS.TELEMETRY_DATA
useEffect(() => {
if (typeof window === 'undefined') return
if (!enabled) return
const cookies = document.cookie.split(';')
const cookieOptions = getTelemetryCookieOptions()
const telemetryCookie = cookies.find((cookie) =>
cookie.trim().startsWith(`${telemetryStorageKey}=`)
)
if (telemetryCookie) return
const telemetryData = getSharedTelemetryData(window.location.pathname)
const encodedData = encodeURIComponent(JSON.stringify(telemetryData))
document.cookie = `${telemetryStorageKey}=${encodedData}; ${cookieOptions}`
}, [enabled, telemetryStorageKey])
}
export default useTelemetryCookie