UNPKG

@deriv-com/analytics

Version:

Comprehensive analytics package for Deriv applications. Provides unified event tracking, A/B testing, and user analytics through RudderStack, PostHog and GrowthBook integrations with built-in caching and offline support.

76 lines (73 loc) 2.81 kB
import { RudderAnalytics } from '@rudderstack/analytics-js'; /** * RudderStack analytics wrapper with singleton pattern. * Handles user tracking, page views, and event analytics. */ declare class RudderStack { analytics: RudderAnalytics; has_identified: boolean; has_initialized: boolean; current_page: string; rudderstack_anonymous_cookie_key: string; private static _instance; private onLoadedCallback?; private debug; private log; constructor(RUDDERSTACK_KEY: string, onLoaded?: () => void, debug?: boolean); /** * Get or create the singleton instance of RudderStack * @param RUDDERSTACK_KEY - RudderStack write key * @param onLoaded - Optional callback when RudderStack is loaded * @param debug - Enable debug logging * @returns The RudderStack singleton instance */ static getRudderStackInstance: (RUDDERSTACK_KEY: string, onLoaded?: () => void, debug?: boolean) => RudderStack; /** * Get the anonymous ID from cookies * @returns The anonymous ID or undefined if not found */ getAnonymousId: () => string | undefined; /** * Set anonymous ID cookie if it doesn't exist * Creates a secure cookie with proper domain and security attributes */ setCookieIfNotExists: () => void; /** * Get the current user ID * @returns The user ID, null, or undefined if not identified */ getUserId: () => (string | null) | undefined; /** * Initialize RudderStack with the provided key * @param RUDDERSTACK_KEY - RudderStack write key */ init: (RUDDERSTACK_KEY: string) => void; /** * Identify a user with RudderStack * Only identifies if user hasn't been identified yet * @param user_id - The user ID to identify * @param payload - Optional user traits (e.g., language, custom properties) */ identifyEvent: (user_id: string, payload?: Record<string, any>) => void; /** * Track a page view event * @param current_page - The page name/path * @param platform - The platform name (default: 'Deriv App') * @param user_id - The user ID * @param properties - Additional page properties */ pageView: (current_page: string, platform: string | undefined, user_id: string, properties?: Record<string, unknown>) => void; /** * Reset the RudderStack instance * Clears user identification and resets tracking state */ reset: () => void; /** * Track a custom event with payload * Payload is pre-cleaned by analytics.ts using cleanObject before being passed here * @param event - The event name * @param payload - The event payload with core attributes */ track: (event: string, payload: Record<string, any>) => void; } export { RudderStack };