/** * Pixel Runtime V2 Types * * V2 uses a self-registration model where standalone SDK scripts * (in public/scripts/) register with the runtime via a window global. * The runtime owns all subscriptions internally (not on EventBus), * enabling per-SDK replay and zero-duplicate event delivery. */ /** * Registration payload passed by SDK scripts to * `window.__OPEN_STORE_PIXEL_RUNTIME__.register()`. * * Example (from meta-pixel-sdk.js): * ```js * window.__OPEN_STORE_PIXEL_RUNTIME__.register({ * name: 'meta-pixel', * register: function(analytics, config) { * fbq('init', config.pixelId); * analytics.subscribe('page_view', function() { fbq('track', 'PageView'); }); * } * }); * ``` */ interface PixelRegistration { /** SDK name — must match a key in pixelConfig (e.g., "meta-pixel", "ga4") */ name: string; /** Called by runtime. SDK uses analytics.subscribe() to listen to events. */ register: (analytics: SDKAnalyticsAPI, config: Record) => void; } /** * Frozen analytics object passed to each SDK's register() function. * SDKs can only subscribe to events — they cannot emit. */ interface SDKAnalyticsAPI { /** Subscribe to events by type. Use '*' for wildcard. */ subscribe: (eventType: string, callback: (event: SDKEvent) => void) => void; } /** * Event shape delivered to SDK callbacks. * Transformed from the internal OpenStoreEvent envelope by the runtime. * * Internal OpenStoreEvent uses: event.data, event.event_id (top-level) * SDK-facing SDKEvent uses: event.properties, event.metadata.event_id */ interface SDKEvent { event_type: string; timestamp: number; /** Event-specific payload (mapped from internal event.data) */ properties: Record; /** Metadata collected by middleware (event_id, session, cookies, etc.) */ metadata: SDKEventMetadata; } interface SDKEventMetadata { event_id: string; session_id: string; user_agent: string; page: { url: string; path: string; title: string; referrer: string; }; cookies?: Record; user_data?: Record; experiment?: Record; } /** * Pixel configuration map — SDK name → config object or null. * Null means the SDK is disabled for this merchant. * * Lives in the app layer (e.g., lib/pixelConfig.ts): * ```typescript * export const pixelConfig: PixelConfigMap = { * 'meta-pixel': process.env.NEXT_PUBLIC_META_PIXEL_ID * ? { pixelId: process.env.NEXT_PUBLIC_META_PIXEL_ID } * : null, * 'ga4': process.env.NEXT_PUBLIC_GA_ID * ? { measurementId: process.env.NEXT_PUBLIC_GA_ID } * : null, * }; * ``` */ type PixelConfigMap = Record | null>; /** * PixelRuntime constructor options. */ interface PixelRuntimeConfig { /** Max events to queue before SDKs register. Default: 200 */ maxQueueSize?: number; /** Registration window in ms before cleanup. Default: 10_000 (10s) */ registrationWindowMs?: number; /** Consent gate — return true to allow, false to block */ consentCheck?: (sdkName: string, eventType: string) => boolean; } /** * Circuit breaker states for per-SDK error handling. * - closed: normal operation * - open: SDK suspended after consecutive errors * - half-open: retry after cooldown period */ type CircuitBreakerState = "closed" | "open" | "half-open"; interface CircuitBreakerConfig { /** Errors before opening circuit (default: 5) */ failureThreshold: number; /** Cooldown in ms before half-open (default: 60_000) */ cooldownMs: number; } declare const DEFAULT_CIRCUIT_BREAKER_CONFIG: CircuitBreakerConfig; /** * Internal state tracked per registered SDK. */ interface RegisteredSDK { name: string; /** Event subscriptions: eventType → set of callbacks */ subscriptions: Map void>>; /** Per-SDK circuit breaker */ circuitBreaker: { state: CircuitBreakerState; errorCount: number; cooldownTimer: ReturnType | null; }; } /** * Shape of the window global exposed during the registration window. */ interface PixelRuntimeGlobal { register: (registration: PixelRegistration) => void; } declare global { interface Window { __OPEN_STORE_PIXEL_RUNTIME__?: PixelRuntimeGlobal; __OPEN_STORE_PIXEL_PENDING__?: PixelRegistration[]; } } export { type CircuitBreakerConfig as C, DEFAULT_CIRCUIT_BREAKER_CONFIG as D, type PixelRuntimeConfig as P, type RegisteredSDK as R, type SDKAnalyticsAPI as S, type PixelConfigMap as a, type CircuitBreakerState as b, type PixelRegistration as c, type PixelRuntimeGlobal as d, type SDKEvent as e, type SDKEventMetadata as f };