UNPKG

thrivestack-analytics

Version:

ThriveStack Analytics - Privacy-first web analytics for Node.js and browsers

256 lines (255 loc) 6.42 kB
interface ThriveStackOptions { apiKey: string; apiEndpoint?: string; trackClicks?: boolean; trackForms?: boolean; respectDoNotTrack?: boolean; enableConsent?: boolean; batchSize?: number; batchInterval?: number; geoIpServiceUrl?: string; source?: string; sessionTimeout?: number; debounceDelay?: number; defaultConsent?: boolean; } interface LocationInfo { city?: string | null; region?: string | null; country?: string | null; postal?: string | null; loc?: string | null; timezone?: string | null; } interface ConsentCategories { functional: boolean; analytics: boolean; marketing: boolean; } interface InteractionHistory { type: string; details: any; timestamp: string; sequence: number; } interface EventData { event_name: string; properties: Record<string, any>; user_id?: string; context: { group_id?: string; device_id?: string | null; session_id?: string; source?: string; }; timestamp: string; } interface IdentifyData { user_id: string; traits: Record<string, any>; timestamp: string; } interface GroupData { group_id: string; user_id?: string; traits: Record<string, any>; timestamp: string; } export declare class ThriveStack { private apiKey; private apiEndpoint; private respectDoNotTrack; private trackClicks; private trackForms; private enableConsent; private source; private geoIpServiceUrl; private ipAddress; private locationInfo; private eventQueue; private queueTimer; private batchSize; private batchInterval; private interactionHistory; private maxHistoryLength; private consentCategories; private userId; private groupId; private deviceId; private deviceIdReady; private sessionTimeout; private debounceDelay; private sessionUpdateTimer; private debugMode; constructor(options: ThriveStackOptions | string); /** * Initialize device ID with proper fallback logic */ private initializeDeviceId; /** * Generate a random device ID as fallback */ private generateRandomDeviceId; /** * Fetch IP address and location information */ private fetchIpAndLocationInfo; /** * Initialize ThriveStack and start tracking */ init(userId?: string, source?: string): Promise<void>; /** * Check if tracking is allowed based on DNT and consent settings */ private shouldTrack; /** * Check if specific tracking category is allowed */ private isTrackingAllowed; /** * Update consent settings for a tracking category */ setConsent(category: keyof ConsentCategories, hasConsent: boolean): void; /** * Set user ID for tracking */ setUserId(userId: string): void; /** * Set group ID for tracking */ setGroupId(groupId: string): void; /** * Get userId from cookie (browser only) */ private getUserIdFromCookie; /** * Get groupId from cookie (browser only) */ private getGroupIdFromCookie; /** * Set source for tracking */ setSource(source: string): void; /** * Queue an event for batched sending */ private queueEvent; /** * Process queue only if device ID is ready */ private processQueueIfReady; /** * Process and send queued events */ private processQueue; /** * Track events by sending to ThriveStack API */ track(events: EventData[]): Promise<any>; /** * Send user identification data */ identify(data: IdentifyData | IdentifyData[]): Promise<any>; /** * Send group data */ group(data: GroupData | GroupData[]): Promise<any>; /** * Get device ID */ getDeviceId(): string | null; /** * Get session ID */ private getSessionId; /** * Update session activity with debouncing */ private updateSessionActivity; /** * Immediate session activity update */ private updateSessionActivityImmediate; /** * Capture page visit event (works in both Node.js and browser) */ capturePageVisit(pageInfo?: { title?: string; url?: string; path?: string; referrer?: string; }): Promise<void>; /** * Capture custom event */ captureEvent(eventName: string, properties?: Record<string, any>): Promise<void>; /** * Add event to interaction history */ private addToInteractionHistory; /** * Automatically detect and clean PII from event data */ private cleanPIIFromEventData; /** * Set user information and optionally make identify API call */ setUser(userId: string, emailId?: string, properties?: Record<string, any>): Promise<any | null>; /** * Set group information and optionally make group API call */ setGroup(groupId: string, groupDomain?: string, groupName?: string, properties?: Record<string, any>): Promise<any | null>; /** * Enable debug mode for troubleshooting */ enableDebugMode(): void; /** * Get interaction history */ getInteractionHistory(): InteractionHistory[]; /** * Get current location info */ getLocationInfo(): LocationInfo | null; /** * Get current IP address */ getIpAddress(): string | null; /** * Get UTM parameters from URL (browser only) */ private getUtmParameters; /** * Auto-capture page visits (browser only) */ autoCapturePageVisit(): void; /** * Auto-capture click events (browser only) */ autoCaptureClickEvents(): void; /** * Capture click event (browser only) */ captureClickEvent(event: Event): void; /** * Get element hierarchy for DOM traversal (browser only) */ private getElementHierarchy; /** * Get CSS selector for element (browser only) */ private getElementSelector; /** * Auto-capture form events (browser only) */ autoCaptureFormEvents(): void; /** * Capture form events (browser only) */ captureFormEvent(event: any, type: string): void; /** * Setup session tracking (browser only) */ setupSessionTracking(): void; } export {};