mirror of
https://github.com/supabase/supabase.git
synced 2026-10-12 21:05:06 +03:00
37 lines
1.2 KiB
TypeScript
37 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
|