@aranova/tracking-react 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/phone.js CHANGED
@@ -29,6 +29,79 @@ __export(phone_utils_exports, {
29
29
  });
30
30
  module.exports = __toCommonJS(phone_utils_exports);
31
31
 
32
+ // ../tracking-core/src/consent.ts
33
+ var CONSENT_STATE_KEY = "consent_state";
34
+ var grantedListeners = /* @__PURE__ */ new Set();
35
+ function onConsentGranted(listener) {
36
+ grantedListeners.add(listener);
37
+ return () => {
38
+ grantedListeners.delete(listener);
39
+ };
40
+ }
41
+ function getConsentState() {
42
+ if (typeof window === "undefined") return "pending";
43
+ try {
44
+ const storedState = window.localStorage.getItem(CONSENT_STATE_KEY);
45
+ if (storedState === "granted" || storedState === "denied") return storedState;
46
+ } catch {
47
+ }
48
+ return "pending";
49
+ }
50
+
51
+ // ../tracking-core/src/gtag.ts
52
+ var SEND_TO_RE = /^AW-[A-Za-z0-9]+\/[A-Za-z0-9_-]+$/;
53
+ function isValidSendTo(sendTo) {
54
+ return SEND_TO_RE.test(sendTo);
55
+ }
56
+ function fireGtagConversion(input) {
57
+ if (typeof window === "undefined" || typeof window.gtag !== "function") return false;
58
+ if (!isValidSendTo(input.sendTo)) return false;
59
+ const params = { send_to: input.sendTo };
60
+ if (input.value != null) params.value = input.value;
61
+ if (input.currency) params.currency = input.currency;
62
+ if (input.transactionId) params.transaction_id = input.transactionId;
63
+ try {
64
+ window.gtag("event", "conversion", params);
65
+ return true;
66
+ } catch {
67
+ return false;
68
+ }
69
+ }
70
+
71
+ // ../tracking-core/src/resources/conversion-firing.ts
72
+ var DEDUP_PREFIX = "_aranova_conv_";
73
+ var pendingQueue = [];
74
+ function dedupKey(input) {
75
+ return `${DEDUP_PREFIX}${input.transactionId ?? ""}:${input.sendTo}`;
76
+ }
77
+ function alreadyFired(input) {
78
+ if (!input.transactionId || typeof window === "undefined") return false;
79
+ try {
80
+ return window.sessionStorage.getItem(dedupKey(input)) !== null;
81
+ } catch {
82
+ return false;
83
+ }
84
+ }
85
+ function markFired(input) {
86
+ if (!input.transactionId || typeof window === "undefined") return;
87
+ try {
88
+ window.sessionStorage.setItem(dedupKey(input), "1");
89
+ } catch {
90
+ }
91
+ }
92
+ function fireOnce(input) {
93
+ if (alreadyFired(input)) return;
94
+ if (fireGtagConversion(input)) markFired(input);
95
+ }
96
+ function flushPendingConversions() {
97
+ if (getConsentState() !== "granted") return;
98
+ while (pendingQueue.length > 0) {
99
+ const input = pendingQueue.shift();
100
+ if (input) fireOnce(input);
101
+ }
102
+ }
103
+ if (typeof window !== "undefined") onConsentGranted(flushPendingConversions);
104
+
32
105
  // ../tracking-core/src/session.ts
33
106
  var SESSION_IDLE_MS = 30 * 60 * 1e3;
34
107
 
package/dist/phone.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/phone-utils.ts","../../tracking-core/src/session.ts","../../tracking-core/src/events/page-view.ts","../../tracking-core/src/events/cta-click.ts","../../tracking-core/src/events/sdk-heartbeat.ts","../../tracking-core/src/events/form-start.ts","../../tracking-core/src/events/form-submit.ts","../../tracking-core/src/events/multi-page-session.ts","../../tracking-core/src/events/phone-click.ts","../../tracking-core/src/events/scroll-depth.ts","../../tracking-core/src/events/specific-page-visit.ts","../../tracking-core/src/events/time-on-site.ts","../../tracking-core/src/events/registry.ts","../../tracking-core/src/resources/sales/schema.ts","../../tracking-core/src/phone.ts","../../tracking-core/src/phone-field.ts"],"sourcesContent":["// `@aranova/tracking-react/phone` — the isomorphic phone utils only (NO React,\n// NO 'use client'), so server/Node code can normalize/format too. The React\n// hook + component live on the package root.\nexport {\n DEFAULT_PHONE_COUNTRY,\n formatPhone,\n formatPhoneAsTyped,\n parsePhone,\n phoneField,\n toE164,\n} from \"../../tracking-core/src/index\";\nexport type {\n CountryCode,\n ParsedPhone,\n PhoneConfig,\n PhoneDisplayFormat,\n TrackedField,\n} from \"../../tracking-core/src/index\";\n","// Visitor + session identity for the tracking SDK.\n//\n// Visitor: persistent localStorage UUID, never expires until the user clears\n// browser storage. Used for cross-session correlation.\n//\n// Session: rolling 30-minute idle window. Regenerated when more than\n// SESSION_IDLE_MS has passed since the last event. Matches the behavior of\n// GA4, PostHog, Mixpanel, etc., so analytics is comparable.\n\n/**\n * localStorage key for the persistent visitor id.\n */\nexport const VISITOR_STORAGE_KEY = \"aranova_tracking_visitor\";\n\n/**\n * localStorage key for the rolling session id state.\n */\nexport const SESSION_STORAGE_KEY = \"aranova_tracking_session\";\n\n/**\n * Idle window before a new session id is created.\n */\nexport const SESSION_IDLE_MS = 30 * 60 * 1000;\n\n/**\n * Serialized session state stored in localStorage.\n */\nexport interface StoredSession {\n /** Client-generated session UUID. */\n id: string;\n /** Unix timestamp in milliseconds for the most recent event/session touch. */\n last_event_at: number;\n}\n\nfunction safeUuid(): string {\n if (typeof crypto !== \"undefined\" && typeof crypto.randomUUID === \"function\")\n return crypto.randomUUID();\n // Fallback for ancient browsers — not cryptographically perfect but unique enough.\n return `${Date.now().toString(16)}-${Math.random().toString(16).slice(2)}-${Math.random().toString(16).slice(2)}`;\n}\n\nfunction readLocalStorage(key: string): string | null {\n try {\n return window.localStorage.getItem(key);\n } catch {\n return null;\n }\n}\n\nfunction writeLocalStorage(key: string, value: string): void {\n try {\n window.localStorage.setItem(key, value);\n } catch {\n // Storage may be denied (private mode, blocked cookies, quota). Caller is\n // responsible for degrading gracefully.\n }\n}\n\n/**\n * Return the persistent visitor id for this browser profile.\n *\n * Creates and stores a new id when one does not already exist. During SSR,\n * returns an ephemeral id because browser storage is unavailable.\n */\nexport function getVisitorId(): string {\n if (typeof window === \"undefined\") return safeUuid();\n\n const existing = readLocalStorage(VISITOR_STORAGE_KEY);\n if (existing && existing.length > 0) return existing;\n\n const fresh = safeUuid();\n writeLocalStorage(VISITOR_STORAGE_KEY, fresh);\n return fresh;\n}\n\n/**\n * Result from `getOrRotateSessionId()`.\n */\nexport interface SessionIdResult {\n /** Current session id. */\n id: string;\n /** Whether this call created a new session. */\n isNew: boolean;\n}\n\n/**\n * Return the current session id, rotating it after the idle window expires.\n *\n * Also refreshes `last_event_at` for active sessions.\n */\nexport function getOrRotateSessionId(now: number = Date.now()): SessionIdResult {\n if (typeof window === \"undefined\") return { id: safeUuid(), isNew: true };\n\n const raw = readLocalStorage(SESSION_STORAGE_KEY);\n if (raw) {\n try {\n const parsed = JSON.parse(raw) as Partial<StoredSession>;\n if (typeof parsed.id === \"string\" && typeof parsed.last_event_at === \"number\") {\n if (now - parsed.last_event_at <= SESSION_IDLE_MS) {\n const refreshed: StoredSession = { id: parsed.id, last_event_at: now };\n writeLocalStorage(SESSION_STORAGE_KEY, JSON.stringify(refreshed));\n return { id: parsed.id, isNew: false };\n }\n }\n } catch {\n // Fall through to a fresh session.\n }\n }\n\n const fresh: StoredSession = { id: safeUuid(), last_event_at: now };\n writeLocalStorage(SESSION_STORAGE_KEY, JSON.stringify(fresh));\n return { id: fresh.id, isNew: true };\n}\n\n/**\n * Clear visitor and session identity from localStorage.\n *\n * Intended for tests, debugging, and explicit user reset flows.\n */\nexport function resetTrackingIdentity(): void {\n if (typeof window === \"undefined\") return;\n try {\n window.localStorage.removeItem(VISITOR_STORAGE_KEY);\n window.localStorage.removeItem(SESSION_STORAGE_KEY);\n } catch {\n // ignored\n }\n}\n","import { z } from \"zod\";\n\n/**\n * Metadata for the automatic `page_view` event.\n *\n * The SDK emits this on initial load, SPA route changes, and bfcache restores.\n * Consumers do not call `trackEvent('page_view', ...)`; registering\n * `automatic: { page_view: {} }` enables the SDK-owned trigger.\n */\nexport const pageViewMetadataSchema = z\n .object({\n page: z\n .object({\n title: z.string().nullable(),\n path: z.string(),\n search: z.string(),\n hash: z.string(),\n })\n .strict(),\n referrer: z.string().nullable(),\n // `.nullable().optional()` — absent (undefined) OR explicit null OR a\n // real viewport object. Mirrors Pydantic's `_Viewport | None = None`\n // on the backend side so the drift test stays clean.\n viewport: z\n .object({\n w: z.number(),\n h: z.number(),\n })\n .strict()\n .nullable()\n .optional(),\n })\n .strict();\n\nexport type PageViewMetadata = z.infer<typeof pageViewMetadataSchema>;\n\n/**\n * Registration config for automatic `page_view`.\n *\n * `page_view` is required in every trigger registry and currently has no\n * options. Use `{ page_view: {} }`.\n */\nexport const pageViewConfigSchema = z.object({}).strict();\nexport type PageViewConfig = z.infer<typeof pageViewConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for a manually fired `cta_click` event.\n *\n * Use this for non-phone calls to action such as directions, appointment\n * buttons, downloads, or external booking links.\n */\nexport const ctaClickMetadataSchema = z\n .object({\n cta_name: z.string(),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n section: z.string().nullable().optional(),\n destination_url: z.string().nullable().optional(),\n })\n .strict();\n\nexport type CtaClickMetadata = z.infer<typeof ctaClickMetadataSchema>;\n\n/**\n * Registration config for `cta_click`.\n *\n * This event is manual-only and currently has no registration options.\n */\nexport const ctaClickConfigSchema = z.object({}).strict();\nexport type CtaClickConfig = z.infer<typeof ctaClickConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Registered trigger names reported by the SDK heartbeat.\n */\nexport const sdkHeartbeatTriggersSchema = z\n .object({\n automatic: z.array(z.string()),\n manual: z.array(z.string()),\n })\n .strict();\n\n/**\n * Metadata for the SDK-internal `sdk_heartbeat` event.\n *\n * The SDK fires this once per new session so the dashboard can show which SDK\n * version, install surface, and trigger registry a client site is running.\n * Consumers do not manually register or fire this event.\n */\nexport const sdkHeartbeatMetadataSchema = z\n .object({\n sdk_version: z.string(),\n package_name: z.string().nullable(),\n surface: z.enum([\"next\", \"react\", \"script\"]),\n triggers: sdkHeartbeatTriggersSchema,\n trigger_config: z.record(z.string(), z.record(z.string(), z.unknown())).nullable().optional(),\n configured_gtag_ids: z.record(z.string(), z.string()).nullable().optional(),\n })\n .strict();\n\nexport type SdkHeartbeatMetadata = z.infer<typeof sdkHeartbeatMetadataSchema>;\n\n/**\n * Internal registration config for `sdk_heartbeat`.\n *\n * This event has no consumer-facing options.\n */\nexport const sdkHeartbeatConfigSchema = z.object({}).strict();\nexport type SdkHeartbeatConfig = z.infer<typeof sdkHeartbeatConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for the automatic `form_start` event.\n *\n * The SDK emits this once per form when the visitor first focuses a field.\n */\nexport const formStartMetadataSchema = z\n .object({\n form: z\n .object({\n id: z.string(),\n action: z.string().nullable(),\n })\n .strict(),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type FormStartMetadata = z.infer<typeof formStartMetadataSchema>;\n\n/**\n * Registration config for automatic `form_start`.\n *\n * Use `selector` to narrow which forms can trigger the event. When omitted,\n * the SDK observes all `<form>` elements.\n */\nexport const formStartConfigSchema = z\n .object({\n selector: z.string().optional(),\n })\n .strict();\n\nexport type FormStartConfig = z.infer<typeof formStartConfigSchema>;\n","import { z } from \"zod\";\n\n// form_submit is manual — the SDK never auto-fires this. Consumer code calls\n// `tracking.trackEvent('form_submit', { form, page })` from their own submit\n// handler. Registering it enables the type-level permission; omitting it\n// turns manual calls into a compile error.\n//\n// The optional `fields` array captures submitted form field metadata and JSON\n// values. Consumers explicitly build the fields array themselves so they\n// control exactly what is sent.\n\n/**\n * JSON-serializable value accepted by `form_submit.fields[].value`.\n *\n * This intentionally excludes `undefined`, functions, symbols, `Date`\n * instances, and non-finite numbers. Values are stored in PostgreSQL JSONB, so\n * consumers should send only data that has a stable JSON representation.\n */\nexport type JsonValue =\n | string\n | number\n | boolean\n | null\n | JsonValue[]\n | { [key: string]: JsonValue };\n\nexport const jsonValueSchema: z.ZodType<JsonValue> = z.lazy(() =>\n z.union([\n z.string(),\n z.number().finite(),\n z.boolean(),\n z.null(),\n z.array(jsonValueSchema),\n z.record(jsonValueSchema),\n ]),\n);\n\n/**\n * Metadata for a manually fired `form_submit` event.\n *\n * Register the event with `manual: { form_submit: {} }`, then call\n * `trackEvent('form_submit', metadata)` from the host site's submit handler.\n *\n * `fields` is optional. If present, each field value must be JSON-serializable\n * and should be explicitly allowlisted by the integration. Do not send names,\n * emails, visitor phone numbers, addresses, payment data, medical details,\n * passwords, file contents, or free-text messages.\n *\n * @example\n * ```ts\n * tracking.trackEvent('form_submit', {\n * form: {\n * id: 'lead-form',\n * action: '/api/lead',\n * fields: [\n * {\n * name: 'service_interest',\n * type: 'select',\n * label: 'Service interest',\n * value: 'teeth_whitening',\n * },\n * ],\n * },\n * page: { path: window.location.pathname },\n * });\n * ```\n */\nexport const formSubmitMetadataSchema = z\n .object({\n form: z\n .object({\n id: z.string(),\n action: z.string().nullable(),\n fields: z\n .array(\n z\n .object({\n name: z.string(),\n type: z.string(),\n label: z.string().nullable(),\n value: jsonValueSchema,\n })\n .strict(),\n )\n .optional(),\n })\n .strict(),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type FormSubmitMetadata = z.infer<typeof formSubmitMetadataSchema>;\n\n/**\n * Registration config for `form_submit`.\n *\n * This event is manual-only and currently has no registration options. The\n * empty object enables typed `trackEvent('form_submit', ...)` calls.\n */\nexport const formSubmitConfigSchema = z.object({}).strict();\nexport type FormSubmitConfig = z.infer<typeof formSubmitConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for the automatic `multi_page_session` event.\n *\n * Fired when the visitor reaches the configured distinct-page threshold in a\n * single tracking session.\n */\nexport const multiPageSessionMetadataSchema = z\n .object({\n page_count: z.number().int().min(2),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type MultiPageSessionMetadata = z.infer<typeof multiPageSessionMetadataSchema>;\n\n/**\n * Registration config for automatic `multi_page_session`.\n */\nexport const multiPageSessionConfigSchema = z\n .object({\n pageThreshold: z.number().int().min(2),\n })\n .strict();\n\nexport type MultiPageSessionConfig = z.infer<typeof multiPageSessionConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for a manually fired `phone_click` event.\n *\n * `phone_number` should be the business phone number from the clicked `tel:`\n * link, not a visitor-entered phone number. `section` can distinguish header,\n * footer, hero, or contact-page links.\n */\nexport const phoneClickMetadataSchema = z\n .object({\n phone_number: z.string(),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n section: z.string().nullable().optional(),\n })\n .strict();\n\nexport type PhoneClickMetadata = z.infer<typeof phoneClickMetadataSchema>;\n\n/**\n * Registration config for `phone_click`.\n *\n * This event is manual-only and currently has no registration options.\n */\nexport const phoneClickConfigSchema = z.object({}).strict();\nexport type PhoneClickConfig = z.infer<typeof phoneClickConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for the automatic `scroll_depth` event.\n *\n * Fired once per configured threshold per page.\n */\nexport const scrollDepthMetadataSchema = z\n .object({\n depth_percent: z.number().int().min(1).max(100),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type ScrollDepthMetadata = z.infer<typeof scrollDepthMetadataSchema>;\n\n/**\n * Registration config for automatic `scroll_depth`.\n *\n * `thresholds` are integer percentages from 1 to 100.\n */\nexport const scrollDepthConfigSchema = z\n .object({\n thresholds: z.array(z.number().int().min(1).max(100)).min(1),\n })\n .strict();\n\nexport type ScrollDepthConfig = z.infer<typeof scrollDepthConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Canonical page intent names supported by `specific_page_visit`.\n */\nexport const SPECIFIC_PAGE_NAMES = [\n \"contact_page\",\n \"about_page\",\n \"services_page\",\n \"booking_page\",\n \"location_page\",\n \"pricing_page\",\n \"faq_page\",\n \"testimonials_page\",\n] as const;\n\nexport type SpecificPageName = (typeof SPECIFIC_PAGE_NAMES)[number];\n\nexport const specificPageNameSchema = z.enum(SPECIFIC_PAGE_NAMES);\n\n/**\n * Metadata for the automatic `specific_page_visit` event.\n *\n * The SDK emits this when the current pathname matches one of the configured\n * named page patterns.\n */\nexport const specificPageVisitMetadataSchema = z\n .object({\n page_name: specificPageNameSchema,\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type SpecificPageVisitMetadata = z.infer<typeof specificPageVisitMetadataSchema>;\n\n/**\n * Registration config for automatic `specific_page_visit`.\n *\n * Each page entry pairs a semantic `name` with a `RegExp` that matches the\n * pathname. Use this instead of hard-coding path regexes downstream.\n */\nexport const specificPageVisitConfigSchema = z\n .object({\n pages: z\n .array(\n z\n .object({\n name: specificPageNameSchema,\n pathPattern: z.custom<RegExp>((value) => value instanceof RegExp, {\n message: \"pathPattern must be a RegExp\",\n }),\n })\n .strict(),\n )\n .min(1),\n })\n .strict();\n\nexport type SpecificPageVisitConfig = z.infer<typeof specificPageVisitConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for the automatic `time_on_site` event.\n *\n * The SDK starts a visibility-aware timer and fires once when visible\n * engagement crosses the configured threshold.\n */\nexport const timeOnSiteMetadataSchema = z\n .object({\n duration_ms: z.number().int().nonnegative(),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type TimeOnSiteMetadata = z.infer<typeof timeOnSiteMetadataSchema>;\n\n/**\n * Registration config for automatic `time_on_site`.\n */\nexport const timeOnSiteConfigSchema = z\n .object({\n thresholdSeconds: z.number().int().positive(),\n })\n .strict();\nexport type TimeOnSiteConfig = z.infer<typeof timeOnSiteConfigSchema>;\n","import type { z } from \"zod\";\n\nimport {\n ctaClickConfigSchema,\n ctaClickMetadataSchema,\n type CtaClickConfig,\n type CtaClickMetadata,\n} from \"./cta-click\";\nimport {\n sdkHeartbeatConfigSchema,\n sdkHeartbeatMetadataSchema,\n type SdkHeartbeatConfig,\n type SdkHeartbeatMetadata,\n} from \"./sdk-heartbeat\";\nimport {\n formStartConfigSchema,\n formStartMetadataSchema,\n type FormStartConfig,\n type FormStartMetadata,\n} from \"./form-start\";\nimport {\n formSubmitConfigSchema,\n formSubmitMetadataSchema,\n type FormSubmitConfig,\n type FormSubmitMetadata,\n} from \"./form-submit\";\nimport {\n multiPageSessionConfigSchema,\n multiPageSessionMetadataSchema,\n type MultiPageSessionConfig,\n type MultiPageSessionMetadata,\n} from \"./multi-page-session\";\nimport {\n pageViewConfigSchema,\n pageViewMetadataSchema,\n type PageViewConfig,\n type PageViewMetadata,\n} from \"./page-view\";\nimport {\n phoneClickConfigSchema,\n phoneClickMetadataSchema,\n type PhoneClickConfig,\n type PhoneClickMetadata,\n} from \"./phone-click\";\nimport {\n scrollDepthConfigSchema,\n scrollDepthMetadataSchema,\n type ScrollDepthConfig,\n type ScrollDepthMetadata,\n} from \"./scroll-depth\";\nimport {\n specificPageVisitConfigSchema,\n specificPageVisitMetadataSchema,\n type SpecificPageVisitConfig,\n type SpecificPageVisitMetadata,\n} from \"./specific-page-visit\";\nimport {\n timeOnSiteConfigSchema,\n timeOnSiteMetadataSchema,\n type TimeOnSiteConfig,\n type TimeOnSiteMetadata,\n} from \"./time-on-site\";\n\n// Event kind — automatic events are fired by the SDK itself when their\n// client-side signal fires (page_view on navigation, time_on_site on timer,\n// etc). Manual events are only fireable via explicit consumer code.\nexport type EventKind = \"automatic\" | \"manual\";\n\n// The registry is the single client-side source of truth for \"what events\n// exist, what shape does their metadata take, and what kind are they\". The\n// backend mirror lives in apps/api/src/schemas/tracking_events.py and is\n// kept in sync via the drift test in apps/api/tests/test_event_schema_drift.py.\nexport const EVENT_REGISTRY = {\n // --- automatic triggers ---\n page_view: {\n kind: \"automatic\",\n metadataSchema: pageViewMetadataSchema,\n configSchema: pageViewConfigSchema,\n },\n time_on_site: {\n kind: \"automatic\",\n metadataSchema: timeOnSiteMetadataSchema,\n configSchema: timeOnSiteConfigSchema,\n },\n specific_page_visit: {\n kind: \"automatic\",\n metadataSchema: specificPageVisitMetadataSchema,\n configSchema: specificPageVisitConfigSchema,\n },\n scroll_depth: {\n kind: \"automatic\",\n metadataSchema: scrollDepthMetadataSchema,\n configSchema: scrollDepthConfigSchema,\n },\n multi_page_session: {\n kind: \"automatic\",\n metadataSchema: multiPageSessionMetadataSchema,\n configSchema: multiPageSessionConfigSchema,\n },\n form_start: {\n kind: \"automatic\",\n metadataSchema: formStartMetadataSchema,\n configSchema: formStartConfigSchema,\n },\n // --- SDK-internal automatic (not consumer-configurable) ---\n sdk_heartbeat: {\n kind: \"automatic\",\n metadataSchema: sdkHeartbeatMetadataSchema,\n configSchema: sdkHeartbeatConfigSchema,\n },\n // --- manual triggers ---\n form_submit: {\n kind: \"manual\",\n metadataSchema: formSubmitMetadataSchema,\n configSchema: formSubmitConfigSchema,\n },\n phone_click: {\n kind: \"manual\",\n metadataSchema: phoneClickMetadataSchema,\n configSchema: phoneClickConfigSchema,\n },\n cta_click: {\n kind: \"manual\",\n metadataSchema: ctaClickMetadataSchema,\n configSchema: ctaClickConfigSchema,\n },\n} as const;\n\n/**\n * Name of any event known to the tracking SDK.\n */\nexport type EventName = keyof typeof EVENT_REGISTRY;\n\n/**\n * Event names that are fired by the SDK when their configured signal occurs.\n *\n * Automatic events are not accepted by the typed `trackEvent()` API.\n */\nexport type AutomaticEventName = {\n [K in EventName]: (typeof EVENT_REGISTRY)[K][\"kind\"] extends \"automatic\" ? K : never;\n}[EventName];\n\n/**\n * Event names that consumer code can fire manually after registering them.\n */\nexport type ManualEventName = {\n [K in EventName]: (typeof EVENT_REGISTRY)[K][\"kind\"] extends \"manual\" ? K : never;\n}[EventName];\n\n// Map an event name to its explicit metadata / config TS type. We pull the\n// inferred types directly from the per-event files rather than deriving\n// via `z.infer<typeof EVENT_REGISTRY[K]['metadataSchema']>` so that error\n// messages name the event-specific type (PageViewMetadata, not an\n// anonymous zod inference).\ntype MetadataByName = {\n page_view: PageViewMetadata;\n time_on_site: TimeOnSiteMetadata;\n specific_page_visit: SpecificPageVisitMetadata;\n scroll_depth: ScrollDepthMetadata;\n multi_page_session: MultiPageSessionMetadata;\n form_start: FormStartMetadata;\n sdk_heartbeat: SdkHeartbeatMetadata;\n form_submit: FormSubmitMetadata;\n phone_click: PhoneClickMetadata;\n cta_click: CtaClickMetadata;\n};\n\ntype ConfigByName = {\n page_view: PageViewConfig;\n time_on_site: TimeOnSiteConfig;\n specific_page_visit: SpecificPageVisitConfig;\n scroll_depth: ScrollDepthConfig;\n multi_page_session: MultiPageSessionConfig;\n form_start: FormStartConfig;\n sdk_heartbeat: SdkHeartbeatConfig;\n form_submit: FormSubmitConfig;\n phone_click: PhoneClickConfig;\n cta_click: CtaClickConfig;\n};\n\n/**\n * Metadata payload type for a specific tracking event.\n *\n * @example\n * ```ts\n * type SubmitMetadata = EventMetadata<'form_submit'>;\n * ```\n */\nexport type EventMetadata<K extends EventName> = MetadataByName[K];\n\n/**\n * Trigger registration config type for a specific tracking event.\n */\nexport type EventConfig<K extends EventName> = ConfigByName[K];\n\n// Runtime constant arrays for iteration at consumer / factory time.\n/**\n * Runtime list of automatic event names.\n */\nexport const ALL_AUTOMATIC_EVENT_NAMES: readonly AutomaticEventName[] = (\n Object.entries(EVENT_REGISTRY) as Array<[EventName, (typeof EVENT_REGISTRY)[EventName]]>\n)\n .filter(([, def]) => def.kind === \"automatic\")\n .map(([name]) => name) as AutomaticEventName[];\n\n/**\n * Runtime list of manual event names.\n */\nexport const ALL_MANUAL_EVENT_NAMES: readonly ManualEventName[] = (\n Object.entries(EVENT_REGISTRY) as Array<[EventName, (typeof EVENT_REGISTRY)[EventName]]>\n)\n .filter(([, def]) => def.kind === \"manual\")\n .map(([name]) => name) as ManualEventName[];\n\n/**\n * Trigger registry passed to `createTracking({ triggers })`.\n *\n * `automatic.page_view` is required because every install should capture page\n * views. Other automatic events are opt-in. Manual events must be registered\n * here before the typed client accepts `trackEvent()` calls for them.\n *\n * @example\n * ```ts\n * createTracking({\n * apiKey,\n * endpoint,\n * triggers: {\n * automatic: {\n * page_view: {},\n * time_on_site: { thresholdSeconds: 60 },\n * },\n * manual: {\n * form_submit: {},\n * phone_click: {},\n * },\n * },\n * });\n * ```\n */\nexport type TriggerRegistryConfig = {\n automatic: {\n page_view: EventConfig<\"page_view\">;\n } & Partial<{\n time_on_site: EventConfig<\"time_on_site\">;\n specific_page_visit: EventConfig<\"specific_page_visit\">;\n scroll_depth: EventConfig<\"scroll_depth\">;\n multi_page_session: EventConfig<\"multi_page_session\">;\n form_start: EventConfig<\"form_start\">;\n }>;\n manual?: Partial<{\n form_submit: EventConfig<\"form_submit\">;\n phone_click: EventConfig<\"phone_click\">;\n cta_click: EventConfig<\"cta_click\">;\n }>;\n};\n\n/**\n * Manual event names registered in a concrete trigger registry.\n *\n * Used by `TypedTrackingClient` so `trackEvent()` only accepts events the\n * consumer explicitly enabled.\n */\nexport type RegisteredManualEvents<TRegistry extends TriggerRegistryConfig> = Extract<\n keyof NonNullable<TRegistry[\"manual\"]>,\n ManualEventName\n>;\n\n/**\n * Automatic event names registered in a concrete trigger registry.\n */\nexport type RegisteredAutomaticEvents<TRegistry extends TriggerRegistryConfig> = Extract<\n keyof TRegistry[\"automatic\"],\n AutomaticEventName\n>;\n\n/**\n * Discriminated union of valid manual tracking calls for a registry.\n */\nexport type TrackableEvent<TRegistry extends TriggerRegistryConfig> = {\n [K in RegisteredManualEvents<TRegistry>]: {\n eventType: K;\n metadata: EventMetadata<K>;\n };\n}[RegisteredManualEvents<TRegistry>];\n\n// Runtime helper: look up a schema pair by name. Cast through `unknown`\n// because the registry is `as const` and TS loses the specific schema type\n// when indexing via a dynamic key.\nexport function getEventDefinition(name: EventName): {\n kind: EventKind;\n metadataSchema: z.ZodTypeAny;\n configSchema: z.ZodTypeAny;\n} {\n return EVENT_REGISTRY[name] as unknown as {\n kind: EventKind;\n metadataSchema: z.ZodTypeAny;\n configSchema: z.ZodTypeAny;\n };\n}\n","import { z } from \"zod\";\n\n/**\n * Sales / Conversions wire schemas — the client-side source of truth.\n *\n * `saleCreateSchema` is mirrored by `SaleCreateSchema` in\n * `apps/api/src/schemas/tracking_sales.py` and enforced by the backend drift\n * test (the `resources` section of `events.schema.json`). Keep them in lockstep.\n *\n * Money is **integer minor units (cents)**; `quantity` is a decimal string;\n * `currency` is the required `SupportedCurrency` enum.\n */\n\nexport const SUPPORTED_CURRENCIES = [\"USD\", \"CAD\"] as const;\nexport type SupportedCurrency = (typeof SUPPORTED_CURRENCIES)[number];\n\nconst TRACKING_ENVIRONMENTS = [\"production\", \"development\"] as const;\n\nconst currencySchema = z.enum(SUPPORTED_CURRENCIES);\nconst centsSchema = z.number().int().nonnegative();\nconst quantitySchema = z.string().regex(/^\\d+(\\.\\d{1,3})?$/);\n// Free-form JSON object — the sale's extensibility escape hatch. Mirrors the\n// Pydantic `dict[str, Any]` (`additionalProperties: true`) on the wire.\nconst metadataSchema = z.record(z.unknown());\n\nexport const saleItemSchema = z\n .object({\n external_item_id: z.string().nullable().optional(),\n name: z.string().nullable().optional(),\n category: z.string().nullable().optional(),\n quantity: quantitySchema,\n unit_price_cents: centsSchema,\n // Non-negativity validated on the wire — same contract as the other cents\n // fields — and backstopped by the DB CHECK.\n unit_cost_cents: centsSchema.nullable().optional(),\n })\n .strict();\n\n// One service covered by a sale, at its own price. `service` is the per-business\n// service key, validated against the taxonomy by the backend at write time.\nexport const saleServiceSchema = z\n .object({\n service: z.string(),\n amount_cents: centsSchema,\n })\n .strict();\n\n// Raw customer identity. Stored as-is on the backend for human display in\n// dashboards; hashing happens at writeback-upload time in the future Google\n// Ads reconciliation worker (see docs/tracking-package/conversion-writeback.md).\n// The SDK never hashes — pass values straight through.\nconst customerNameSchema = z.string().max(200);\nconst customerPhoneSchema = z.string().max(64);\nconst customerEmailSchema = z.string().max(320).email();\n\n// The singular `service` (priced by `amount_total_cents`) and the plural\n// `services` (each priced individually) are mutually exclusive. When `services`\n// is present, `amount_total_cents` is optional and the backend derives it as the\n// sum; otherwise it is required. Shared by create + update.\nfunction refineServiceXor(\n val: {\n service?: string | null;\n services?: ReadonlyArray<{ service: string; amount_cents: number }> | null;\n amount_total_cents?: number | null;\n },\n ctx: z.RefinementCtx,\n { requireAmount }: { requireAmount: boolean },\n): void {\n if (val.services != null) {\n if (val.service != null) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: \"pass either `service` or `services`, not both\",\n path: [\"services\"],\n });\n }\n if (val.services.length === 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: \"`services` must not be empty\",\n path: [\"services\"],\n });\n }\n const keys = val.services.map((s) => s.service);\n if (new Set(keys).size !== keys.length) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: \"`services` must not list the same service more than once\",\n path: [\"services\"],\n });\n }\n if (val.amount_total_cents != null) {\n const sum = val.services.reduce((acc, s) => acc + s.amount_cents, 0);\n if (val.amount_total_cents !== sum) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message:\n \"amount_total_cents must equal the sum of the services amounts \" +\n \"(omit it to derive it automatically)\",\n path: [\"amount_total_cents\"],\n });\n }\n }\n } else if (requireAmount && val.amount_total_cents == null) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: \"amount_total_cents is required unless `services` is provided\",\n path: [\"amount_total_cents\"],\n });\n }\n}\n\nexport const saleCreateSchema = z\n .object({\n external_id: z.string().nullable().optional(),\n description: z.string().nullable().optional(),\n service: z.string().nullable().optional(),\n services: z.array(saleServiceSchema).nullable().optional(),\n currency: currencySchema,\n // Optional only because the plural `services` form derives it from the sum\n // (see refineServiceXor); the singular/serviceless path still requires it.\n amount_total_cents: centsSchema.nullable().optional(),\n occurred_at: z.string().datetime(),\n environment: z.enum(TRACKING_ENVIRONMENTS).default(\"production\"),\n items: z.array(saleItemSchema).default([]),\n metadata: metadataSchema.nullable().optional(),\n customer_name: customerNameSchema.nullable().optional(),\n customer_phone: customerPhoneSchema.nullable().optional(),\n customer_email: customerEmailSchema.nullable().optional(),\n })\n .strict()\n .superRefine((val, ctx) => refineServiceXor(val, ctx, { requireAmount: true }));\n\nexport const saleUpdateSchema = z\n .object({\n description: z.string().nullable().optional(),\n service: z.string().nullable().optional(),\n services: z.array(saleServiceSchema).nullable().optional(),\n currency: currencySchema.optional(),\n amount_total_cents: centsSchema.optional(),\n occurred_at: z.string().datetime().optional(),\n items: z.array(saleItemSchema).optional(),\n metadata: metadataSchema.nullable().optional(),\n customer_name: customerNameSchema.nullable().optional(),\n customer_phone: customerPhoneSchema.nullable().optional(),\n customer_email: customerEmailSchema.nullable().optional(),\n })\n .strict()\n .superRefine((val, ctx) => refineServiceXor(val, ctx, { requireAmount: false }));\n\nexport type SaleItemInput = z.input<typeof saleItemSchema>;\nexport type SaleServiceInput = z.input<typeof saleServiceSchema>;\n// Use the INPUT type so fields with Zod defaults (environment, items) and\n// occurred_at are optional for callers — the sales client fills them in.\nexport type SaleInput = z.input<typeof saleCreateSchema>;\nexport type SaleUpdateInput = z.input<typeof saleUpdateSchema>;\n\n// ---------------------------------------------------------------------------\n// Read shapes (server responses; not validated on the client)\n// ---------------------------------------------------------------------------\n\nexport interface SaleItem {\n id: string;\n external_item_id: string | null;\n name: string | null;\n category: string | null;\n quantity: string;\n unit_price_cents: number;\n unit_cost_cents: number | null;\n}\n\nexport interface SaleService {\n id: string;\n service_id: string;\n service_key: string;\n service_label: string;\n amount_cents: number;\n}\n\nexport interface Sale {\n id: string;\n business_id: string;\n business_name?: string | null;\n external_id: string | null;\n currency: SupportedCurrency;\n amount_total_cents: number;\n description: string | null;\n // Singular service fields are populated only for single-service sales (kept\n // for backwards compatibility); `services` is always the full set.\n service_id: string | null;\n service_key?: string | null;\n service_label?: string | null;\n services: SaleService[];\n occurred_at: string;\n environment: (typeof TRACKING_ENVIRONMENTS)[number];\n metadata: Record<string, unknown> | null;\n customer_name: string | null;\n customer_phone: string | null;\n customer_email: string | null;\n created_at: string;\n updated_at: string;\n items: SaleItem[];\n}\n\nexport interface SaleListPage {\n items: Sale[];\n total: number;\n}\n\nexport interface SaleCursorPage {\n items: Sale[];\n next_cursor: string | null;\n /** Populated only when `want_total` was requested (a single indexed COUNT). */\n total_count?: number;\n has_more?: boolean;\n}\n\n/**\n * Sortable columns on the secret-key (keyset) and admin (offset) list endpoints.\n * `business_name` only applies to the cross-business admin list — it sorts the\n * joined `businesses.name` column.\n */\nexport type SaleSortField =\n | \"occurred_at\"\n | \"created_at\"\n | \"amount_total_cents\"\n | \"customer_name\"\n | \"business_name\";\n\nexport type SaleSortOrder = \"asc\" | \"desc\";\n\n/**\n * Comprehensive filter shape mirrored from the backend's `SaleQueryFilters`.\n *\n * `search` runs case-insensitively across `customer_name`, `customer_phone`,\n * `customer_email`, `description`, and `external_id` — the human-facing\n * columns. `service_id` is the resolved per-business service UUID (different\n * from the create-time `service` *key*).\n */\nexport interface SaleFilters {\n business_id?: string;\n external_id?: string;\n service_id?: string;\n /** E.164 phone — the canonical customer key; filters sales for one customer. */\n customer_phone?: string;\n currency?: SupportedCurrency;\n environment?: (typeof TRACKING_ENVIRONMENTS)[number];\n since?: string;\n until?: string;\n min_amount_cents?: number;\n max_amount_cents?: number;\n search?: string;\n}\n\n/**\n * Full query input for `SalesClient.list()` — filters + pagination.\n *\n * Pagination is **keyset (cursor)**: `next_cursor` returned by one page is\n * passed back as `cursor` on the next. `null` / undefined cursor = first page.\n *\n * Ordering on this endpoint is fixed at **`occurred_at DESC, id DESC`** — the\n * cursor encodes a position in that index, so a different sort would\n * invalidate cursors mid-pagination. For ad-hoc sorted reads use the\n * dashboard admin endpoint, which is offset-paginated.\n */\nexport interface SaleListQuery extends SaleFilters {\n limit?: number;\n cursor?: string | null;\n}\n\n/** Keyset-safe sort columns (NOT-NULL, indexed). */\nexport type SaleKeysetSortField = \"occurred_at\" | \"created_at\" | \"amount_total_cents\";\n\n/** v2 list query — adds server sort + opt-in total. */\nexport interface SaleListQueryV2 extends SaleListQuery {\n sort?: SaleKeysetSortField;\n order?: SaleSortOrder;\n /** Opt-in: a single indexed COUNT over the filtered set. */\n want_total?: boolean;\n}\n\n// ---------------------------------------------------------------------------\n// Sales summary (aggregations). Read by `SalesClient.summary()`. Every monetary\n// figure is grouped **by currency** — USD and CAD cents are never summed into a\n// single total. Money stays integer cents; only `quantity` is a decimal string.\n// ---------------------------------------------------------------------------\n\nexport const TRACKING_RANGES = [\"24h\", \"7d\", \"30d\"] as const;\nexport type TrackingOverviewRange = (typeof TRACKING_RANGES)[number];\n\n/** Query input for `SalesClient.summary()` — filters + range + options. */\nexport interface SaleSummaryQuery extends SaleFilters {\n range: TrackingOverviewRange;\n /** Include the per-category line-item breakdown (extra join; default false). */\n include_categories?: boolean;\n /** Surface soft-deleted services individually (flagged \"(deleted)\") instead of\n * rolling them into a single \"Deleted services\" bucket. Default false. */\n include_deleted_services?: boolean;\n /** Cap on the by-service / by-category rows (1–50, default 10). */\n top_n?: number;\n}\n\nexport interface CurrencyRevenue {\n currency: SupportedCurrency;\n sale_count: number;\n revenue_cents: number;\n average_order_value_cents: number;\n}\n\nexport interface SalesServiceBreakdown {\n service_id: string | null;\n service_key: string | null;\n /** \"Unassigned\" when the sale has no service. */\n service_label: string | null;\n currency: SupportedCurrency;\n sale_count: number;\n revenue_cents: number;\n}\n\nexport interface SalesCategoryBreakdown {\n category: string | null;\n currency: SupportedCurrency;\n /** sum(unit_price_cents * quantity) — advisory, not authoritative. */\n revenue_cents: number;\n /** Decimal string. */\n quantity: string;\n}\n\nexport interface SalesTrendPoint {\n bucket_start: string;\n currency: SupportedCurrency;\n sale_count: number;\n revenue_cents: number;\n}\n\nexport interface SaleSummary {\n range: TrackingOverviewRange;\n business_id: string | null;\n metrics: {\n sale_count: number;\n distinct_customers: number;\n by_currency: CurrencyRevenue[];\n };\n by_service: SalesServiceBreakdown[];\n by_currency: CurrencyRevenue[];\n by_category: SalesCategoryBreakdown[];\n series: SalesTrendPoint[];\n}\n\n// ---------------------------------------------------------------------------\n// v2 dashboard surface (additive, back-compat). Reads only → plain interfaces,\n// not drift-checked. Legacy `24h/7d/30d` callers get the same numbers; new\n// ranges/options layer on tz-correct calendar windows + comparisons.\n// ---------------------------------------------------------------------------\n\nexport const NAMED_RANGES = [\n \"today\",\n \"yesterday\",\n \"wtd\",\n \"mtd\",\n \"qtd\",\n \"ytd\",\n \"24h\",\n \"7d\",\n \"30d\",\n \"90d\",\n \"custom\",\n] as const;\n/** Superset of `TrackingOverviewRange` (`24h`/`7d`/`30d` stay valid). */\nexport type NamedRange = (typeof NAMED_RANGES)[number];\nexport type Granularity = \"hour\" | \"day\" | \"week\" | \"month\" | \"auto\";\nexport type CompareTo = \"previous_period\" | \"previous_year\" | \"none\";\n\nexport interface DistinctCustomersByCurrency {\n currency: SupportedCurrency;\n distinct_customers: number;\n}\n\nexport interface SummaryWindow {\n since: string;\n until: string;\n}\n\nexport interface SummaryCurrencyDelta {\n currency: SupportedCurrency;\n revenue_cents_delta: number;\n /** Ratio (0.12 = +12%); null when previous revenue was 0. */\n revenue_pct_delta: number | null;\n sale_count_delta: number;\n}\n\nexport interface SummaryDeltas {\n by_currency: SummaryCurrencyDelta[];\n sale_count_delta: number;\n distinct_customers_delta: number;\n}\n\nexport interface SaleSummaryPrevious {\n window: SummaryWindow;\n metrics: SaleSummaryV2[\"metrics\"];\n by_currency: CurrencyRevenue[];\n series: SalesTrendPoint[];\n}\n\n/** Query input for `SalesClient.summary()` v2 — widened range + tz/compare options. */\nexport interface SaleSummaryQueryV2 extends SaleFilters {\n range?: NamedRange;\n /** Required when `range === 'custom'`. */\n since?: string;\n until?: string;\n /** IANA tz, e.g. `America/Toronto`. Default = business tz. */\n timezone?: string;\n granularity?: Granularity;\n compare_to?: CompareTo;\n include_categories?: boolean;\n include_deleted_services?: boolean;\n top_n?: number;\n}\n\n/** Superset of `SaleSummary` (assignable to it). */\nexport interface SaleSummaryV2 extends Omit<SaleSummary, \"range\" | \"metrics\"> {\n range: NamedRange;\n metrics: SaleSummary[\"metrics\"] & {\n distinct_customers_by_currency: DistinctCustomersByCurrency[];\n };\n timezone: string | null;\n window: SummaryWindow | null;\n granularity: string | null;\n previous: SaleSummaryPrevious | null;\n deltas: SummaryDeltas | null;\n}\n\n// ---------------------------------------------------------------------------\n// Customers — phone-keyed (a customer IS their E.164 phone). `customer_id` is\n// the E.164 string. Money is per-currency, never summed.\n// ---------------------------------------------------------------------------\n\nexport type CustomerSegment = \"new\" | \"returning\" | \"repeat\" | \"lapsed\";\nexport type CustomerSortField = \"total_spent\" | \"last_purchase\" | \"purchases\" | \"first_purchase\";\n\nexport interface CustomerCurrencyTotal {\n currency: SupportedCurrency;\n revenue_cents: number;\n sale_count: number;\n average_order_value_cents: number;\n}\n\nexport interface CustomerSummary {\n /** The E.164 phone — the canonical customer id. */\n customer_id: string;\n display_name: string | null;\n email: string | null;\n phone: string;\n first_purchase_at: string;\n last_purchase_at: string;\n purchase_count: number;\n segment: CustomerSegment;\n totals: CustomerCurrencyTotal[];\n}\n\nexport type CustomerProfile = CustomerSummary;\n\nexport interface CustomerListQuery extends Omit<SaleFilters, \"external_id\"> {\n segment?: CustomerSegment;\n sort?: CustomerSortField;\n order?: SaleSortOrder;\n cursor?: string | null;\n limit?: number;\n want_total?: boolean;\n}\n\nexport interface CustomerListPage {\n items: CustomerSummary[];\n next_cursor: string | null;\n total_count: number | null;\n}\n\nexport interface CustomerGetOptions {\n include_sales?: boolean;\n limit?: number;\n cursor?: string | null;\n}\n\nexport interface CustomerGetResult {\n customer: CustomerProfile;\n /** Present only when `include_sales: true` (and a business-scoped key). */\n sales?: SaleCursorPage | null;\n}\n\nexport interface CustomerSummaryQuery extends SaleFilters {\n range?: NamedRange;\n since?: string;\n until?: string;\n timezone?: string;\n compare_to?: CompareTo;\n}\n\nexport interface CustomerSegmentCount {\n segment: CustomerSegment;\n count: number;\n}\n\nexport interface CustomerCurrencyDelta {\n currency: SupportedCurrency;\n revenue_cents_delta: number;\n /** Ratio (0.12 = +12%); null when previous revenue was 0. */\n revenue_pct_delta: number | null;\n}\n\nexport interface CustomerKpisDeltas {\n total_customers_delta: number;\n new_customers_delta: number;\n returning_customers_delta: number;\n repeat_rate_delta: number;\n ltv_by_currency: CustomerCurrencyDelta[];\n}\n\nexport interface CustomerKpisPrevious {\n window_since: string;\n window_until: string;\n total_customers: number;\n new_customers: number;\n returning_customers: number;\n repeat_rate: number;\n ltv_by_currency: CustomerCurrencyTotal[];\n}\n\nexport interface CustomerKpis {\n range: NamedRange;\n timezone: string;\n window_since: string;\n window_until: string;\n total_customers: number;\n new_customers: number;\n returning_customers: number;\n repeat_rate: number;\n by_segment: CustomerSegmentCount[];\n ltv_by_currency: CustomerCurrencyTotal[];\n /** Present only when `compare_to` is set. */\n previous?: CustomerKpisPrevious | null;\n deltas?: CustomerKpisDeltas | null;\n}\n\n// ---------------------------------------------------------------------------\n// Business config — supersets `fetchServices()`.\n// ---------------------------------------------------------------------------\n\nexport interface BusinessConfigService {\n key: string;\n label: string;\n archived: boolean;\n}\n\nexport interface BusinessConfigFeatures {\n customers: boolean;\n comparisons: boolean;\n retention: boolean;\n}\n\nexport interface BusinessConfig {\n business_id: string;\n display_name: string;\n timezone: string;\n primary_currency: SupportedCurrency;\n currencies: SupportedCurrency[];\n default_phone_country: string | null;\n services: BusinessConfigService[];\n features: BusinessConfigFeatures;\n}\n","// Framework-agnostic phone-number utilities — isomorphic (browser + Node/RSC),\n// zero React. Bundled `libphonenumber-js` (standard metadata) so clients add no\n// dependency. The transmitted value is ALWAYS E.164; display is the only knob.\n\nimport { AsYouType, parsePhoneNumberFromString, type CountryCode } from \"libphonenumber-js\";\n\nexport type { CountryCode };\n\n/**\n * How a phone number is shown in the UI. The transmitted value is always E.164\n * and is deliberately NOT part of this — only the display format is configurable.\n * A function form covers the long tail (`(parsed) => string`).\n */\nexport type PhoneDisplayFormat =\n | \"national\"\n | \"international\"\n | \"e164\"\n | ((parsed: ParsedPhone) => string);\n\nexport interface ParsedPhone {\n /** E.164 (`\"+14165550199\"`) or `null` when the input isn't a valid number. This is what gets transmitted. */\n e164: string | null;\n /** National display form (`\"(416) 555-0199\"`); empty string when unparseable. */\n national: string;\n /** International display form (`\"+1 416 555 0199\"`); empty string when unparseable. */\n international: string;\n /** ISO-3166 country resolved by libphonenumber, or `null`. */\n country: CountryCode | null;\n isValid: boolean;\n}\n\n/** Region assumed for numbers typed without a country code. */\nexport const DEFAULT_PHONE_COUNTRY: CountryCode = \"CA\";\n\n/** Parse a raw/display string into every representation at once (one parse → display + wire never drift). */\nexport function parsePhone(raw: string, country?: CountryCode): ParsedPhone {\n const region = country ?? DEFAULT_PHONE_COUNTRY;\n const parsed = parsePhoneNumberFromString(raw ?? \"\", region);\n if (!parsed) {\n return { e164: null, national: \"\", international: \"\", country: region, isValid: false };\n }\n const isValid = parsed.isValid();\n return {\n // E.164 is only surfaced for a *valid* number — a possible-but-invalid input\n // (e.g. too few digits) still parses but must not be transmitted.\n e164: isValid ? parsed.number : null,\n national: parsed.formatNational(),\n international: parsed.formatInternational(),\n country: parsed.country ?? region,\n isValid,\n };\n}\n\n/** Normalize any raw/display value to E.164, or `null` if it isn't a valid number. */\nexport function toE164(raw: string, country?: CountryCode): string | null {\n return parsePhone(raw, country).e164;\n}\n\n/** Format a value for display. Defaults to `'national'`. Never affects the wire value. */\nexport function formatPhone(\n value: string,\n format: PhoneDisplayFormat = \"national\",\n country?: CountryCode,\n): string {\n const parsed = parsePhone(value, country);\n if (typeof format === \"function\") return format(parsed);\n switch (format) {\n case \"international\":\n return parsed.international || value;\n case \"e164\":\n return parsed.e164 ?? value;\n case \"national\":\n default:\n return parsed.national || value;\n }\n}\n\n/** Live, incremental formatting for an `<input>` as the user types (`AsYouType`). */\nexport function formatPhoneAsTyped(raw: string, country?: CountryCode): string {\n return new AsYouType(country ?? DEFAULT_PHONE_COUNTRY).input(raw ?? \"\");\n}\n","// Layer 0 (init config) + Layer 3 (tracking integration) for phone fields.\n// Kept in core so both the React and Next packages share identical types, and\n// the `phoneField` helper stays isomorphic.\n\nimport type { JsonValue } from \"./events/form-submit\";\nimport { toE164, type CountryCode, type PhoneDisplayFormat } from \"./phone\";\n\n/**\n * Init-time phone config (`createTracking({ phone })`). The transmitted value is\n * ALWAYS E.164 and is deliberately not configurable here — only display is.\n */\nexport interface PhoneConfig {\n /** Region assumed for numbers typed without a country code. Default `'CA'`. */\n defaultCountry?: CountryCode;\n /** How the input DISPLAYS to the user. Default `'national'`. Does not affect the wire. */\n display?: PhoneDisplayFormat;\n}\n\n/** A single tracked form field destined for `form_submit.fields[]`. */\nexport interface TrackedField {\n name: string;\n type: string;\n value: JsonValue;\n label?: string | null;\n}\n\n/**\n * Build a tracked field whose wire value is ALWAYS E.164. The client keeps its\n * own display value for UI/email; this puts `+E.164` on the wire (or `null` when\n * the input isn't a valid number).\n */\nexport function phoneField(name: string, raw: string, country?: CountryCode): TrackedField {\n return { name, type: \"phone\", value: toE164(raw, country) };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACsBO,IAAM,kBAAkB,KAAK,KAAK;;;ACtBzC,iBAAkB;AASX,IAAM,yBAAyB,aACnC,OAAO;AAAA,EACN,MAAM,aACH,OAAO;AAAA,IACN,OAAO,aAAE,OAAO,EAAE,SAAS;AAAA,IAC3B,MAAM,aAAE,OAAO;AAAA,IACf,QAAQ,aAAE,OAAO;AAAA,IACjB,MAAM,aAAE,OAAO;AAAA,EACjB,CAAC,EACA,OAAO;AAAA,EACV,UAAU,aAAE,OAAO,EAAE,SAAS;AAAA;AAAA;AAAA;AAAA,EAI9B,UAAU,aACP,OAAO;AAAA,IACN,GAAG,aAAE,OAAO;AAAA,IACZ,GAAG,aAAE,OAAO;AAAA,EACd,CAAC,EACA,OAAO,EACP,SAAS,EACT,SAAS;AACd,CAAC,EACA,OAAO;AAUH,IAAM,uBAAuB,aAAE,OAAO,CAAC,CAAC,EAAE,OAAO;;;AC1CxD,IAAAA,cAAkB;AAQX,IAAM,yBAAyB,cACnC,OAAO;AAAA,EACN,UAAU,cAAE,OAAO;AAAA,EACnB,MAAM,cACH,OAAO;AAAA,IACN,MAAM,cAAE,OAAO;AAAA,EACjB,CAAC,EACA,OAAO;AAAA,EACV,SAAS,cAAE,OAAO,EAAE,SAAS,EAAE,SAAS;AAAA,EACxC,iBAAiB,cAAE,OAAO,EAAE,SAAS,EAAE,SAAS;AAClD,CAA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AC1B,QAAM,aAAS,qDAA2B,OAAO,IAAI,MAAM;AAC3D,MAAI,CAAC,QAAQ;AACX,WAAO,EAAE,MAAM,MAAM,UAAU,IAAI,eAAe,IAAI,SAAS,QAAQ,SAAS,MAAM;AAAA,EACxF;AACA,QAAM,UAAU,OAAO,QAAQ;AAC/B,SAAO;AAAA;AAAA;AAAA,IAGL,MAAM,UAAU,OAAO,SAAS;AAAA,IAChC,UAAU,OAAO,eAAe;AAAA,IAChC,eAAe,OAAO,oBAAoB;AAAA,IAC1C,SAAS,OAAO,WAAW;AAAA,IAC3B;AAAA,EACF;AACF;AAGO,SAAS,OAAO,KAAa,SAAsC;AACxE,SAAO,WAAW,KAAK,OAAO,EAAE;AAClC;AAGO,SAAS,YACd,OACA,SAA6B,YAC7B,SACQ;AACR,QAAM,SAAS,WAAW,OAAO,OAAO;AACxC,MAAI,OAAO,WAAW,WAAY,QAAO,OAAO,MAAM;AACtD,UAAQ,QAAQ;AAAA,IACd,KAAK;AACH,aAAO,OAAO,iBAAiB;AAAA,IACjC,KAAK;AACH,aAAO,OAAO,QAAQ;AAAA,IACxB,KAAK;AAAA,IACL;AACE,aAAO,OAAO,YAAY;AAAA,EAC9B;AACF;AAGO,SAAS,mBAAmB,KAAa,SAA+B;AAC7E,SAAO,IAAI,mCAAU,WAAW,qBAAqB,EAAE,MAAM,OAAO,EAAE;AACxE;;;ACjDO,SAAS,WAAW,MAAc,KAAa,SAAqC;AACzF,SAAO,EAAE,MAAM,MAAM,SAAS,OAAO,OAAO,KAAK,OAAO,EAAE;AAC5D;","names":["import_zod","import_zod","import_zod","import_zod","import_zod","import_zod","import_zod","import_zod","import_zod","import_zod"]}
1
+ {"version":3,"sources":["../src/phone-utils.ts","../../tracking-core/src/consent.ts","../../tracking-core/src/gtag.ts","../../tracking-core/src/resources/conversion-firing.ts","../../tracking-core/src/session.ts","../../tracking-core/src/events/page-view.ts","../../tracking-core/src/events/cta-click.ts","../../tracking-core/src/events/sdk-heartbeat.ts","../../tracking-core/src/events/form-start.ts","../../tracking-core/src/events/form-submit.ts","../../tracking-core/src/events/multi-page-session.ts","../../tracking-core/src/events/phone-click.ts","../../tracking-core/src/events/scroll-depth.ts","../../tracking-core/src/events/specific-page-visit.ts","../../tracking-core/src/events/time-on-site.ts","../../tracking-core/src/events/registry.ts","../../tracking-core/src/resources/sales/schema.ts","../../tracking-core/src/phone.ts","../../tracking-core/src/phone-field.ts"],"sourcesContent":["// `@aranova/tracking-react/phone` — the isomorphic phone utils only (NO React,\n// NO 'use client'), so server/Node code can normalize/format too. The React\n// hook + component live on the package root.\nexport {\n DEFAULT_PHONE_COUNTRY,\n formatPhone,\n formatPhoneAsTyped,\n parsePhone,\n phoneField,\n toE164,\n} from \"../../tracking-core/src/index\";\nexport type {\n CountryCode,\n ParsedPhone,\n PhoneConfig,\n PhoneDisplayFormat,\n TrackedField,\n} from \"../../tracking-core/src/index\";\n","import type { ConsentState } from \"./types\";\n\n/**\n * localStorage key for the visitor's consent choice.\n */\nexport const CONSENT_STATE_KEY = \"consent_state\";\n\n/**\n * localStorage key for the ISO timestamp when consent was last changed.\n */\nexport const CONSENT_TIMESTAMP_KEY = \"consent_timestamp\";\n\n/**\n * Google Consent Mode value sent to `gtag('consent', 'update', ...)`.\n */\nexport type GtagConsentValue = \"granted\" | \"denied\";\n\n/**\n * Consent Mode v2 payload sent to Google Ads when consent changes.\n */\nexport interface ConsentUpdatePayload {\n ad_storage: GtagConsentValue;\n ad_user_data: GtagConsentValue;\n ad_personalization: GtagConsentValue;\n analytics_storage: GtagConsentValue;\n}\n\ntype ConsentGrantedListener = () => void;\nconst grantedListeners = new Set<ConsentGrantedListener>();\n\n/**\n * Subscribe to consent being granted. Used to flush conversions that were queued while\n * consent was still pending (GAP28). Returns an unsubscribe function.\n */\nexport function onConsentGranted(listener: ConsentGrantedListener): () => void {\n grantedListeners.add(listener);\n return () => {\n grantedListeners.delete(listener);\n };\n}\n\n/**\n * Build the Google Consent Mode v2 update payload for a single consent state.\n */\nexport function buildConsentPayload(state: GtagConsentValue): ConsentUpdatePayload {\n return {\n ad_storage: state,\n ad_user_data: state,\n ad_personalization: state,\n analytics_storage: state,\n };\n}\n\n/**\n * Read the persisted visitor consent state from localStorage.\n *\n * Returns `pending` when called during SSR or before the visitor has made a\n * choice.\n */\nexport function getConsentState(): ConsentState {\n if (typeof window === \"undefined\") return \"pending\";\n\n try {\n const storedState = window.localStorage.getItem(CONSENT_STATE_KEY);\n if (storedState === \"granted\" || storedState === \"denied\") return storedState;\n } catch {\n // localStorage blocked (sandboxed iframe / storage disabled) — treat as pending\n }\n\n return \"pending\";\n}\n\n/**\n * Persist a visitor consent choice and update Google Consent Mode when gtag is\n * loaded.\n */\nexport function setConsentState(state: GtagConsentValue): void {\n if (typeof window === \"undefined\") return;\n\n try {\n window.localStorage.setItem(CONSENT_STATE_KEY, state);\n window.localStorage.setItem(CONSENT_TIMESTAMP_KEY, new Date().toISOString());\n } catch {\n // localStorage blocked — still push the live consent update below so the\n // visitor's click takes effect for this page load even if it can't persist\n }\n\n if (typeof window.gtag === \"function\")\n window.gtag(\"consent\", \"update\", buildConsentPayload(state));\n\n // Mirror the choice to the Meta Pixel so one banner gates both ad platforms.\n if (typeof window.fbq === \"function\")\n window.fbq(\"consent\", state === \"granted\" ? \"grant\" : \"revoke\");\n\n // Flush any conversions queued while consent was pending (GAP28).\n if (state === \"granted\") {\n for (const listener of grantedListeners) {\n try {\n listener();\n } catch {\n // a listener must never break the consent flow\n }\n }\n }\n}\n\n/**\n * Clear the stored consent choice so the banner re-appears on next render.\n *\n * Power a \"Cookie preferences\" link in a footer so visitors can change their\n * mind without losing access to your site:\n *\n * ```tsx\n * const { reset } = useConsent();\n * <button onClick={reset}>Cookie preferences</button>\n * ```\n *\n * Does NOT push an `update` to gtag — there's nothing to update because the\n * visitor hasn't chosen anything yet. The next `setConsentState()` call will\n * sync gtag once they re-choose.\n */\nexport function resetConsent(): void {\n if (typeof window === \"undefined\") return;\n\n try {\n window.localStorage.removeItem(CONSENT_STATE_KEY);\n window.localStorage.removeItem(CONSENT_TIMESTAMP_KEY);\n } catch {\n // localStorage blocked — nothing to clear\n }\n}\n\n/**\n * Re-apply a stored consent choice to Google Consent Mode.\n *\n * Useful after bootstrapping gtag on a page that already has a saved choice.\n */\nexport function restoreStoredConsent(): ConsentState {\n const consentState = getConsentState();\n\n if (consentState === \"granted\" || consentState === \"denied\") setConsentState(consentState);\n\n return consentState;\n}\n","// This module loads the Google tag (gtag.js) and today fires ONLY `gtag('config', …)`\n// plus Consent Mode — i.e. it builds Google Ads REMARKETING audiences once the visitor\n// accepts the consent banner. It does not fire `gtag('event','conversion')` yet, so\n// conversions are currently tracked OFFLINE via the backend writeback worker\n// (UPLOAD_CLICKS actions). Real-time on-site conversion firing through this tag is\n// planned (gap 28). See docs/google-ads-deployment/gtags-and-conversion-tracking.md.\n\nimport { buildConsentPayload, getConsentState, restoreStoredConsent } from \"./consent\";\nimport type { GtagEnvironmentMap } from \"./types\";\n\n/**\n * Google Tag Manager script host used for gtag loading.\n */\nexport const GTAG_SCRIPT_HOST = \"https://www.googletagmanager.com/gtag/js\";\n\n/**\n * Attribute used to mark scripts inserted by the tracking SDK.\n */\nexport const TRACKING_SCRIPT_ATTRIBUTE = \"data-aranova-tracking\";\n\nfunction getScriptMarker(id: string): string {\n return `aranova-${id}`;\n}\n\n/**\n * Pattern matching valid Google Ads / GA4 tag IDs.\n *\n * Accepted formats: `AW-123456789`, `G-XXXXXXXXXX`, `GT-XXXXXXX`, `DC-XXXXXXX`.\n * IDs that don't match are silently dropped to prevent script injection when\n * interpolated into inline `<script>` content.\n */\nconst GTAG_ID_PATTERN = /^[A-Z]{1,3}-[A-Za-z0-9_-]+$/;\n\nexport function isValidGtagId(id: string): boolean {\n return GTAG_ID_PATTERN.test(id);\n}\n\n/**\n * Create an inline script that initializes gtag with denied consent defaults.\n */\nexport function createConsentDefaultScript(): string {\n return `\nwindow.dataLayer = window.dataLayer || [];\nfunction gtag(){dataLayer.push(arguments);}\nwindow.gtag = gtag;\ngtag('consent', 'default', {\n ad_storage: 'denied',\n ad_user_data: 'denied',\n ad_personalization: 'denied',\n analytics_storage: 'denied',\n wait_for_update: 500,\n});\n`.trim();\n}\n\n/**\n * Create an inline script that initializes gtag for a Google Ads tag id.\n */\nexport function createGtagInitScript(gtagId: string): string {\n if (!isValidGtagId(gtagId)) return \"\";\n return `\nwindow.dataLayer = window.dataLayer || [];\nfunction gtag(){dataLayer.push(arguments);}\nwindow.gtag = gtag;\ngtag('js', new Date());\ngtag('config', '${gtagId}');\n`.trim();\n}\n\n/**\n * Create an inline script that restores a stored consent choice into gtag.\n */\nexport function createConsentRestoreScript(): string {\n // Guarded: window.localStorage access throws a SecurityError in sandboxed\n // iframes / storage-blocked contexts; an uncaught throw would abort the inline\n // <Script> and leave a prior consent choice un-applied.\n return `\ntry {\n var consentState = window.localStorage.getItem('consent_state');\n if (window.gtag && (consentState === 'granted' || consentState === 'denied')) {\n window.gtag('consent', 'update', {\n ad_storage: consentState,\n ad_user_data: consentState,\n ad_personalization: consentState,\n analytics_storage: consentState,\n });\n }\n} catch (e) {}\n`.trim();\n}\n\n/**\n * Ensure `window.gtag` and `window.dataLayer` exist and return the gtag shim.\n */\nexport function ensureGtagFunction(): NonNullable<Window[\"gtag\"]> {\n window.dataLayer = window.dataLayer || [];\n\n if (typeof window.gtag === \"function\") return window.gtag;\n\n window.gtag = (...args: unknown[]) => {\n window.dataLayer?.push(args);\n };\n\n return window.gtag;\n}\n\nconst SEND_TO_RE = /^AW-[A-Za-z0-9]+\\/[A-Za-z0-9_-]+$/;\n\n/** A gtag `send_to` target, e.g. `AW-123456789/AbC-D_efg`. */\nexport function isValidSendTo(sendTo: string): boolean {\n return SEND_TO_RE.test(sendTo);\n}\n\nexport interface GtagConversionInput {\n /** `AW-<id>/<label>` — the conversion action's firing target. */\n sendTo: string;\n /** Conversion value in MAJOR units; omit to let Google apply the action's default. */\n value?: number | null;\n currency?: string | null;\n transactionId?: string | null;\n}\n\n/**\n * Fire a real-time on-site conversion — `gtag('event','conversion',{ send_to, … })`\n * (GAP28). Browser-gated and never throws: `record()` is isomorphic and runs server-side\n * under a secret key where `window`/`gtag` are absent, so this no-ops there. Consent\n * gating + de-dup are the caller's responsibility (see `fireConversionWithConsent`).\n * Returns whether the event was actually pushed.\n */\nexport function fireGtagConversion(input: GtagConversionInput): boolean {\n if (typeof window === \"undefined\" || typeof window.gtag !== \"function\") return false;\n if (!isValidSendTo(input.sendTo)) return false;\n const params: Record<string, unknown> = { send_to: input.sendTo };\n if (input.value != null) params.value = input.value;\n if (input.currency) params.currency = input.currency;\n if (input.transactionId) params.transaction_id = input.transactionId;\n try {\n window.gtag(\"event\", \"conversion\", params);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Apply denied-by-default Google Consent Mode state.\n */\nexport function applyDefaultConsentState(): void {\n const gtag = ensureGtagFunction();\n gtag(\"consent\", \"default\", {\n ad_storage: \"denied\",\n ad_user_data: \"denied\",\n ad_personalization: \"denied\",\n analytics_storage: \"denied\",\n wait_for_update: 500,\n });\n}\n\n/**\n * Load the external gtag script once for the provided Google Ads tag id.\n */\nexport function loadGtagScript(gtagId: string): void {\n if (typeof document === \"undefined\") return;\n\n const marker = getScriptMarker(\"gtag-loader\");\n const existingScript = document.querySelector<HTMLScriptElement>(\n `script[${TRACKING_SCRIPT_ATTRIBUTE}=\"${marker}\"]`,\n );\n if (existingScript) return;\n\n const script = document.createElement(\"script\");\n script.async = true;\n script.src = `${GTAG_SCRIPT_HOST}?id=${encodeURIComponent(gtagId)}`;\n script.setAttribute(TRACKING_SCRIPT_ATTRIBUTE, marker);\n document.head.append(script);\n}\n\n/**\n * Send the initial `gtag('js')` and `gtag('config')` calls.\n */\nexport function initializeGtag(gtagId: string): void {\n const gtag = ensureGtagFunction();\n gtag(\"js\", new Date());\n gtag(\"config\", gtagId);\n}\n\n/**\n * Push the stored consent choice to gtag if one exists.\n */\nexport function restoreConsentState(): void {\n if (typeof window === \"undefined\") return;\n\n const consentState = getConsentState();\n if (consentState === \"granted\" || consentState === \"denied\")\n window.gtag?.(\"consent\", \"update\", buildConsentPayload(consentState));\n}\n\n/**\n * Load and initialize Google Ads tracking with Consent Mode support.\n *\n * Used by framework components and browser-script initialization.\n */\nexport function bootstrapGoogleAdsTracking(gtagId: string): void {\n if (typeof window === \"undefined\" || typeof document === \"undefined\") return;\n if (!isValidGtagId(gtagId)) return;\n\n applyDefaultConsentState();\n loadGtagScript(gtagId);\n initializeGtag(gtagId);\n restoreStoredConsent();\n}\n\n/**\n * Load and initialize Google Ads tracking for ALL labelled gtag IDs.\n *\n * Every ID in the map gets a `gtag('config', ...)` call — gtag natively\n * supports multiple configured tags on the same page. The script loader\n * only runs once (for the first ID); subsequent IDs reuse the shared\n * `dataLayer`.\n */\nexport function bootstrapMultipleGtags(gtagIds: GtagEnvironmentMap): void {\n if (typeof window === \"undefined\" || typeof document === \"undefined\") return;\n\n const ids = Object.values(gtagIds).filter(\n (id): id is string => typeof id === \"string\" && isValidGtagId(id),\n );\n if (ids.length === 0) return;\n\n applyDefaultConsentState();\n loadGtagScript(ids[0]!);\n\n // Call gtag('js') once, then gtag('config') per ID. initializeGtag\n // calls both, but gtag('js') should only fire once per page load.\n const gtag = ensureGtagFunction();\n gtag(\"js\", new Date());\n for (const id of ids) {\n gtag(\"config\", id);\n }\n\n restoreStoredConsent();\n}\n\n/**\n * Create an inline script that initializes gtag for multiple Google Ads tag ids.\n *\n * Used by the Next.js `GoogleAdsTracking` component when rendering `<Script>`\n * tags for a multi-gtag configuration.\n */\nexport function createGtagInitScriptMulti(gtagIds: string[]): string {\n const safeIds = gtagIds.filter(isValidGtagId);\n const configs = safeIds.map((id) => `gtag('config', '${id}');`).join(\"\\n\");\n return `\nwindow.dataLayer = window.dataLayer || [];\nfunction gtag(){dataLayer.push(arguments);}\nwindow.gtag = gtag;\ngtag('js', new Date());\n${configs}\n`.trim();\n}\n","// Consent-gated, de-duped orchestration around the raw `fireGtagConversion` (GAP28).\n//\n// granted → fire now (de-duped by transaction_id + send_to via sessionStorage)\n// pending → queue, flush automatically when consent is granted\n// denied → drop\n//\n// `fireGtagConversion` is itself browser-gated and never throws, so this is safe to call\n// from the isomorphic sales `record()` — server-side it simply no-ops.\n\nimport { getConsentState, onConsentGranted } from \"../consent\";\nimport { fireGtagConversion, type GtagConversionInput } from \"../gtag\";\n\nconst DEDUP_PREFIX = \"_aranova_conv_\";\n// Cap the queue so a visitor who never accepts the banner can't grow it unbounded; we drop\n// the oldest entry past the cap (the most recent conversions are the ones worth keeping).\nconst MAX_PENDING = 100;\nconst pendingQueue: GtagConversionInput[] = [];\n\nfunction dedupKey(input: GtagConversionInput): string {\n return `${DEDUP_PREFIX}${input.transactionId ?? \"\"}:${input.sendTo}`;\n}\n\n// Has this (transaction_id, send_to) already fired? Read-only — the key is written ONLY\n// after a successful fire (see `fireOnce`), so a fire that never happened (e.g. gtag not\n// ready yet) is never suppressed on retry. Best-effort: a blocked sessionStorage reads as\n// \"not fired\" so the conversion still gets a chance.\nfunction alreadyFired(input: GtagConversionInput): boolean {\n if (!input.transactionId || typeof window === \"undefined\") return false;\n try {\n return window.sessionStorage.getItem(dedupKey(input)) !== null;\n } catch {\n return false;\n }\n}\n\nfunction markFired(input: GtagConversionInput): void {\n if (!input.transactionId || typeof window === \"undefined\") return;\n try {\n window.sessionStorage.setItem(dedupKey(input), \"1\");\n } catch {\n // sessionStorage unavailable — Google's counting_type is the authoritative backstop\n }\n}\n\n// Fire once: skip if already fired, and only record the guard AFTER the fire succeeds.\nfunction fireOnce(input: GtagConversionInput): void {\n if (alreadyFired(input)) return;\n if (fireGtagConversion(input)) markFired(input);\n}\n\n/** Fire a conversion through the consent gate + de-dup guard. */\nexport function fireConversionWithConsent(input: GtagConversionInput): void {\n const state = getConsentState();\n if (state === \"denied\") return;\n if (state === \"pending\") {\n if (pendingQueue.length >= MAX_PENDING) pendingQueue.shift();\n pendingQueue.push(input);\n return;\n }\n fireOnce(input);\n}\n\n/** Flush conversions queued while consent was pending; called on consent-grant. */\nexport function flushPendingConversions(): void {\n if (getConsentState() !== \"granted\") return;\n while (pendingQueue.length > 0) {\n const input = pendingQueue.shift();\n if (input) fireOnce(input);\n }\n}\n\n// Subscribe once at module load so a grant flushes the queue regardless of how the queue\n// was filled (browser-only; no-ops under SSR where there is no consent flow).\nif (typeof window !== \"undefined\") onConsentGranted(flushPendingConversions);\n","// Visitor + session identity for the tracking SDK.\n//\n// Visitor: persistent localStorage UUID, never expires until the user clears\n// browser storage. Used for cross-session correlation.\n//\n// Session: rolling 30-minute idle window. Regenerated when more than\n// SESSION_IDLE_MS has passed since the last event. Matches the behavior of\n// GA4, PostHog, Mixpanel, etc., so analytics is comparable.\n\n/**\n * localStorage key for the persistent visitor id.\n */\nexport const VISITOR_STORAGE_KEY = \"aranova_tracking_visitor\";\n\n/**\n * localStorage key for the rolling session id state.\n */\nexport const SESSION_STORAGE_KEY = \"aranova_tracking_session\";\n\n/**\n * Idle window before a new session id is created.\n */\nexport const SESSION_IDLE_MS = 30 * 60 * 1000;\n\n/**\n * Serialized session state stored in localStorage.\n */\nexport interface StoredSession {\n /** Client-generated session UUID. */\n id: string;\n /** Unix timestamp in milliseconds for the most recent event/session touch. */\n last_event_at: number;\n}\n\nfunction safeUuid(): string {\n if (typeof crypto !== \"undefined\" && typeof crypto.randomUUID === \"function\")\n return crypto.randomUUID();\n // Fallback for ancient browsers — not cryptographically perfect but unique enough.\n return `${Date.now().toString(16)}-${Math.random().toString(16).slice(2)}-${Math.random().toString(16).slice(2)}`;\n}\n\nfunction readLocalStorage(key: string): string | null {\n try {\n return window.localStorage.getItem(key);\n } catch {\n return null;\n }\n}\n\nfunction writeLocalStorage(key: string, value: string): void {\n try {\n window.localStorage.setItem(key, value);\n } catch {\n // Storage may be denied (private mode, blocked cookies, quota). Caller is\n // responsible for degrading gracefully.\n }\n}\n\n/**\n * Return the persistent visitor id for this browser profile.\n *\n * Creates and stores a new id when one does not already exist. During SSR,\n * returns an ephemeral id because browser storage is unavailable.\n */\nexport function getVisitorId(): string {\n if (typeof window === \"undefined\") return safeUuid();\n\n const existing = readLocalStorage(VISITOR_STORAGE_KEY);\n if (existing && existing.length > 0) return existing;\n\n const fresh = safeUuid();\n writeLocalStorage(VISITOR_STORAGE_KEY, fresh);\n return fresh;\n}\n\n/**\n * Result from `getOrRotateSessionId()`.\n */\nexport interface SessionIdResult {\n /** Current session id. */\n id: string;\n /** Whether this call created a new session. */\n isNew: boolean;\n}\n\n/**\n * Return the current session id, rotating it after the idle window expires.\n *\n * Also refreshes `last_event_at` for active sessions.\n */\nexport function getOrRotateSessionId(now: number = Date.now()): SessionIdResult {\n if (typeof window === \"undefined\") return { id: safeUuid(), isNew: true };\n\n const raw = readLocalStorage(SESSION_STORAGE_KEY);\n if (raw) {\n try {\n const parsed = JSON.parse(raw) as Partial<StoredSession>;\n if (typeof parsed.id === \"string\" && typeof parsed.last_event_at === \"number\") {\n if (now - parsed.last_event_at <= SESSION_IDLE_MS) {\n const refreshed: StoredSession = { id: parsed.id, last_event_at: now };\n writeLocalStorage(SESSION_STORAGE_KEY, JSON.stringify(refreshed));\n return { id: parsed.id, isNew: false };\n }\n }\n } catch {\n // Fall through to a fresh session.\n }\n }\n\n const fresh: StoredSession = { id: safeUuid(), last_event_at: now };\n writeLocalStorage(SESSION_STORAGE_KEY, JSON.stringify(fresh));\n return { id: fresh.id, isNew: true };\n}\n\n/**\n * Clear visitor and session identity from localStorage.\n *\n * Intended for tests, debugging, and explicit user reset flows.\n */\nexport function resetTrackingIdentity(): void {\n if (typeof window === \"undefined\") return;\n try {\n window.localStorage.removeItem(VISITOR_STORAGE_KEY);\n window.localStorage.removeItem(SESSION_STORAGE_KEY);\n } catch {\n // ignored\n }\n}\n","import { z } from \"zod\";\n\n/**\n * Metadata for the automatic `page_view` event.\n *\n * The SDK emits this on initial load, SPA route changes, and bfcache restores.\n * Consumers do not call `trackEvent('page_view', ...)`; registering\n * `automatic: { page_view: {} }` enables the SDK-owned trigger.\n */\nexport const pageViewMetadataSchema = z\n .object({\n page: z\n .object({\n title: z.string().nullable(),\n path: z.string(),\n search: z.string(),\n hash: z.string(),\n })\n .strict(),\n referrer: z.string().nullable(),\n // `.nullable().optional()` — absent (undefined) OR explicit null OR a\n // real viewport object. Mirrors Pydantic's `_Viewport | None = None`\n // on the backend side so the drift test stays clean.\n viewport: z\n .object({\n w: z.number(),\n h: z.number(),\n })\n .strict()\n .nullable()\n .optional(),\n })\n .strict();\n\nexport type PageViewMetadata = z.infer<typeof pageViewMetadataSchema>;\n\n/**\n * Registration config for automatic `page_view`.\n *\n * `page_view` is required in every trigger registry and currently has no\n * options. Use `{ page_view: {} }`.\n */\nexport const pageViewConfigSchema = z.object({}).strict();\nexport type PageViewConfig = z.infer<typeof pageViewConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for a manually fired `cta_click` event.\n *\n * Use this for non-phone calls to action such as directions, appointment\n * buttons, downloads, or external booking links.\n */\nexport const ctaClickMetadataSchema = z\n .object({\n cta_name: z.string(),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n section: z.string().nullable().optional(),\n destination_url: z.string().nullable().optional(),\n })\n .strict();\n\nexport type CtaClickMetadata = z.infer<typeof ctaClickMetadataSchema>;\n\n/**\n * Registration config for `cta_click`.\n *\n * This event is manual-only and currently has no registration options.\n */\nexport const ctaClickConfigSchema = z.object({}).strict();\nexport type CtaClickConfig = z.infer<typeof ctaClickConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Registered trigger names reported by the SDK heartbeat.\n */\nexport const sdkHeartbeatTriggersSchema = z\n .object({\n automatic: z.array(z.string()),\n manual: z.array(z.string()),\n })\n .strict();\n\n/**\n * Metadata for the SDK-internal `sdk_heartbeat` event.\n *\n * The SDK fires this once per new session so the dashboard can show which SDK\n * version, install surface, and trigger registry a client site is running.\n * Consumers do not manually register or fire this event.\n */\nexport const sdkHeartbeatMetadataSchema = z\n .object({\n sdk_version: z.string(),\n package_name: z.string().nullable(),\n surface: z.enum([\"next\", \"react\", \"script\"]),\n triggers: sdkHeartbeatTriggersSchema,\n trigger_config: z.record(z.string(), z.record(z.string(), z.unknown())).nullable().optional(),\n configured_gtag_ids: z.record(z.string(), z.string()).nullable().optional(),\n })\n .strict();\n\nexport type SdkHeartbeatMetadata = z.infer<typeof sdkHeartbeatMetadataSchema>;\n\n/**\n * Internal registration config for `sdk_heartbeat`.\n *\n * This event has no consumer-facing options.\n */\nexport const sdkHeartbeatConfigSchema = z.object({}).strict();\nexport type SdkHeartbeatConfig = z.infer<typeof sdkHeartbeatConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for the automatic `form_start` event.\n *\n * The SDK emits this once per form when the visitor first focuses a field.\n */\nexport const formStartMetadataSchema = z\n .object({\n form: z\n .object({\n id: z.string(),\n action: z.string().nullable(),\n })\n .strict(),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type FormStartMetadata = z.infer<typeof formStartMetadataSchema>;\n\n/**\n * Registration config for automatic `form_start`.\n *\n * Use `selector` to narrow which forms can trigger the event. When omitted,\n * the SDK observes all `<form>` elements.\n */\nexport const formStartConfigSchema = z\n .object({\n selector: z.string().optional(),\n })\n .strict();\n\nexport type FormStartConfig = z.infer<typeof formStartConfigSchema>;\n","import { z } from \"zod\";\n\n// form_submit is manual — the SDK never auto-fires this. Consumer code calls\n// `tracking.trackEvent('form_submit', { form, page })` from their own submit\n// handler. Registering it enables the type-level permission; omitting it\n// turns manual calls into a compile error.\n//\n// The optional `fields` array captures submitted form field metadata and JSON\n// values. Consumers explicitly build the fields array themselves so they\n// control exactly what is sent.\n\n/**\n * JSON-serializable value accepted by `form_submit.fields[].value`.\n *\n * This intentionally excludes `undefined`, functions, symbols, `Date`\n * instances, and non-finite numbers. Values are stored in PostgreSQL JSONB, so\n * consumers should send only data that has a stable JSON representation.\n */\nexport type JsonValue =\n | string\n | number\n | boolean\n | null\n | JsonValue[]\n | { [key: string]: JsonValue };\n\nexport const jsonValueSchema: z.ZodType<JsonValue> = z.lazy(() =>\n z.union([\n z.string(),\n z.number().finite(),\n z.boolean(),\n z.null(),\n z.array(jsonValueSchema),\n z.record(jsonValueSchema),\n ]),\n);\n\n/**\n * Metadata for a manually fired `form_submit` event.\n *\n * Register the event with `manual: { form_submit: {} }`, then call\n * `trackEvent('form_submit', metadata)` from the host site's submit handler.\n *\n * `fields` is optional. If present, each field value must be JSON-serializable\n * and should be explicitly allowlisted by the integration. Do not send names,\n * emails, visitor phone numbers, addresses, payment data, medical details,\n * passwords, file contents, or free-text messages.\n *\n * @example\n * ```ts\n * tracking.trackEvent('form_submit', {\n * form: {\n * id: 'lead-form',\n * action: '/api/lead',\n * fields: [\n * {\n * name: 'service_interest',\n * type: 'select',\n * label: 'Service interest',\n * value: 'teeth_whitening',\n * },\n * ],\n * },\n * page: { path: window.location.pathname },\n * });\n * ```\n */\nexport const formSubmitMetadataSchema = z\n .object({\n form: z\n .object({\n id: z.string(),\n action: z.string().nullable(),\n fields: z\n .array(\n z\n .object({\n name: z.string(),\n type: z.string(),\n label: z.string().nullable(),\n value: jsonValueSchema,\n })\n .strict(),\n )\n .optional(),\n })\n .strict(),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type FormSubmitMetadata = z.infer<typeof formSubmitMetadataSchema>;\n\n/**\n * Registration config for `form_submit`.\n *\n * This event is manual-only and currently has no registration options. The\n * empty object enables typed `trackEvent('form_submit', ...)` calls.\n */\nexport const formSubmitConfigSchema = z.object({}).strict();\nexport type FormSubmitConfig = z.infer<typeof formSubmitConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for the automatic `multi_page_session` event.\n *\n * Fired when the visitor reaches the configured distinct-page threshold in a\n * single tracking session.\n */\nexport const multiPageSessionMetadataSchema = z\n .object({\n page_count: z.number().int().min(2),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type MultiPageSessionMetadata = z.infer<typeof multiPageSessionMetadataSchema>;\n\n/**\n * Registration config for automatic `multi_page_session`.\n */\nexport const multiPageSessionConfigSchema = z\n .object({\n pageThreshold: z.number().int().min(2),\n })\n .strict();\n\nexport type MultiPageSessionConfig = z.infer<typeof multiPageSessionConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for a manually fired `phone_click` event.\n *\n * `phone_number` should be the business phone number from the clicked `tel:`\n * link, not a visitor-entered phone number. `section` can distinguish header,\n * footer, hero, or contact-page links.\n */\nexport const phoneClickMetadataSchema = z\n .object({\n phone_number: z.string(),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n section: z.string().nullable().optional(),\n })\n .strict();\n\nexport type PhoneClickMetadata = z.infer<typeof phoneClickMetadataSchema>;\n\n/**\n * Registration config for `phone_click`.\n *\n * This event is manual-only and currently has no registration options.\n */\nexport const phoneClickConfigSchema = z.object({}).strict();\nexport type PhoneClickConfig = z.infer<typeof phoneClickConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for the automatic `scroll_depth` event.\n *\n * Fired once per configured threshold per page.\n */\nexport const scrollDepthMetadataSchema = z\n .object({\n depth_percent: z.number().int().min(1).max(100),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type ScrollDepthMetadata = z.infer<typeof scrollDepthMetadataSchema>;\n\n/**\n * Registration config for automatic `scroll_depth`.\n *\n * `thresholds` are integer percentages from 1 to 100.\n */\nexport const scrollDepthConfigSchema = z\n .object({\n thresholds: z.array(z.number().int().min(1).max(100)).min(1),\n })\n .strict();\n\nexport type ScrollDepthConfig = z.infer<typeof scrollDepthConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Canonical page intent names supported by `specific_page_visit`.\n */\nexport const SPECIFIC_PAGE_NAMES = [\n \"contact_page\",\n \"about_page\",\n \"services_page\",\n \"booking_page\",\n \"location_page\",\n \"pricing_page\",\n \"faq_page\",\n \"testimonials_page\",\n] as const;\n\nexport type SpecificPageName = (typeof SPECIFIC_PAGE_NAMES)[number];\n\nexport const specificPageNameSchema = z.enum(SPECIFIC_PAGE_NAMES);\n\n/**\n * Metadata for the automatic `specific_page_visit` event.\n *\n * The SDK emits this when the current pathname matches one of the configured\n * named page patterns.\n */\nexport const specificPageVisitMetadataSchema = z\n .object({\n page_name: specificPageNameSchema,\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type SpecificPageVisitMetadata = z.infer<typeof specificPageVisitMetadataSchema>;\n\n/**\n * Registration config for automatic `specific_page_visit`.\n *\n * Each page entry pairs a semantic `name` with a `RegExp` that matches the\n * pathname. Use this instead of hard-coding path regexes downstream.\n */\nexport const specificPageVisitConfigSchema = z\n .object({\n pages: z\n .array(\n z\n .object({\n name: specificPageNameSchema,\n pathPattern: z.custom<RegExp>((value) => value instanceof RegExp, {\n message: \"pathPattern must be a RegExp\",\n }),\n })\n .strict(),\n )\n .min(1),\n })\n .strict();\n\nexport type SpecificPageVisitConfig = z.infer<typeof specificPageVisitConfigSchema>;\n","import { z } from \"zod\";\n\n/**\n * Metadata for the automatic `time_on_site` event.\n *\n * The SDK starts a visibility-aware timer and fires once when visible\n * engagement crosses the configured threshold.\n */\nexport const timeOnSiteMetadataSchema = z\n .object({\n duration_ms: z.number().int().nonnegative(),\n page: z\n .object({\n path: z.string(),\n })\n .strict(),\n })\n .strict();\n\nexport type TimeOnSiteMetadata = z.infer<typeof timeOnSiteMetadataSchema>;\n\n/**\n * Registration config for automatic `time_on_site`.\n */\nexport const timeOnSiteConfigSchema = z\n .object({\n thresholdSeconds: z.number().int().positive(),\n })\n .strict();\nexport type TimeOnSiteConfig = z.infer<typeof timeOnSiteConfigSchema>;\n","import type { z } from \"zod\";\n\nimport {\n ctaClickConfigSchema,\n ctaClickMetadataSchema,\n type CtaClickConfig,\n type CtaClickMetadata,\n} from \"./cta-click\";\nimport {\n sdkHeartbeatConfigSchema,\n sdkHeartbeatMetadataSchema,\n type SdkHeartbeatConfig,\n type SdkHeartbeatMetadata,\n} from \"./sdk-heartbeat\";\nimport {\n formStartConfigSchema,\n formStartMetadataSchema,\n type FormStartConfig,\n type FormStartMetadata,\n} from \"./form-start\";\nimport {\n formSubmitConfigSchema,\n formSubmitMetadataSchema,\n type FormSubmitConfig,\n type FormSubmitMetadata,\n} from \"./form-submit\";\nimport {\n multiPageSessionConfigSchema,\n multiPageSessionMetadataSchema,\n type MultiPageSessionConfig,\n type MultiPageSessionMetadata,\n} from \"./multi-page-session\";\nimport {\n pageViewConfigSchema,\n pageViewMetadataSchema,\n type PageViewConfig,\n type PageViewMetadata,\n} from \"./page-view\";\nimport {\n phoneClickConfigSchema,\n phoneClickMetadataSchema,\n type PhoneClickConfig,\n type PhoneClickMetadata,\n} from \"./phone-click\";\nimport {\n scrollDepthConfigSchema,\n scrollDepthMetadataSchema,\n type ScrollDepthConfig,\n type ScrollDepthMetadata,\n} from \"./scroll-depth\";\nimport {\n specificPageVisitConfigSchema,\n specificPageVisitMetadataSchema,\n type SpecificPageVisitConfig,\n type SpecificPageVisitMetadata,\n} from \"./specific-page-visit\";\nimport {\n timeOnSiteConfigSchema,\n timeOnSiteMetadataSchema,\n type TimeOnSiteConfig,\n type TimeOnSiteMetadata,\n} from \"./time-on-site\";\n\n// Event kind — automatic events are fired by the SDK itself when their\n// client-side signal fires (page_view on navigation, time_on_site on timer,\n// etc). Manual events are only fireable via explicit consumer code.\nexport type EventKind = \"automatic\" | \"manual\";\n\n// The registry is the single client-side source of truth for \"what events\n// exist, what shape does their metadata take, and what kind are they\". The\n// backend mirror lives in apps/api/src/schemas/tracking_events.py and is\n// kept in sync via the drift test in apps/api/tests/test_event_schema_drift.py.\nexport const EVENT_REGISTRY = {\n // --- automatic triggers ---\n page_view: {\n kind: \"automatic\",\n metadataSchema: pageViewMetadataSchema,\n configSchema: pageViewConfigSchema,\n },\n time_on_site: {\n kind: \"automatic\",\n metadataSchema: timeOnSiteMetadataSchema,\n configSchema: timeOnSiteConfigSchema,\n },\n specific_page_visit: {\n kind: \"automatic\",\n metadataSchema: specificPageVisitMetadataSchema,\n configSchema: specificPageVisitConfigSchema,\n },\n scroll_depth: {\n kind: \"automatic\",\n metadataSchema: scrollDepthMetadataSchema,\n configSchema: scrollDepthConfigSchema,\n },\n multi_page_session: {\n kind: \"automatic\",\n metadataSchema: multiPageSessionMetadataSchema,\n configSchema: multiPageSessionConfigSchema,\n },\n form_start: {\n kind: \"automatic\",\n metadataSchema: formStartMetadataSchema,\n configSchema: formStartConfigSchema,\n },\n // --- SDK-internal automatic (not consumer-configurable) ---\n sdk_heartbeat: {\n kind: \"automatic\",\n metadataSchema: sdkHeartbeatMetadataSchema,\n configSchema: sdkHeartbeatConfigSchema,\n },\n // --- manual triggers ---\n form_submit: {\n kind: \"manual\",\n metadataSchema: formSubmitMetadataSchema,\n configSchema: formSubmitConfigSchema,\n },\n phone_click: {\n kind: \"manual\",\n metadataSchema: phoneClickMetadataSchema,\n configSchema: phoneClickConfigSchema,\n },\n cta_click: {\n kind: \"manual\",\n metadataSchema: ctaClickMetadataSchema,\n configSchema: ctaClickConfigSchema,\n },\n} as const;\n\n/**\n * Name of any event known to the tracking SDK.\n */\nexport type EventName = keyof typeof EVENT_REGISTRY;\n\n/**\n * Event names that are fired by the SDK when their configured signal occurs.\n *\n * Automatic events are not accepted by the typed `trackEvent()` API.\n */\nexport type AutomaticEventName = {\n [K in EventName]: (typeof EVENT_REGISTRY)[K][\"kind\"] extends \"automatic\" ? K : never;\n}[EventName];\n\n/**\n * Event names that consumer code can fire manually after registering them.\n */\nexport type ManualEventName = {\n [K in EventName]: (typeof EVENT_REGISTRY)[K][\"kind\"] extends \"manual\" ? K : never;\n}[EventName];\n\n// Map an event name to its explicit metadata / config TS type. We pull the\n// inferred types directly from the per-event files rather than deriving\n// via `z.infer<typeof EVENT_REGISTRY[K]['metadataSchema']>` so that error\n// messages name the event-specific type (PageViewMetadata, not an\n// anonymous zod inference).\ntype MetadataByName = {\n page_view: PageViewMetadata;\n time_on_site: TimeOnSiteMetadata;\n specific_page_visit: SpecificPageVisitMetadata;\n scroll_depth: ScrollDepthMetadata;\n multi_page_session: MultiPageSessionMetadata;\n form_start: FormStartMetadata;\n sdk_heartbeat: SdkHeartbeatMetadata;\n form_submit: FormSubmitMetadata;\n phone_click: PhoneClickMetadata;\n cta_click: CtaClickMetadata;\n};\n\ntype ConfigByName = {\n page_view: PageViewConfig;\n time_on_site: TimeOnSiteConfig;\n specific_page_visit: SpecificPageVisitConfig;\n scroll_depth: ScrollDepthConfig;\n multi_page_session: MultiPageSessionConfig;\n form_start: FormStartConfig;\n sdk_heartbeat: SdkHeartbeatConfig;\n form_submit: FormSubmitConfig;\n phone_click: PhoneClickConfig;\n cta_click: CtaClickConfig;\n};\n\n/**\n * Metadata payload type for a specific tracking event.\n *\n * @example\n * ```ts\n * type SubmitMetadata = EventMetadata<'form_submit'>;\n * ```\n */\nexport type EventMetadata<K extends EventName> = MetadataByName[K];\n\n/**\n * Trigger registration config type for a specific tracking event.\n */\nexport type EventConfig<K extends EventName> = ConfigByName[K];\n\n// Runtime constant arrays for iteration at consumer / factory time.\n/**\n * Runtime list of automatic event names.\n */\nexport const ALL_AUTOMATIC_EVENT_NAMES: readonly AutomaticEventName[] = (\n Object.entries(EVENT_REGISTRY) as Array<[EventName, (typeof EVENT_REGISTRY)[EventName]]>\n)\n .filter(([, def]) => def.kind === \"automatic\")\n .map(([name]) => name) as AutomaticEventName[];\n\n/**\n * Runtime list of manual event names.\n */\nexport const ALL_MANUAL_EVENT_NAMES: readonly ManualEventName[] = (\n Object.entries(EVENT_REGISTRY) as Array<[EventName, (typeof EVENT_REGISTRY)[EventName]]>\n)\n .filter(([, def]) => def.kind === \"manual\")\n .map(([name]) => name) as ManualEventName[];\n\n/**\n * Trigger registry passed to `createTracking({ triggers })`.\n *\n * `automatic.page_view` is required because every install should capture page\n * views. Other automatic events are opt-in. Manual events must be registered\n * here before the typed client accepts `trackEvent()` calls for them.\n *\n * @example\n * ```ts\n * createTracking({\n * apiKey,\n * endpoint,\n * triggers: {\n * automatic: {\n * page_view: {},\n * time_on_site: { thresholdSeconds: 60 },\n * },\n * manual: {\n * form_submit: {},\n * phone_click: {},\n * },\n * },\n * });\n * ```\n */\nexport type TriggerRegistryConfig = {\n automatic: {\n page_view: EventConfig<\"page_view\">;\n } & Partial<{\n time_on_site: EventConfig<\"time_on_site\">;\n specific_page_visit: EventConfig<\"specific_page_visit\">;\n scroll_depth: EventConfig<\"scroll_depth\">;\n multi_page_session: EventConfig<\"multi_page_session\">;\n form_start: EventConfig<\"form_start\">;\n }>;\n manual?: Partial<{\n form_submit: EventConfig<\"form_submit\">;\n phone_click: EventConfig<\"phone_click\">;\n cta_click: EventConfig<\"cta_click\">;\n }>;\n};\n\n/**\n * Manual event names registered in a concrete trigger registry.\n *\n * Used by `TypedTrackingClient` so `trackEvent()` only accepts events the\n * consumer explicitly enabled.\n */\nexport type RegisteredManualEvents<TRegistry extends TriggerRegistryConfig> = Extract<\n keyof NonNullable<TRegistry[\"manual\"]>,\n ManualEventName\n>;\n\n/**\n * Automatic event names registered in a concrete trigger registry.\n */\nexport type RegisteredAutomaticEvents<TRegistry extends TriggerRegistryConfig> = Extract<\n keyof TRegistry[\"automatic\"],\n AutomaticEventName\n>;\n\n/**\n * Discriminated union of valid manual tracking calls for a registry.\n */\nexport type TrackableEvent<TRegistry extends TriggerRegistryConfig> = {\n [K in RegisteredManualEvents<TRegistry>]: {\n eventType: K;\n metadata: EventMetadata<K>;\n };\n}[RegisteredManualEvents<TRegistry>];\n\n// Runtime helper: look up a schema pair by name. Cast through `unknown`\n// because the registry is `as const` and TS loses the specific schema type\n// when indexing via a dynamic key.\nexport function getEventDefinition(name: EventName): {\n kind: EventKind;\n metadataSchema: z.ZodTypeAny;\n configSchema: z.ZodTypeAny;\n} {\n return EVENT_REGISTRY[name] as unknown as {\n kind: EventKind;\n metadataSchema: z.ZodTypeAny;\n configSchema: z.ZodTypeAny;\n };\n}\n","import { z } from \"zod\";\n\n/**\n * Sales / Conversions wire schemas — the client-side source of truth.\n *\n * `saleCreateSchema` is mirrored by `SaleCreateSchema` in\n * `apps/api/src/schemas/tracking_sales.py` and enforced by the backend drift\n * test (the `resources` section of `events.schema.json`). Keep them in lockstep.\n *\n * Money is **integer minor units (cents)**; `quantity` is a decimal string;\n * `currency` is the required `SupportedCurrency` enum.\n */\n\nexport const SUPPORTED_CURRENCIES = [\"USD\", \"CAD\"] as const;\nexport type SupportedCurrency = (typeof SUPPORTED_CURRENCIES)[number];\n\nconst TRACKING_ENVIRONMENTS = [\"production\", \"development\"] as const;\n\nconst currencySchema = z.enum(SUPPORTED_CURRENCIES);\nconst centsSchema = z.number().int().nonnegative();\nconst quantitySchema = z.string().regex(/^\\d+(\\.\\d{1,3})?$/);\n// Free-form JSON object — the sale's extensibility escape hatch. Mirrors the\n// Pydantic `dict[str, Any]` (`additionalProperties: true`) on the wire.\nconst metadataSchema = z.record(z.unknown());\n\nexport const saleItemSchema = z\n .object({\n external_item_id: z.string().nullable().optional(),\n name: z.string().nullable().optional(),\n category: z.string().nullable().optional(),\n quantity: quantitySchema,\n unit_price_cents: centsSchema,\n // Non-negativity validated on the wire — same contract as the other cents\n // fields — and backstopped by the DB CHECK.\n unit_cost_cents: centsSchema.nullable().optional(),\n })\n .strict();\n\n// One service covered by a sale, at its own price. `service` is the per-business\n// service key, validated against the taxonomy by the backend at write time.\nexport const saleServiceSchema = z\n .object({\n service: z.string(),\n amount_cents: centsSchema,\n })\n .strict();\n\n// Raw customer identity. Stored as-is on the backend for human display in\n// dashboards; hashing happens at writeback-upload time in the future Google\n// Ads reconciliation worker (see docs/tracking-package/conversion-writeback.md).\n// The SDK never hashes — pass values straight through.\nconst customerNameSchema = z.string().max(200);\nconst customerPhoneSchema = z.string().max(64);\nconst customerEmailSchema = z.string().max(320).email();\n\n// The singular `service` (priced by `amount_total_cents`) and the plural\n// `services` (each priced individually) are mutually exclusive. When `services`\n// is present, `amount_total_cents` is optional and the backend derives it as the\n// sum; otherwise it is required. Shared by create + update.\nfunction refineServiceXor(\n val: {\n service?: string | null;\n services?: ReadonlyArray<{ service: string; amount_cents: number }> | null;\n amount_total_cents?: number | null;\n },\n ctx: z.RefinementCtx,\n { requireAmount }: { requireAmount: boolean },\n): void {\n if (val.services != null) {\n if (val.service != null) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: \"pass either `service` or `services`, not both\",\n path: [\"services\"],\n });\n }\n if (val.services.length === 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: \"`services` must not be empty\",\n path: [\"services\"],\n });\n }\n const keys = val.services.map((s) => s.service);\n if (new Set(keys).size !== keys.length) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: \"`services` must not list the same service more than once\",\n path: [\"services\"],\n });\n }\n if (val.amount_total_cents != null) {\n const sum = val.services.reduce((acc, s) => acc + s.amount_cents, 0);\n if (val.amount_total_cents !== sum) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message:\n \"amount_total_cents must equal the sum of the services amounts \" +\n \"(omit it to derive it automatically)\",\n path: [\"amount_total_cents\"],\n });\n }\n }\n } else if (requireAmount && val.amount_total_cents == null) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: \"amount_total_cents is required unless `services` is provided\",\n path: [\"amount_total_cents\"],\n });\n }\n}\n\nexport const saleCreateSchema = z\n .object({\n external_id: z.string().nullable().optional(),\n description: z.string().nullable().optional(),\n service: z.string().nullable().optional(),\n services: z.array(saleServiceSchema).nullable().optional(),\n currency: currencySchema,\n // Optional only because the plural `services` form derives it from the sum\n // (see refineServiceXor); the singular/serviceless path still requires it.\n amount_total_cents: centsSchema.nullable().optional(),\n occurred_at: z.string().datetime(),\n environment: z.enum(TRACKING_ENVIRONMENTS).default(\"production\"),\n items: z.array(saleItemSchema).default([]),\n metadata: metadataSchema.nullable().optional(),\n customer_name: customerNameSchema.nullable().optional(),\n customer_phone: customerPhoneSchema.nullable().optional(),\n customer_email: customerEmailSchema.nullable().optional(),\n })\n .strict()\n .superRefine((val, ctx) => refineServiceXor(val, ctx, { requireAmount: true }));\n\nexport const saleUpdateSchema = z\n .object({\n description: z.string().nullable().optional(),\n service: z.string().nullable().optional(),\n services: z.array(saleServiceSchema).nullable().optional(),\n currency: currencySchema.optional(),\n amount_total_cents: centsSchema.optional(),\n occurred_at: z.string().datetime().optional(),\n items: z.array(saleItemSchema).optional(),\n metadata: metadataSchema.nullable().optional(),\n customer_name: customerNameSchema.nullable().optional(),\n customer_phone: customerPhoneSchema.nullable().optional(),\n customer_email: customerEmailSchema.nullable().optional(),\n })\n .strict()\n .superRefine((val, ctx) => refineServiceXor(val, ctx, { requireAmount: false }));\n\nexport type SaleItemInput = z.input<typeof saleItemSchema>;\nexport type SaleServiceInput = z.input<typeof saleServiceSchema>;\n// Use the INPUT type so fields with Zod defaults (environment, items) and\n// occurred_at are optional for callers — the sales client fills them in.\nexport type SaleInput = z.input<typeof saleCreateSchema>;\nexport type SaleUpdateInput = z.input<typeof saleUpdateSchema>;\n\n// ---------------------------------------------------------------------------\n// Read shapes (server responses; not validated on the client)\n// ---------------------------------------------------------------------------\n\nexport interface SaleItem {\n id: string;\n external_item_id: string | null;\n name: string | null;\n category: string | null;\n quantity: string;\n unit_price_cents: number;\n unit_cost_cents: number | null;\n}\n\nexport interface SaleService {\n id: string;\n service_id: string;\n service_key: string;\n service_label: string;\n amount_cents: number;\n}\n\nexport interface Sale {\n id: string;\n business_id: string;\n business_name?: string | null;\n external_id: string | null;\n currency: SupportedCurrency;\n amount_total_cents: number;\n description: string | null;\n // Singular service fields are populated only for single-service sales (kept\n // for backwards compatibility); `services` is always the full set.\n service_id: string | null;\n service_key?: string | null;\n service_label?: string | null;\n services: SaleService[];\n occurred_at: string;\n environment: (typeof TRACKING_ENVIRONMENTS)[number];\n metadata: Record<string, unknown> | null;\n customer_name: string | null;\n customer_phone: string | null;\n customer_email: string | null;\n created_at: string;\n updated_at: string;\n items: SaleItem[];\n}\n\nexport interface SaleListPage {\n items: Sale[];\n total: number;\n}\n\nexport interface SaleCursorPage {\n items: Sale[];\n next_cursor: string | null;\n /** Populated only when `want_total` was requested (a single indexed COUNT). */\n total_count?: number;\n has_more?: boolean;\n}\n\n/**\n * Sortable columns on the secret-key (keyset) and admin (offset) list endpoints.\n * `business_name` only applies to the cross-business admin list — it sorts the\n * joined `businesses.name` column.\n */\nexport type SaleSortField =\n | \"occurred_at\"\n | \"created_at\"\n | \"amount_total_cents\"\n | \"customer_name\"\n | \"business_name\";\n\nexport type SaleSortOrder = \"asc\" | \"desc\";\n\n/**\n * Comprehensive filter shape mirrored from the backend's `SaleQueryFilters`.\n *\n * `search` runs case-insensitively across `customer_name`, `customer_phone`,\n * `customer_email`, `description`, and `external_id` — the human-facing\n * columns. `service_id` is the resolved per-business service UUID (different\n * from the create-time `service` *key*).\n */\nexport interface SaleFilters {\n business_id?: string;\n external_id?: string;\n service_id?: string;\n /** E.164 phone — the canonical customer key; filters sales for one customer. */\n customer_phone?: string;\n currency?: SupportedCurrency;\n environment?: (typeof TRACKING_ENVIRONMENTS)[number];\n since?: string;\n until?: string;\n min_amount_cents?: number;\n max_amount_cents?: number;\n search?: string;\n}\n\n/**\n * Full query input for `SalesClient.list()` — filters + pagination.\n *\n * Pagination is **keyset (cursor)**: `next_cursor` returned by one page is\n * passed back as `cursor` on the next. `null` / undefined cursor = first page.\n *\n * Ordering on this endpoint is fixed at **`occurred_at DESC, id DESC`** — the\n * cursor encodes a position in that index, so a different sort would\n * invalidate cursors mid-pagination. For ad-hoc sorted reads use the\n * dashboard admin endpoint, which is offset-paginated.\n */\nexport interface SaleListQuery extends SaleFilters {\n limit?: number;\n cursor?: string | null;\n}\n\n/** Keyset-safe sort columns (NOT-NULL, indexed). */\nexport type SaleKeysetSortField = \"occurred_at\" | \"created_at\" | \"amount_total_cents\";\n\n/** v2 list query — adds server sort + opt-in total. */\nexport interface SaleListQueryV2 extends SaleListQuery {\n sort?: SaleKeysetSortField;\n order?: SaleSortOrder;\n /** Opt-in: a single indexed COUNT over the filtered set. */\n want_total?: boolean;\n}\n\n// ---------------------------------------------------------------------------\n// Sales summary (aggregations). Read by `SalesClient.summary()`. Every monetary\n// figure is grouped **by currency** — USD and CAD cents are never summed into a\n// single total. Money stays integer cents; only `quantity` is a decimal string.\n// ---------------------------------------------------------------------------\n\nexport const TRACKING_RANGES = [\"24h\", \"7d\", \"30d\"] as const;\nexport type TrackingOverviewRange = (typeof TRACKING_RANGES)[number];\n\n/** Query input for `SalesClient.summary()` — filters + range + options. */\nexport interface SaleSummaryQuery extends SaleFilters {\n range: TrackingOverviewRange;\n /** Include the per-category line-item breakdown (extra join; default false). */\n include_categories?: boolean;\n /** Surface soft-deleted services individually (flagged \"(deleted)\") instead of\n * rolling them into a single \"Deleted services\" bucket. Default false. */\n include_deleted_services?: boolean;\n /** Cap on the by-service / by-category rows (1–50, default 10). */\n top_n?: number;\n}\n\nexport interface CurrencyRevenue {\n currency: SupportedCurrency;\n sale_count: number;\n revenue_cents: number;\n average_order_value_cents: number;\n}\n\nexport interface SalesServiceBreakdown {\n service_id: string | null;\n service_key: string | null;\n /** \"Unassigned\" when the sale has no service. */\n service_label: string | null;\n currency: SupportedCurrency;\n sale_count: number;\n revenue_cents: number;\n}\n\nexport interface SalesCategoryBreakdown {\n category: string | null;\n currency: SupportedCurrency;\n /** sum(unit_price_cents * quantity) — advisory, not authoritative. */\n revenue_cents: number;\n /** Decimal string. */\n quantity: string;\n}\n\nexport interface SalesTrendPoint {\n bucket_start: string;\n currency: SupportedCurrency;\n sale_count: number;\n revenue_cents: number;\n}\n\nexport interface SaleSummary {\n range: TrackingOverviewRange;\n business_id: string | null;\n metrics: {\n sale_count: number;\n distinct_customers: number;\n by_currency: CurrencyRevenue[];\n };\n by_service: SalesServiceBreakdown[];\n by_currency: CurrencyRevenue[];\n by_category: SalesCategoryBreakdown[];\n series: SalesTrendPoint[];\n}\n\n// ---------------------------------------------------------------------------\n// v2 dashboard surface (additive, back-compat). Reads only → plain interfaces,\n// not drift-checked. Legacy `24h/7d/30d` callers get the same numbers; new\n// ranges/options layer on tz-correct calendar windows + comparisons.\n// ---------------------------------------------------------------------------\n\nexport const NAMED_RANGES = [\n \"today\",\n \"yesterday\",\n \"wtd\",\n \"mtd\",\n \"qtd\",\n \"ytd\",\n \"24h\",\n \"7d\",\n \"30d\",\n \"90d\",\n \"custom\",\n] as const;\n/** Superset of `TrackingOverviewRange` (`24h`/`7d`/`30d` stay valid). */\nexport type NamedRange = (typeof NAMED_RANGES)[number];\nexport type Granularity = \"hour\" | \"day\" | \"week\" | \"month\" | \"auto\";\nexport type CompareTo = \"previous_period\" | \"previous_year\" | \"none\";\n\nexport interface DistinctCustomersByCurrency {\n currency: SupportedCurrency;\n distinct_customers: number;\n}\n\nexport interface SummaryWindow {\n since: string;\n until: string;\n}\n\nexport interface SummaryCurrencyDelta {\n currency: SupportedCurrency;\n revenue_cents_delta: number;\n /** Ratio (0.12 = +12%); null when previous revenue was 0. */\n revenue_pct_delta: number | null;\n sale_count_delta: number;\n}\n\nexport interface SummaryDeltas {\n by_currency: SummaryCurrencyDelta[];\n sale_count_delta: number;\n distinct_customers_delta: number;\n}\n\nexport interface SaleSummaryPrevious {\n window: SummaryWindow;\n metrics: SaleSummaryV2[\"metrics\"];\n by_currency: CurrencyRevenue[];\n series: SalesTrendPoint[];\n}\n\n/** Query input for `SalesClient.summary()` v2 — widened range + tz/compare options. */\nexport interface SaleSummaryQueryV2 extends SaleFilters {\n range?: NamedRange;\n /** Required when `range === 'custom'`. */\n since?: string;\n until?: string;\n /** IANA tz, e.g. `America/Toronto`. Default = business tz. */\n timezone?: string;\n granularity?: Granularity;\n compare_to?: CompareTo;\n include_categories?: boolean;\n include_deleted_services?: boolean;\n top_n?: number;\n}\n\n/** Superset of `SaleSummary` (assignable to it). */\nexport interface SaleSummaryV2 extends Omit<SaleSummary, \"range\" | \"metrics\"> {\n range: NamedRange;\n metrics: SaleSummary[\"metrics\"] & {\n distinct_customers_by_currency: DistinctCustomersByCurrency[];\n };\n timezone: string | null;\n window: SummaryWindow | null;\n granularity: string | null;\n previous: SaleSummaryPrevious | null;\n deltas: SummaryDeltas | null;\n}\n\n// ---------------------------------------------------------------------------\n// Customers — phone-keyed (a customer IS their E.164 phone). `customer_id` is\n// the E.164 string. Money is per-currency, never summed.\n// ---------------------------------------------------------------------------\n\nexport type CustomerSegment = \"new\" | \"returning\" | \"repeat\" | \"lapsed\";\nexport type CustomerSortField = \"total_spent\" | \"last_purchase\" | \"purchases\" | \"first_purchase\";\n\nexport interface CustomerCurrencyTotal {\n currency: SupportedCurrency;\n revenue_cents: number;\n sale_count: number;\n average_order_value_cents: number;\n}\n\nexport interface CustomerSummary {\n /** The E.164 phone — the canonical customer id. */\n customer_id: string;\n display_name: string | null;\n email: string | null;\n phone: string;\n first_purchase_at: string;\n last_purchase_at: string;\n purchase_count: number;\n segment: CustomerSegment;\n totals: CustomerCurrencyTotal[];\n}\n\nexport type CustomerProfile = CustomerSummary;\n\nexport interface CustomerListQuery extends Omit<SaleFilters, \"external_id\"> {\n segment?: CustomerSegment;\n sort?: CustomerSortField;\n order?: SaleSortOrder;\n cursor?: string | null;\n limit?: number;\n want_total?: boolean;\n}\n\nexport interface CustomerListPage {\n items: CustomerSummary[];\n next_cursor: string | null;\n total_count: number | null;\n}\n\nexport interface CustomerGetOptions {\n include_sales?: boolean;\n limit?: number;\n cursor?: string | null;\n}\n\nexport interface CustomerGetResult {\n customer: CustomerProfile;\n /** Present only when `include_sales: true` (and a business-scoped key). */\n sales?: SaleCursorPage | null;\n}\n\nexport interface CustomerSummaryQuery extends SaleFilters {\n range?: NamedRange;\n since?: string;\n until?: string;\n timezone?: string;\n compare_to?: CompareTo;\n}\n\nexport interface CustomerSegmentCount {\n segment: CustomerSegment;\n count: number;\n}\n\nexport interface CustomerCurrencyDelta {\n currency: SupportedCurrency;\n revenue_cents_delta: number;\n /** Ratio (0.12 = +12%); null when previous revenue was 0. */\n revenue_pct_delta: number | null;\n}\n\nexport interface CustomerKpisDeltas {\n total_customers_delta: number;\n new_customers_delta: number;\n returning_customers_delta: number;\n repeat_rate_delta: number;\n ltv_by_currency: CustomerCurrencyDelta[];\n}\n\nexport interface CustomerKpisPrevious {\n window_since: string;\n window_until: string;\n total_customers: number;\n new_customers: number;\n returning_customers: number;\n repeat_rate: number;\n ltv_by_currency: CustomerCurrencyTotal[];\n}\n\nexport interface CustomerKpis {\n range: NamedRange;\n timezone: string;\n window_since: string;\n window_until: string;\n total_customers: number;\n new_customers: number;\n returning_customers: number;\n repeat_rate: number;\n by_segment: CustomerSegmentCount[];\n ltv_by_currency: CustomerCurrencyTotal[];\n /** Present only when `compare_to` is set. */\n previous?: CustomerKpisPrevious | null;\n deltas?: CustomerKpisDeltas | null;\n}\n\n// ---------------------------------------------------------------------------\n// Business config — supersets `fetchServices()`.\n// ---------------------------------------------------------------------------\n\nexport interface BusinessConfigService {\n key: string;\n label: string;\n archived: boolean;\n}\n\nexport interface BusinessConfigFeatures {\n customers: boolean;\n comparisons: boolean;\n retention: boolean;\n}\n\nexport interface BusinessConfig {\n business_id: string;\n display_name: string;\n timezone: string;\n primary_currency: SupportedCurrency;\n currencies: SupportedCurrency[];\n default_phone_country: string | null;\n services: BusinessConfigService[];\n features: BusinessConfigFeatures;\n}\n","// Framework-agnostic phone-number utilities — isomorphic (browser + Node/RSC),\n// zero React. Bundled `libphonenumber-js` (standard metadata) so clients add no\n// dependency. The transmitted value is ALWAYS E.164; display is the only knob.\n\nimport { AsYouType, parsePhoneNumberFromString, type CountryCode } from \"libphonenumber-js\";\n\nexport type { CountryCode };\n\n/**\n * How a phone number is shown in the UI. The transmitted value is always E.164\n * and is deliberately NOT part of this — only the display format is configurable.\n * A function form covers the long tail (`(parsed) => string`).\n */\nexport type PhoneDisplayFormat =\n | \"national\"\n | \"international\"\n | \"e164\"\n | ((parsed: ParsedPhone) => string);\n\nexport interface ParsedPhone {\n /** E.164 (`\"+14165550199\"`) or `null` when the input isn't a valid number. This is what gets transmitted. */\n e164: string | null;\n /** National display form (`\"(416) 555-0199\"`); empty string when unparseable. */\n national: string;\n /** International display form (`\"+1 416 555 0199\"`); empty string when unparseable. */\n international: string;\n /** ISO-3166 country resolved by libphonenumber, or `null`. */\n country: CountryCode | null;\n isValid: boolean;\n}\n\n/** Region assumed for numbers typed without a country code. */\nexport const DEFAULT_PHONE_COUNTRY: CountryCode = \"CA\";\n\n/** Parse a raw/display string into every representation at once (one parse → display + wire never drift). */\nexport function parsePhone(raw: string, country?: CountryCode): ParsedPhone {\n const region = country ?? DEFAULT_PHONE_COUNTRY;\n const parsed = parsePhoneNumberFromString(raw ?? \"\", region);\n if (!parsed) {\n return { e164: null, national: \"\", international: \"\", country: region, isValid: false };\n }\n const isValid = parsed.isValid();\n return {\n // E.164 is only surfaced for a *valid* number — a possible-but-invalid input\n // (e.g. too few digits) still parses but must not be transmitted.\n e164: isValid ? parsed.number : null,\n national: parsed.formatNational(),\n international: parsed.formatInternational(),\n country: parsed.country ?? region,\n isValid,\n };\n}\n\n/** Normalize any raw/display value to E.164, or `null` if it isn't a valid number. */\nexport function toE164(raw: string, country?: CountryCode): string | null {\n return parsePhone(raw, country).e164;\n}\n\n/** Format a value for display. Defaults to `'national'`. Never affects the wire value. */\nexport function formatPhone(\n value: string,\n format: PhoneDisplayFormat = \"national\",\n country?: CountryCode,\n): string {\n const parsed = parsePhone(value, country);\n if (typeof format === \"function\") return format(parsed);\n switch (format) {\n case \"international\":\n return parsed.international || value;\n case \"e164\":\n return parsed.e164 ?? value;\n case \"national\":\n default:\n return parsed.national || value;\n }\n}\n\n/** Live, incremental formatting for an `<input>` as the user types (`AsYouType`). */\nexport function formatPhoneAsTyped(raw: string, country?: CountryCode): string {\n return new AsYouType(country ?? DEFAULT_PHONE_COUNTRY).input(raw ?? \"\");\n}\n","// Layer 0 (init config) + Layer 3 (tracking integration) for phone fields.\n// Kept in core so both the React and Next packages share identical types, and\n// the `phoneField` helper stays isomorphic.\n\nimport type { JsonValue } from \"./events/form-submit\";\nimport { toE164, type CountryCode, type PhoneDisplayFormat } from \"./phone\";\n\n/**\n * Init-time phone config (`createTracking({ phone })`). The transmitted value is\n * ALWAYS E.164 and is deliberately not configurable here — only display is.\n */\nexport interface PhoneConfig {\n /** Region assumed for numbers typed without a country code. Default `'CA'`. */\n defaultCountry?: CountryCode;\n /** How the input DISPLAYS to the user. Default `'national'`. Does not affect the wire. */\n display?: PhoneDisplayFormat;\n}\n\n/** A single tracked form field destined for `form_submit.fields[]`. */\nexport interface TrackedField {\n name: string;\n type: string;\n value: JsonValue;\n label?: string | null;\n}\n\n/**\n * Build a tracked field whose wire value is ALWAYS E.164. The client keeps its\n * own display value for UI/email; this puts `+E.164` on the wire (or `null` when\n * the input isn't a valid number).\n */\nexport function phoneField(name: string, raw: string, country?: CountryCode): TrackedField {\n return { name, type: \"phone\", value: toE164(raw, country) };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,oBAAoB;AAuBjC,IAAM,mBAAmB,oBAAI,IAA4B;AAMlD,SAAS,iBAAiB,UAA8C;AAC7E,mBAAiB,IAAI,QAAQ;AAC7B,SAAO,MAAM;AACX,qBAAiB,OAAO,QAAQ;AAAA,EAClC;AACF;AAoBO,SAAS,kBAAgC;AAC9C,MAAI,OAAO,WAAW,YAAa,QAAO;AAE1C,MAAI;AACF,UAAM,cAAc,OAAO,aAAa,QAAQ,iBAAiB;AACjE,QAAI,gBAAgB,aAAa,gBAAgB,SAAU,QAAO;AAAA,EACpE,QAAQ;AAAA,EAER;AAEA,SAAO;AACT;;;ACoCA,IAAM,aAAa;AAGZ,SAAS,cAAc,QAAyB;AACrD,SAAO,WAAW,KAAK,MAAM;AAC/B;AAkBO,SAAS,mBAAmB,OAAqC;AACtE,MAAI,OAAO,WAAW,eAAe,OAAO,OAAO,SAAS,WAAY,QAAO;AAC/E,MAAI,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OACA,SAA6B,YAC7B,SACQ;AACR,QAAM,SAAS,WAAW,OAAO,OAAO;AACxC,MAAI,OAAO,WAAW,WAAY,QAAO,OAAO,MAAM;AACtD,UAAQ,QAAQ;AAAA,IACd,KAAK;AACH,aAAO,OAAO,iBAAiB;AAAA,IACjC,KAAK;AACH,aAAO,OAAO,QAAQ;AAAA,IACxB,KAAK;AAAA,IACL;AACE,aAAO,OAAO,YAAY;AAAA,EAC9B;AACF;AAGO,SAAS,mBAAmB,KAAa,SAA+B;AAC7E,SAAO,IAAI,mCAAU,WAAW,qBAAqB,EAAE,MAAM,OAAO,EAAE;AACxE;;;ACjDO,SAAS,WAAW,MAAc,KAAa,SAAqC;AACzF,SAAO,EAAE,MAAM,MAAM,SAAS,OAAO,OAAO,KAAK,OAAO,EAAE;AAC5D;","names":["import_zod","import_zod","import_zod","import_zod","import_zod","import_zod","import_zod","import_zod","import_zod","import_zod"]}
package/dist/phone.mjs CHANGED
@@ -1,3 +1,76 @@
1
+ // ../tracking-core/src/consent.ts
2
+ var CONSENT_STATE_KEY = "consent_state";
3
+ var grantedListeners = /* @__PURE__ */ new Set();
4
+ function onConsentGranted(listener) {
5
+ grantedListeners.add(listener);
6
+ return () => {
7
+ grantedListeners.delete(listener);
8
+ };
9
+ }
10
+ function getConsentState() {
11
+ if (typeof window === "undefined") return "pending";
12
+ try {
13
+ const storedState = window.localStorage.getItem(CONSENT_STATE_KEY);
14
+ if (storedState === "granted" || storedState === "denied") return storedState;
15
+ } catch {
16
+ }
17
+ return "pending";
18
+ }
19
+
20
+ // ../tracking-core/src/gtag.ts
21
+ var SEND_TO_RE = /^AW-[A-Za-z0-9]+\/[A-Za-z0-9_-]+$/;
22
+ function isValidSendTo(sendTo) {
23
+ return SEND_TO_RE.test(sendTo);
24
+ }
25
+ function fireGtagConversion(input) {
26
+ if (typeof window === "undefined" || typeof window.gtag !== "function") return false;
27
+ if (!isValidSendTo(input.sendTo)) return false;
28
+ const params = { send_to: input.sendTo };
29
+ if (input.value != null) params.value = input.value;
30
+ if (input.currency) params.currency = input.currency;
31
+ if (input.transactionId) params.transaction_id = input.transactionId;
32
+ try {
33
+ window.gtag("event", "conversion", params);
34
+ return true;
35
+ } catch {
36
+ return false;
37
+ }
38
+ }
39
+
40
+ // ../tracking-core/src/resources/conversion-firing.ts
41
+ var DEDUP_PREFIX = "_aranova_conv_";
42
+ var pendingQueue = [];
43
+ function dedupKey(input) {
44
+ return `${DEDUP_PREFIX}${input.transactionId ?? ""}:${input.sendTo}`;
45
+ }
46
+ function alreadyFired(input) {
47
+ if (!input.transactionId || typeof window === "undefined") return false;
48
+ try {
49
+ return window.sessionStorage.getItem(dedupKey(input)) !== null;
50
+ } catch {
51
+ return false;
52
+ }
53
+ }
54
+ function markFired(input) {
55
+ if (!input.transactionId || typeof window === "undefined") return;
56
+ try {
57
+ window.sessionStorage.setItem(dedupKey(input), "1");
58
+ } catch {
59
+ }
60
+ }
61
+ function fireOnce(input) {
62
+ if (alreadyFired(input)) return;
63
+ if (fireGtagConversion(input)) markFired(input);
64
+ }
65
+ function flushPendingConversions() {
66
+ if (getConsentState() !== "granted") return;
67
+ while (pendingQueue.length > 0) {
68
+ const input = pendingQueue.shift();
69
+ if (input) fireOnce(input);
70
+ }
71
+ }
72
+ if (typeof window !== "undefined") onConsentGranted(flushPendingConversions);
73
+
1
74
  // ../tracking-core/src/session.ts
2
75
  var SESSION_IDLE_MS = 30 * 60 * 1e3;
3
76