/** * @file Analytics Utilities * @description Telemetry and analytics wrapper with privacy controls, * consent management, and multiple provider support */ /** * Analytics event types */ export type AnalyticsEventType = 'page_view' | 'click' | 'form_submit' | 'error' | 'performance' | 'feature_usage' | 'search' | 'conversion' | 'custom'; /** * Analytics event */ export interface AnalyticsEvent { type: AnalyticsEventType; name: string; properties?: Record; timestamp?: number; userId?: string; sessionId?: string; } /** * User properties for identification */ export interface UserProperties { id?: string; email?: string; name?: string; role?: string; organization?: string; plan?: string; createdAt?: string; [key: string]: unknown; } /** * Analytics provider interface */ export interface AnalyticsProvider { name: string; initialize(config: Record): Promise; identify(userId: string, properties?: UserProperties): void; track(event: AnalyticsEvent): void; page(name: string, properties?: Record): void; reset(): void; setUserProperties(properties: UserProperties): void; } /** * Consent categories */ export interface ConsentCategories { necessary: boolean; analytics: boolean; marketing: boolean; preferences: boolean; } /** * Analytics configuration */ export interface AnalyticsConfig { /** Enable analytics */ enabled?: boolean; /** Enable debug mode */ debug?: boolean; /** Default consent settings */ defaultConsent?: Partial; /** PHI fields to exclude */ phiFields?: string[]; /** PII fields to hash */ piiFields?: string[]; /** Sample rate (0-1) */ sampleRate?: number; /** Session timeout in ms */ sessionTimeout?: number; /** Queue events when offline */ offlineQueue?: boolean; /** Max queue size */ maxQueueSize?: number; } /** * Analytics context */ export interface AnalyticsContext { page?: { path: string; title: string; referrer: string; }; device?: { type: string; os: string; browser: string; screen: string; }; location?: { timezone: string; language: string; }; campaign?: { source?: string; medium?: string; campaign?: string; term?: string; content?: string; }; } /** * Analytics manager with privacy controls and multi-provider support */ export declare class AnalyticsManager { private config; private providers; private consent; private userId; private sessionId; private sessionStart; private eventQueue; private context; constructor(config?: AnalyticsConfig); /** * Register analytics provider */ registerProvider(provider: AnalyticsProvider, config?: Record): Promise; /** * Remove provider */ removeProvider(name: string): void; /** * Set consent */ setConsent(consent: Partial): void; /** * Get consent */ getConsent(): ConsentCategories; /** * Identify user */ identify(userId: string, properties?: UserProperties): void; /** * Track event */ track(name: string, properties?: Record, type?: AnalyticsEventType): void; /** * Track page view */ page(name?: string, properties?: Record): void; /** * Track error */ trackError(error: Error, context?: Record): void; /** * Track performance metric */ trackPerformance(metric: string, value: number, context?: Record): void; /** * Track feature usage */ trackFeature(feature: string, action: string, context?: Record): void; /** * Track conversion */ trackConversion(name: string, value?: number, context?: Record): void; /** * Set user properties */ setUserProperties(properties: UserProperties): void; /** * Reset analytics (on logout) */ reset(): void; /** * Get analytics context */ getContext(): AnalyticsContext; /** * Get session info */ getSessionInfo(): { sessionId: string; duration: number; }; /** * Generate session ID */ private generateSessionId; /** * Setup context information */ private setupContext; /** * Get device type */ private getDeviceType; /** * Get OS */ private getOS; /** * Get browser */ private getBrowser; /** * Get UTM parameters */ private getUTMParams; /** * Setup session tracking */ private setupSessionTracking; /** * Check if tracking is allowed */ private canTrack; /** * Sanitize event properties */ private sanitizeProperties; /** * Simple hash function for PII */ private hashValue; /** * Send event to providers */ private sendEvent; /** * Queue event for later */ private queueEvent; /** * Flush event queue */ private flushQueue; } /** * Console analytics provider for development */ declare class ConsoleAnalyticsProvider implements AnalyticsProvider { name: string; initialize(): Promise; identify(userId: string, properties?: UserProperties): void; track(event: AnalyticsEvent): void; page(name: string, properties?: Record): void; reset(): void; setUserProperties(properties: UserProperties): void; } export default ConsoleAnalyticsProvider; /** * Global analytics manager instance */ export declare const analytics: AnalyticsManager; /** * Track event */ export declare function trackEvent(name: string, properties?: Record, type?: AnalyticsEventType): void; /** * Track page view */ export declare function trackPageView(name?: string, properties?: Record): void; /** * Identify user */ export declare function identifyUser(userId: string, properties?: UserProperties): void; /** * Track error */ export declare function trackError(error: Error, context?: Record): void; /** * Track performance metric */ export declare function trackPerformance(metric: string, value: number, context?: Record): void; /** * Track feature usage */ export declare function trackFeature(feature: string, action: string, context?: Record): void; /** * Set analytics consent */ export declare function setAnalyticsConsent(consent: Partial): void; /** * Reset analytics */ export declare function resetAnalytics(): void;