export { CookieSettings, ParamBuilder } from 'capi-param-builder-nodejs'; /** * Type definitions for Meta (Facebook) CAPI integration. */ /** Raw CAPI event as received from the client SDK via batch POST */ interface RawCAPIEvent { event_name: string; event_id: string; event_time: number; event_source_url?: string; action_source: string; user_data: Record; custom_data: Record; } /** Facebook CAPI-compliant event ready for Graph API dispatch */ interface MetaCAPIEvent { event_name: string; event_id: string; event_time: number; event_source_url?: string; action_source: string; user_data: MetaHashedUserData; custom_data: Record; } /** User data with Facebook abbreviated field names and hashed PII */ interface MetaHashedUserData { em?: string; ph?: string; fn?: string; ln?: string; ct?: string; st?: string; zp?: string; country?: string; db?: string; ge?: string; external_id?: string; fbp?: string; fbc?: string; client_ip_address?: string; client_user_agent?: string; } /** Server-side context to enrich events with */ interface ServerContext { clientIp?: string; userAgent?: string; } /** Options for the MetaParamsBuilder */ interface MetaParamsBuilderOptions { /** Whether to hash PII fields (default: true) */ hashPII?: boolean; } /** Batch payload shape sent from SDK to CAPI route */ interface CAPIBatchPayload { events: RawCAPIEvent[]; } /** Per-pixel dispatch configuration */ interface FacebookPixelConfig { pixelId: string; accessToken: string; baseUrl: string; } /** Result of dispatching to a single pixel */ interface PixelDispatchResult { pixelId: string; success: boolean; error?: string; events_received?: number; messages?: unknown; durationMs?: number; } /** Validation result from validateRawCAPIEvent */ interface ValidationResult { valid: boolean; errors: string[]; warnings: string[]; } /** * MetaParamsBuilder — Fluent builder for Facebook CAPI-compliant event payloads. * * Transforms raw CAPI events (as received from the client SDK) into * Facebook Graph API-ready payloads with hashed PII and server context. * * @example * ```typescript * // Single event * const metaEvent = MetaParamsBuilder.fromRawEvent(rawEvent) * .withServerContext({ clientIp, userAgent }) * .build(); * * // Batch (primary API for CAPI route) * const enriched = MetaParamsBuilder.buildBatch(rawEvents, { clientIp, userAgent }); * ``` */ declare class MetaParamsBuilder { private event; private serverContext; private options; private constructor(); /** * Create a builder from a raw CAPI event. */ static fromRawEvent(event: RawCAPIEvent, options?: MetaParamsBuilderOptions): MetaParamsBuilder; /** * Add server-side context (IP address, User-Agent). * These fields boost Event Match Quality significantly. */ withServerContext(context: ServerContext): this; /** * Build the final Facebook CAPI-compliant event. */ build(): MetaCAPIEvent; /** * Build a batch of CAPI events from raw SDK events. * This is the primary API for the CAPI route handler. * * @example * ```typescript * const enrichedEvents = MetaParamsBuilder.buildBatch(body.events, { * clientIp: getClientIP(request), * userAgent: request.headers.get('user-agent') ?? undefined, * }); * ``` */ static buildBatch(events: RawCAPIEvent[], serverContext: ServerContext, options?: MetaParamsBuilderOptions): MetaCAPIEvent[]; } /** * PII normalization and SHA-256 hashing for Facebook CAPI. * * Uses Facebook's official `capi-param-builder-nodejs` for: * - Per-field normalization (email, phone, DOB, gender, zip code, etc.) * - SHA-256 hashing * - Already-hashed detection (skips re-hashing) * * Our code handles: * - Field name mapping (email → em, phone → ph, etc.) * - Passthrough of non-PII fields (fbp, fbc, IP, UA) */ /** * Normalize and hash a single PII value using Facebook's official library. * * Facebook's ParamBuilder handles: * - Email: lowercase, trim, domain normalization * - Phone: strip non-digits, country code handling * - DOB: format validation (YYYYMMDD) * - Gender: normalize to single char * - Zip: format validation * - Names, City, State, Country: lowercase, trim, special char handling * - Already-hashed detection: skips re-hashing if value is already SHA-256 * * Returns undefined if the input is empty/undefined or normalization fails. */ declare function hashPIIField(value: string | undefined, field: string): string | undefined; /** * Transform raw user_data into Facebook CAPI format. * * - PII fields: normalized + SHA-256 hashed via Facebook's ParamBuilder, * then mapped to abbreviated names (email → em, phone → ph, etc.) * - Meta tracking fields (fbp, fbc): passed through as-is * - Server context fields: passed through as-is */ declare function hashAndMapUserData(raw: Record): MetaHashedUserData; /** * Meta (Facebook) CAPI constants. * Single source of truth for field mappings, normalization rules, and event names. */ /** Maps raw user_data field names to Facebook CAPI abbreviated field names */ declare const PII_FIELD_MAP: Record; /** Fields that require SHA-256 hashing before sending to Facebook */ declare const HASHABLE_PII_FIELDS: Set; /** Meta tracking identifiers — NOT hashed, passed through as-is */ declare const META_TRACKING_FIELDS: Set; /** Server context fields — NOT hashed, added by the CAPI route */ declare const SERVER_CONTEXT_FIELDS: Set; /** Fields that require phone-specific normalization (strip non-digits) */ declare const PHONE_NORMALIZATION_FIELDS: Set; /** Facebook Graph API version */ declare const GRAPH_API_VERSION = "v21.0"; /** High-value events that should flush the CAPI queue immediately */ declare const HIGH_VALUE_EVENTS: Set; /** All 13 supported Facebook standard event names */ declare const SUPPORTED_EVENT_NAMES: readonly ["PageView", "ViewContent", "AddToCart", "InitiateCheckout", "AddShippingInfo", "AddPaymentInfo", "Purchase", "Search", "AddToWishlist", "Lead", "CompleteRegistration", "Contact", "Subscribe"]; type FacebookEventName = (typeof SUPPORTED_EVENT_NAMES)[number]; /** * Validation helpers for Meta CAPI events. * * - validateRawCAPIEvent: checks required fields and EMQ signals * - estimateEMQScore: approximates Facebook Event Match Quality score */ /** * Validate a raw CAPI event before processing. * Returns errors for blocking issues, warnings for EMQ improvements. */ declare function validateRawCAPIEvent(event: RawCAPIEvent): ValidationResult; /** * Estimate Event Match Quality score (0-10) based on user_data completeness. * * Approximate weights based on Facebook documentation: * - em (email): +2.5 * - ph (phone): +1.5 * - fn + ln (name pair): +1.0 * - ct + st + zp (location): +1.0 * - country: +0.5 * - fbp: +1.5 * - fbc: +1.0 * - client_ip_address: +0.5 * - client_user_agent: +0.5 * * Accepts both raw field names (email) and Facebook abbreviated names (em). */ declare function estimateEMQScore(userData: Record): number; export { type CAPIBatchPayload, type FacebookEventName, type FacebookPixelConfig, GRAPH_API_VERSION, HASHABLE_PII_FIELDS, HIGH_VALUE_EVENTS, META_TRACKING_FIELDS, type MetaCAPIEvent, type MetaHashedUserData, MetaParamsBuilder, type MetaParamsBuilderOptions, PHONE_NORMALIZATION_FIELDS, PII_FIELD_MAP, type PixelDispatchResult, type RawCAPIEvent, SERVER_CONTEXT_FIELDS, SUPPORTED_EVENT_NAMES, type ServerContext, type ValidationResult, estimateEMQScore, hashAndMapUserData, hashPIIField, validateRawCAPIEvent };