|
| 1 | +import { META_ADS_PERMISSION_COOKIE } from './meta-ads' |
| 2 | +import { META_TRACKING_PERMISSION_COOKIE } from './meta-conversions' |
| 3 | +import { PAID_SOCIAL_PERMISSION_COOKIE } from './paid-social-conversions' |
| 4 | +import { X_ADVERTISER_PERMISSION_COOKIE } from './x-advertiser' |
| 5 | + |
| 6 | +/** First-party eligibility marker, not a record of explicit consent. */ |
| 7 | +export const GOOGLE_ADS_PERMISSION_COOKIE = 'freebuff_google_ads_allowed' |
| 8 | +export const GOOGLE_ADS_CLICK_COOKIE = 'freebuff_google_ads_click' |
| 9 | +export const GOOGLE_ADS_CLICK_WINDOW_MS = 90 * 24 * 60 * 60 * 1000 |
| 10 | +export const GOOGLE_ADS_CLICK_KEYS = ['gclid', 'gbraid', 'wbraid'] as const |
| 11 | + |
| 12 | +export type GoogleAdsAttribution = { |
| 13 | + gclid?: string |
| 14 | + gbraid?: string |
| 15 | + wbraid?: string |
| 16 | + capturedAt: number |
| 17 | +} |
| 18 | + |
| 19 | +export function validGoogleAdsClickId(value: unknown): string | undefined { |
| 20 | + return typeof value === 'string' && /^[A-Za-z0-9._~-]{1,512}$/.test(value) |
| 21 | + ? value |
| 22 | + : undefined |
| 23 | +} |
| 24 | + |
| 25 | +/** One identifier only, in Google's priority order; no URL or user data. */ |
| 26 | +export function normalizeGoogleAdsAttribution( |
| 27 | + value: unknown, |
| 28 | + now = Date.now(), |
| 29 | +): GoogleAdsAttribution | undefined { |
| 30 | + if (!value || typeof value !== 'object' || Array.isArray(value)) return |
| 31 | + const input = value as Record<string, unknown> |
| 32 | + const capturedAt = input.capturedAt |
| 33 | + if ( |
| 34 | + typeof capturedAt !== 'number' || |
| 35 | + !Number.isFinite(capturedAt) || |
| 36 | + !Number.isFinite(now) || |
| 37 | + capturedAt > now || |
| 38 | + now - capturedAt >= GOOGLE_ADS_CLICK_WINDOW_MS |
| 39 | + ) |
| 40 | + return |
| 41 | + for (const key of GOOGLE_ADS_CLICK_KEYS) { |
| 42 | + if (input[key] !== undefined && !validGoogleAdsClickId(input[key])) return |
| 43 | + } |
| 44 | + for (const key of GOOGLE_ADS_CLICK_KEYS) { |
| 45 | + const id = validGoogleAdsClickId(input[key]) |
| 46 | + if (id) return { [key]: id, capturedAt } |
| 47 | + } |
| 48 | + return undefined |
| 49 | +} |
| 50 | + |
| 51 | +export function parseGoogleAdsClickCookie( |
| 52 | + value: string | undefined, |
| 53 | + now = Date.now(), |
| 54 | +): GoogleAdsAttribution | undefined { |
| 55 | + if (!value || value.length > 4096) return |
| 56 | + try { |
| 57 | + return normalizeGoogleAdsAttribution( |
| 58 | + JSON.parse(decodeURIComponent(value)), |
| 59 | + now, |
| 60 | + ) |
| 61 | + } catch { |
| 62 | + return undefined |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +const DECLINABLE_PERMISSION_COOKIES = [ |
| 67 | + GOOGLE_ADS_PERMISSION_COOKIE, |
| 68 | + META_TRACKING_PERMISSION_COOKIE, |
| 69 | + META_ADS_PERMISSION_COOKIE, |
| 70 | + PAID_SOCIAL_PERMISSION_COOKIE, |
| 71 | + X_ADVERTISER_PERMISSION_COOKIE, |
| 72 | +] |
| 73 | + |
| 74 | +export function googleAdsPermissionDeclined( |
| 75 | + readCookie: (name: string) => string | undefined, |
| 76 | +): boolean { |
| 77 | + return DECLINABLE_PERMISSION_COOKIES.some((name) => readCookie(name) === '0') |
| 78 | +} |
| 79 | + |
| 80 | +export function googleAdsTrackingOptedOut(headers: { |
| 81 | + get(name: string): string | null |
| 82 | +}): boolean { |
| 83 | + const dnt = headers.get('dnt')?.toLowerCase() |
| 84 | + return headers.get('sec-gpc') === '1' || dnt === '1' || dnt === 'yes' |
| 85 | +} |
0 commit comments