@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.
138 lines (135 loc) • 6.17 kB
text/typescript
import * as _growthbook_growthbook from '@growthbook/growthbook';
import { RudderStack } from './providers/rudderstack/index.mjs';
import { TGrowthbookOptions, GrowthbookConfigs, Growthbook } from './providers/growthbook/index.mjs';
import { T as TPosthogOptions, P as Posthog } from './posthog-DmVmqDsn.mjs';
import '@rudderstack/analytics-js';
import 'posthog-js';
declare global {
interface Window {
AnalyticsInstance: ReturnType<typeof createAnalyticsInstance>;
}
}
/**
* Configuration options for initializing the analytics instance
*/
type Options = {
/** GrowthBook client API key for A/B testing and feature flags */
growthbookKey?: string;
/** GrowthBook decryption key for encrypted feature payloads */
growthbookDecryptionKey?: string;
/** RudderStack write key for event tracking */
rudderstackKey?: string;
/** Additional configuration options for GrowthBook */
growthbookOptions?: TGrowthbookOptions;
/** PostHog configuration options including API keys and settings */
posthogOptions?: TPosthogOptions;
/** Enable debug logging — logs all analytics calls prefixed with [ANALYTIC] */
debug?: boolean;
};
/**
* Creates a unified analytics instance that integrates RudderStack and GrowthBook.
*
* This function provides a centralized interface for:
* - Event tracking across multiple analytics platforms
* - A/B testing and feature flag management via GrowthBook
* - Offline event caching with automatic replay
*
* @param {Options} _options - Optional initialization configuration
* @returns {Object} Analytics instance with methods for tracking, identification, and feature management
*
* @example
* ```typescript
* const analytics = createAnalyticsInstance();
*
* // Initialize with providers
* await analytics.initialise({
* rudderstackKey: 'YOUR_RS_KEY',
* growthbookKey: 'YOUR_GB_KEY',
* growthbookDecryptionKey: 'YOUR_GB_DECRYPT_KEY'
* });
*
* // Set user attributes
* analytics.setAttributes({
* user_id: 'user123',
* country: 'US',
* user_language: 'en'
* });
*
* // Track events
* analytics.trackEvent('button_clicked', { button_name: 'signup' });
*
* // Track page views
* analytics.pageView('/dashboard', 'Deriv App');
* ```
*/
declare function createAnalyticsInstance(_options?: Options): {
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey, growthbookOptions, posthogOptions, debug, }: Options) => Promise<void>;
setAttributes: (attributes: Record<string, any>) => void;
identifyEvent: (user_id?: string, traits?: Record<string, any>) => void;
backfillPersonProperties: ({ user_id, email, language, country_of_residence, }: {
user_id: string;
email?: string;
language?: string;
country_of_residence?: string;
}) => void;
getFeatureState: (id: string) => string | undefined;
getFeatureValue: <K extends keyof GrowthbookConfigs, V extends GrowthbookConfigs[K]>(id: K, defaultValue: V) => _growthbook_growthbook.WidenPrimitives<V> | undefined;
getGrowthbookStatus: () => Promise<{
isLoaded: boolean;
status: void | _growthbook_growthbook.InitResponse;
} | undefined>;
isFeatureOn: (key: string) => boolean | undefined;
setUrl: (href: string) => void | undefined;
getId: () => string;
getAnonymousId: () => string;
trackEvent: (event: string, analytics_data: Record<string, any>) => void;
getInstances: () => {
ab: Growthbook | undefined;
tracking: RudderStack;
posthog: Posthog | undefined;
};
pageView: (current_page: string, platform?: string, properties?: Record<string, unknown>) => void;
reset: () => void;
isPosthogFeatureEnabled: (key: string) => boolean | undefined;
getPosthogFeatureFlag: (key: string) => string | boolean | undefined;
getPosthogFeatureFlagPayload: (key: string) => string | number | boolean | null | Record<string, unknown> | unknown[] | undefined;
getPosthogAllFlags: () => Record<string, string | boolean>;
onPosthogFeatureFlags: (callback: (flags: string[], variants: Record<string, string | boolean>) => void) => (() => void);
reloadPosthogFeatureFlags: () => void;
};
declare const Analytics: {
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey, growthbookOptions, posthogOptions, debug, }: Options) => Promise<void>;
setAttributes: (attributes: Record<string, any>) => void;
identifyEvent: (user_id?: string, traits?: Record<string, any>) => void;
backfillPersonProperties: ({ user_id, email, language, country_of_residence, }: {
user_id: string;
email?: string;
language?: string;
country_of_residence?: string;
}) => void;
getFeatureState: (id: string) => string | undefined;
getFeatureValue: <K extends keyof GrowthbookConfigs, V extends GrowthbookConfigs[K]>(id: K, defaultValue: V) => _growthbook_growthbook.WidenPrimitives<V> | undefined;
getGrowthbookStatus: () => Promise<{
isLoaded: boolean;
status: void | _growthbook_growthbook.InitResponse;
} | undefined>;
isFeatureOn: (key: string) => boolean | undefined;
setUrl: (href: string) => void | undefined;
getId: () => string;
getAnonymousId: () => string;
trackEvent: (event: string, analytics_data: Record<string, any>) => void;
getInstances: () => {
ab: Growthbook | undefined;
tracking: RudderStack;
posthog: Posthog | undefined;
};
pageView: (current_page: string, platform?: string, properties?: Record<string, unknown>) => void;
reset: () => void;
isPosthogFeatureEnabled: (key: string) => boolean | undefined;
getPosthogFeatureFlag: (key: string) => string | boolean | undefined;
getPosthogFeatureFlagPayload: (key: string) => string | number | boolean | null | Record<string, unknown> | unknown[] | undefined;
getPosthogAllFlags: () => Record<string, string | boolean>;
onPosthogFeatureFlags: (callback: (flags: string[], variants: Record<string, string | boolean>) => void) => (() => void);
reloadPosthogFeatureFlags: () => void;
};
export { Analytics };