import { VariantId, UserId, EvaluationReason, JsonValue } from '../advanced/types'; /** * Evaluation event for analytics. */ export interface EvaluationEvent { /** Flag key */ readonly flagKey: string; /** Variant ID */ readonly variantId: VariantId; /** User ID */ readonly userId?: UserId; /** Session ID */ readonly sessionId?: string; /** Evaluation reason */ readonly reason?: EvaluationReason; /** Evaluation time in ms */ readonly durationMs?: number; /** Timestamp */ readonly timestamp: Date; /** Whether value was from cache */ readonly cached?: boolean; /** Additional metadata */ readonly metadata?: Record; } /** * Aggregated metrics for a flag. */ export interface FlagMetrics { /** Flag key */ readonly flagKey: string; /** Total evaluations */ readonly evaluationCount: number; /** Evaluations per variant */ readonly variantCounts: Record; /** Unique users */ readonly uniqueUsers: number; /** Unique sessions */ readonly uniqueSessions: number; /** Average evaluation time */ readonly avgDurationMs: number; /** Cache hit rate */ readonly cacheHitRate: number; /** Errors count */ readonly errorCount: number; /** First evaluation */ readonly firstSeen: Date; /** Last evaluation */ readonly lastSeen: Date; } /** * Analytics configuration. */ export interface FlagAnalyticsConfig { /** Enable analytics collection */ readonly enabled?: boolean; /** Batch size before flushing */ readonly batchSize?: number; /** Flush interval in ms */ readonly flushInterval?: number; /** Analytics endpoint URL */ readonly endpoint?: string; /** API key for endpoint */ readonly apiKey?: string; /** Custom flush handler */ readonly onFlush?: (events: EvaluationEvent[]) => Promise; /** Error handler */ readonly onError?: (error: Error) => void; /** Enable debug logging */ readonly debug?: boolean; /** Sample rate (0-1) */ readonly sampleRate?: number; /** Max events to store */ readonly maxEvents?: number; } /** * Analytics report. */ export interface AnalyticsReport { /** Report generation time */ readonly generatedAt: Date; /** Time period start */ readonly periodStart: Date; /** Time period end */ readonly periodEnd: Date; /** Total evaluations */ readonly totalEvaluations: number; /** Unique flags evaluated */ readonly uniqueFlags: number; /** Unique users */ readonly uniqueUsers: number; /** Metrics per flag */ readonly flagMetrics: FlagMetrics[]; /** Top flags by evaluation count */ readonly topFlags: Array<{ flagKey: string; count: number; }>; /** Error rate */ readonly errorRate: number; } /** * Analytics collector for feature flags. */ export declare class FlagAnalytics { private events; private flushTimer; private config; private flagMetrics; private listeners; private periodStart; constructor(config?: FlagAnalyticsConfig); /** * Track a flag evaluation event. */ trackEvaluation(event: Omit): void; /** * Track an error during evaluation. */ trackError(flagKey: string, error: Error, metadata?: Record): void; /** * Flush pending events. */ flush(): Promise; /** * Get metrics for a specific flag. */ getMetrics(flagKey: string): FlagMetrics | null; /** * Get all flag metrics. */ getAllMetrics(): FlagMetrics[]; /** * Generate an analytics report. */ getReport(): AnalyticsReport; /** * Subscribe to flush events. */ subscribe(listener: (events: EvaluationEvent[]) => void): () => void; /** * Reset all metrics and events. */ reset(): void; /** * Shutdown the analytics collector. */ shutdown(): Promise; /** * Enable or disable analytics. */ setEnabled(enabled: boolean): void; private updateMetrics; private getOrCreateMetrics; /** * Default flush handler that sends events to the configured endpoint. * * Note: This method intentionally uses raw fetch() rather than apiClient because: * 1. Analytics should be independent of the main API client to avoid circular dependencies * 2. Analytics endpoints may be on a different domain/service * 3. This should work even if the main API client fails * * @see {@link @/lib/api/api-client} for the main API client */ private defaultFlush; private startFlushTimer; private stopFlushTimer; private notifyListeners; private log; } /** * Get the singleton analytics instance. */ export declare function getFlagAnalytics(): FlagAnalytics; /** * Initialize the singleton with configuration. */ export declare function initFlagAnalytics(config: FlagAnalyticsConfig): FlagAnalytics; /** * Reset the singleton instance. */ export declare function resetFlagAnalytics(): void;