// --------------------------------------------------------------------------- // Core event types // --------------------------------------------------------------------------- export interface AnalyticsEvent { name: string; properties?: Record; timestamp: number; sessionId?: string; userId?: string; anonymousId: string; context?: EventContext; } export interface ScreenEvent { name: string; properties?: Record; timestamp: number; sessionId?: string; userId?: string; anonymousId: string; previousScreen?: string; } export interface IdentifyPayload { userId: string; traits?: UserTraits; timestamp: number; anonymousId: string; } export interface GroupPayload { groupId: string; traits?: Record; timestamp: number; userId?: string; anonymousId: string; } export interface AliasPayload { newId: string; previousId: string; timestamp: number; } // --------------------------------------------------------------------------- // User traits // --------------------------------------------------------------------------- export interface UserTraits { name?: string; email?: string; phone?: string; avatar?: string; createdAt?: string | Date; [key: string]: any; } // --------------------------------------------------------------------------- // Event context (enriched metadata) // --------------------------------------------------------------------------- export interface EventContext { app?: { name?: string; version?: string; build?: string; }; device?: { type?: string; model?: string; manufacturer?: string; os?: string; osVersion?: string; }; locale?: string; timezone?: string; screen?: { width?: number; height?: number; density?: number; }; network?: { wifi?: boolean; cellular?: boolean; carrier?: string; }; campaign?: { name?: string; source?: string; medium?: string; term?: string; content?: string; }; [key: string]: any; } // --------------------------------------------------------------------------- // Session // --------------------------------------------------------------------------- export interface SessionInfo { sessionId: string; startTime: number; lastActiveTime: number; screenCount: number; eventCount: number; isActive: boolean; } // --------------------------------------------------------------------------- // Provider adapter interface — every analytics provider implements this // --------------------------------------------------------------------------- export interface AnalyticsAdapter { /** Unique identifier for this adapter */ name: string; /** Called once when AnalyticsManager initializes */ initialize(): Promise; /** Track a custom event */ track(event: AnalyticsEvent): Promise; /** Track a screen view */ screen(event: ScreenEvent): Promise; /** Identify a user */ identify(payload: IdentifyPayload): Promise; /** Associate user with a group/org (optional) */ group?(payload: GroupPayload): Promise; /** Alias two user identities (optional) */ alias?(payload: AliasPayload): Promise; /** Reset user identity (e.g. on logout) */ reset(): Promise; /** Force flush any internal queues (optional) */ flush?(): Promise; } // --------------------------------------------------------------------------- // Middleware // --------------------------------------------------------------------------- export type EventMiddlewareFn = ( event: AnalyticsEvent, next: (event: AnalyticsEvent) => void ) => void; export type ScreenMiddlewareFn = ( event: ScreenEvent, next: (event: ScreenEvent) => void ) => void; // --------------------------------------------------------------------------- // Privacy / GDPR config // --------------------------------------------------------------------------- export interface PrivacyConfig { /** Fields to mask in all events (e.g. 'email', 'phone') */ maskFields?: string[]; /** Replacement value for masked fields */ maskValue?: string; /** Whether to start in opted-out state */ defaultOptOut?: boolean; } // --------------------------------------------------------------------------- // Event queue config // --------------------------------------------------------------------------- export interface QueueConfig { /** Max events to hold before forcing a flush (default: 20) */ maxBatchSize?: number; /** How often to flush in ms (default: 10000) */ flushInterval?: number; /** Max retry attempts for failed flushes (default: 3) */ maxRetries?: number; /** Persist queue to storage for offline support (default: true) */ persistQueue?: boolean; } // --------------------------------------------------------------------------- // Session config // --------------------------------------------------------------------------- export interface SessionConfig { /** Inactivity timeout in ms before a new session starts (default: 30 min) */ sessionTimeout?: number; /** Auto-track session_start and session_end events (default: true) */ autoTrackSessionEvents?: boolean; } // --------------------------------------------------------------------------- // Main config // --------------------------------------------------------------------------- export interface AnalyticsConfig { /** Provider adapters to use */ adapters: AnalyticsAdapter[]; /** Privacy / GDPR settings */ privacy?: PrivacyConfig; /** Event queue settings */ queue?: QueueConfig; /** Session tracking settings */ session?: SessionConfig; /** Global properties added to every event */ globalProperties?: Record; /** Called when an event fails to send after all retries */ onError?: (error: Error, event: AnalyticsEvent | ScreenEvent) => void; /** Enable debug logging (default: false) */ debug?: boolean; }