/** * @youranalytics/web-sdk Analytics Class * Core SDK class that orchestrates storage, network, and event tracking */ import type { AnalyticsConfig, ResolvedConfig, VisitorId, SessionId } from './types'; /** * Main Analytics SDK class. * Manages event tracking, queue, and communication with the analytics server. * * @example * ```typescript * const analytics = new Analytics({ apiKey: 'pk_live_xxx' }); * * // Track custom events * analytics.track('signup', { plan: 'pro' }); * * // Track page views (usually automatic) * analytics.trackPageView(); * * // Clean up when done * analytics.destroy(); * ``` */ export declare class Analytics { private config; private queue; private visitorId; private sessionId; private flushTimer; private isInitialized; private lastPageUrl; private lastRawUrl; private lastSanitizedUrl; private boundHandlePageUnload; private boundHandleOnline; private boundHandleOffline; private boundHandlePopstate; private debouncedTrackPageView; private originalPushState; private originalReplaceState; private adapter; private adapterCleanup; private useAdaptiveNetwork; /** * Create a new Analytics instance. * * @param config - Analytics configuration * @throws Error if API key is missing */ constructor(config: AnalyticsConfig); /** * Set up auto-tracking, timers, and event listeners. */ private init; /** * Initialize the network adapter for adaptive behavior. */ private initNetworkAdapter; /** * Apply adaptive settings based on network conditions. */ private applyAdaptiveSettings; /** * Track a custom event. * * @param eventName - Name of the event (will be sanitized) * @param properties - Optional event properties * * @example * ```typescript * analytics.track('purchase', { * product_id: '123', * price: 99.99, * currency: 'USD', * }); * ``` */ track(eventName: string, properties?: Record): void; /** * Read the current page URL and apply sanitizeUrl. * Caches the last result so sanitizeUrl runs once per distinct URL. * Falls back to the raw URL if sanitizeUrl throws or returns a non-string. */ private capturePageUrl; /** * Track a page view event. * Convenience method that calls track('pageview', {}). */ trackPageView(): void; /** * Internal pageview tracking (not debounced). */ private trackPageViewInternal; /** * Send all queued events to the server. * Uses sendBeacon first, falls back to fetch. * * @returns Promise that resolves when flush is complete */ flush(): Promise; /** * Build RawEvents from QueuedEvents by adding context metadata. */ private buildRawEvents; /** * Start periodic flush timer. */ private startFlushTimer; /** * Stop flush timer. */ private stopFlushTimer; /** * Set up automatic page view tracking for SPAs. * Wraps history methods and listens to popstate. */ private setupPageViewTracking; /** * Wrap history method to track navigation. */ private wrapHistoryMethod; /** * Handle popstate event (back/forward navigation). */ private handlePopstate; /** * Restore original history methods. */ private restoreHistoryMethods; /** * Handle page unload - flush remaining events. * Must use sendBeacon for reliability. */ private handlePageUnload; /** * Handle coming back online. * Send any queued offline events. */ private handleOnline; /** * Handle going offline. * Flush current queue to localStorage. */ private handleOffline; /** * Send any queued offline events. */ private sendOfflineEvents; /** * Update configuration at runtime. * * @param updates - Partial configuration to merge */ setConfig(updates: Partial): void; /** * Get current visitor ID. * Useful for debugging or external tools. * * @returns Current visitor ID */ getVisitorId(): VisitorId; /** * Get current session ID. * * @returns Current session ID */ getSessionId(): SessionId; /** * Get current configuration. * * @returns Current resolved configuration */ getConfig(): ResolvedConfig; /** * Get current queue length. * * @returns Number of events in queue */ getQueueLength(): number; /** * Check if SDK is initialized. * * @returns true if initialized */ isActive(): boolean; /** * Clean up SDK instance. * Flushes remaining events and removes listeners. */ destroy(): void; private log; private warn; private error; } /** * Create a new Analytics instance. * Convenience function that wraps the class constructor. * * @param config - Analytics configuration * @returns New Analytics instance */ export declare function createAnalytics(config: AnalyticsConfig): Analytics; //# sourceMappingURL=analytics.d.ts.map