{"version":3,"sources":["../../src/hooks/use-click-id-persistence.ts"],"sourcesContent":["import { expiringStorage } from '@shware/utils';\nimport { parseCookie } from 'cookie';\nimport { useEffect } from 'react';\nimport { keys } from '../constants/storage';\n\n// common practice ~90 days\nconst TTL_MS = 90 * 24 * 60 * 60 * 1000;\n\nfunction setCookie(name: string, value: string, ttlInMs: number) {\n  const d = new Date();\n  d.setTime(d.getTime() + ttlInMs);\n  const expires = `expires=${d.toUTCString()}`;\n  document.cookie = `${name}=${value}; ${expires}; path=/; SameSite=Lax; Secure`;\n}\n\n/** Extract the raw fbclid portion from a formatted fbc string (fb.1.ts.FBCLID) */\nfunction extractFbclid(fbc: string): string | undefined {\n  // fb.<subdomainIndex>.<creationTime>.<fbclid>\n  const parts = fbc.split('.');\n  return parts.length >= 4 ? parts.slice(3).join('.') : undefined;\n}\n\n/**\n * Persist a click-id value across cookie + localStorage.\n * - cookie: readable by third-party SDKs (e.g. Meta Pixel)\n * - localStorage (via expiringStorage): survives Safari ITP JS-cookie caps\n *\n * If only one store has the value, sync it to the other.\n */\nfunction persist(\n  cookieName: string,\n  storageKey: string,\n  cookieValue: string | undefined,\n  newValue: string | undefined\n) {\n  if (newValue) {\n    setCookie(cookieName, newValue, TTL_MS);\n    expiringStorage.setItem(storageKey, newValue, TTL_MS);\n  } else {\n    const stored = expiringStorage.getItem<string>(storageKey);\n    if (cookieValue && !stored) {\n      // cookie → localStorage: back up before ITP wipes the cookie\n      expiringStorage.setItem(storageKey, cookieValue, TTL_MS);\n    } else if (!cookieValue && stored) {\n      // localStorage → cookie: restore so third-party SDKs (Meta Pixel) can read it\n      setCookie(cookieName, stored, TTL_MS);\n    }\n  }\n}\n\n// todo: do not set tracking cookies before the user has granted consent where required.\n// reference: https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/fbp-and-fbc/#3--store-clickid\n// reference: https://watsspace.com/blog/meta-conversions-api-fbc-and-fbp-parameters/\nexport function useClickIdPersistence() {\n  useEffect(() => {\n    const params = new URLSearchParams(window.location.search);\n    const parsed = parseCookie(document.cookie);\n\n    // --- Meta fbc ---\n    const fbclid = params.get('fbclid');\n    const existingFbc = parsed._fbc ?? expiringStorage.getItem<string>(keys.fbc);\n    // Only build a new fbc if the fbclid actually changed (preserve original creationTime otherwise).\n    const fbclidChanged = fbclid && extractFbclid(existingFbc ?? '') !== fbclid;\n    const newFbc = fbclidChanged ? `fb.1.${Date.now()}.${fbclid}` : undefined;\n\n    persist('_fbc', keys.fbc, parsed._fbc, newFbc);\n\n    // Also persist raw fbclid so the server-side Conversions API fallback\n    // (fbc reconstruction from fbclid) works even after SPA navigation.\n    if (fbclid) {\n      expiringStorage.setItem(keys.fbclid, fbclid, TTL_MS);\n    }\n\n    // --- Reddit rdt_cid ---\n    const rdt_cid = params.get('rdt_cid');\n    persist('_rdt_cid', keys.rdt_cid, parsed._rdt_cid, rdt_cid ?? undefined);\n  }, []);\n}\n"],"mappings":";AAAA,SAAS,uBAAuB;AAChC,SAAS,mBAAmB;AAC5B,SAAS,iBAAiB;AAC1B,SAAS,YAAY;AAGrB,IAAM,SAAS,KAAK,KAAK,KAAK,KAAK;AAEnC,SAAS,UAAU,MAAc,OAAe,SAAiB;AAC/D,QAAM,IAAI,oBAAI,KAAK;AACnB,IAAE,QAAQ,EAAE,QAAQ,IAAI,OAAO;AAC/B,QAAM,UAAU,WAAW,EAAE,YAAY,CAAC;AAC1C,WAAS,SAAS,GAAG,IAAI,IAAI,KAAK,KAAK,OAAO;AAChD;AAGA,SAAS,cAAc,KAAiC;AAEtD,QAAM,QAAQ,IAAI,MAAM,GAAG;AAC3B,SAAO,MAAM,UAAU,IAAI,MAAM,MAAM,CAAC,EAAE,KAAK,GAAG,IAAI;AACxD;AASA,SAAS,QACP,YACA,YACA,aACA,UACA;AACA,MAAI,UAAU;AACZ,cAAU,YAAY,UAAU,MAAM;AACtC,oBAAgB,QAAQ,YAAY,UAAU,MAAM;AAAA,EACtD,OAAO;AACL,UAAM,SAAS,gBAAgB,QAAgB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