@datanova/browser
Version:
Lightweight browser SDK for event tracking and A/B testing
116 lines (106 loc) • 3.81 kB
text/typescript
interface Event {
eventName: string;
eventType: EventType;
properties?: Record<string, unknown>;
timestamp: string;
context: Context;
}
declare const EventType: {
readonly CLICK: "click";
readonly PAGE_VIEW: "pageView";
readonly IMPRESSION: "impression";
readonly SUBMIT: "submit";
readonly CHANGE: "change";
};
type EventType = (typeof EventType)[keyof typeof EventType];
interface Context {
userId?: string;
userProperties?: Record<string, unknown>;
sessionId: string;
browser: BrowserContext;
library: {
name: string;
version: string;
};
}
interface BrowserContext {
url: string;
title: string;
referrer: string;
path: string;
search: string;
userAgent: string;
}
interface EventsService {
send(event: Event): Promise<void>;
}
type Variant = 'control' | 'variant';
interface ExperimentsService {
getVariant(experimentId: number, identifier: string): Promise<Variant>;
}
interface SDKConfig {
eventsService?: EventsService;
experimentsService?: ExperimentsService;
/** @internal - Context overrides */
context?: Partial<Context>;
}
declare class Datanova {
private initialized;
private tracker?;
private readonly contextManager;
private experimentManager?;
init(config: SDKConfig): void;
identify: (userId: string, properties?: Record<string, unknown>) => void;
reset: () => void;
track: (eventName: string, eventType: EventType, properties?: Record<string, unknown>) => void;
trackClick: (eventName: string, properties?: Record<string, unknown>) => void;
trackPageView: (eventName: string, properties?: Record<string, unknown>) => void;
trackImpression: (eventName: string, properties?: Record<string, unknown>) => void;
trackSubmit: (eventName: string, properties?: Record<string, unknown>) => void;
trackChange: (eventName: string, properties?: Record<string, unknown>) => void;
getVariant: (experimentId: number) => Promise<Variant>;
private assertInitialized;
private assertTracker;
private assertExperimentManager;
}
declare class DatanovaEventsService implements EventsService {
private sdkKey;
private endpoint;
constructor(sdkKey: string, endpoint?: string);
send(event: Event): Promise<void>;
}
declare class ConsoleEventsService implements EventsService {
send(event: Event): Promise<void>;
}
declare class NoopEventsService implements EventsService {
send(_event: Event): Promise<void>;
}
declare class DatanovaExperimentsService implements ExperimentsService {
private sdkKey;
constructor(sdkKey: string);
getVariant(experimentId: number, identifier: string): Promise<Variant>;
private determineVariant;
private saveAssignment;
private isExpired;
private fetchConfig;
private hash;
private storageKey;
}
/**
* Random experiments service that assigns variants based on a hash
* Useful for client-side experimentation without server calls
*/
declare class RandomExperimentsService implements ExperimentsService {
private cache;
private splitRatio;
constructor(splitRatio?: number);
getVariant(experimentId: number, identifier: string): Promise<Variant>;
private hashCode;
}
declare function generateAnonymousId(): string;
declare function createDatanova(sdkKey: string): Datanova;
declare function createDatanova(config: {
eventsService?: EventsService;
experimentsService?: ExperimentsService;
}): Datanova;
export { ConsoleEventsService, type Context, Datanova, DatanovaEventsService, DatanovaExperimentsService, type Event, EventType, type EventsService, type ExperimentsService, NoopEventsService, RandomExperimentsService, type SDKConfig, type Variant, createDatanova, generateAnonymousId };