/** * Types for the OXVO Insights browser API: `insights.init({ token, host })`, `track`, * `flags`, `consent` and `verify`. */ import type { JsonType, Properties } from './common'; import type { OxvoInsightsConfig } from './oxvoinsights-config'; type SnakeToCamel = S extends `${infer Head}_${infer Tail}` ? `${Head}${Capitalize>}` : S; /** Configuration keys that `InsightsInitOptions` renames or sets from its own fields. */ type ReplacedConfigKeys = 'token' | 'api_host' | 'opt_out_capturing_by_default' | 'cookieless_mode'; /** * Every configuration option, with camelCase names: `capturePageview`, `sessionRecording`, * `personProfiles` and so on. */ export type InsightsOptions = { [K in keyof OxvoInsightsConfig as K extends ReplacedConfigKeys | `_${string}` ? never : K extends string ? SnakeToCamel : never]?: OxvoInsightsConfig[K]; }; /** Options for `insights.init()`. */ export interface InsightsInitOptions extends InsightsOptions { /** Your project token. It identifies your project and is safe to ship in browser code. */ token: string; /** The OXVO Insights API host that events are sent to, such as `https://insights.example.com`. */ host: string; /** Capture nothing until `insights.consent.grant()` is called. */ requireConsent?: boolean; /** Use no cookies or browser storage: `'always'`, or `'on_reject'` after a visitor declines. */ cookieless?: 'always' | 'on_reject'; } /** * A map of event names to their properties, used to type `track()`: * `OxvoInsights<{ plan_selected: { plan: 'free' | 'pro' } }>`. */ export type InsightsEvents = Record; /** The properties argument of `track()`: optional when the event's properties can be `undefined`. */ export type InsightsTrackArgs = undefined extends TEvents[E] ? [properties?: TEvents[E]] : [properties: TEvents[E]]; /** * Where consent stands: * - `'granted'`: the visitor agreed. * - `'denied'`: the visitor declined, or the browser sends Do Not Track and `respectDnt` is on. * - `'required'`: no choice yet, and nothing is captured until `consent.grant()`. * - `'not-required'`: no choice yet, and capture runs because consent wasn't required. */ export type InsightsConsentStatus = 'granted' | 'denied' | 'required' | 'not-required'; /** Consent controls, available as `insights.consent`. */ export interface InsightsConsent { /** Record that the visitor agreed, and start capturing. */ grant(): void; /** Record that the visitor declined, and stop capturing. */ revoke(): void; /** Where consent stands. */ status(): InsightsConsentStatus; } /** The flags that are on, passed to `flags.onChange()` listeners. */ export interface InsightsFlagsChange { /** Keys of the flags that are on. */ enabled: string[]; /** The value of each flag that's on: `true`, or the variant for a multivariate flag. */ variants: Record; } /** Feature flags for the current visitor, available as `insights.flags`. */ export interface InsightsFlags { /** Whether a flag is on. `undefined` until flags load, or when the flag doesn't exist. */ isEnabled(key: string): boolean | undefined; /** A multivariate flag's variant, or `true`/`false` for a boolean flag. `undefined` until flags load. */ getVariant(key: string): string | boolean | undefined; /** The JSON payload attached to the flag's current value, if any. */ getPayload(key: string): JsonType | undefined; /** Call `listener` when flags load and whenever they change. Returns a function that removes it. */ onChange(listener: (flags: InsightsFlagsChange) => void): () => void; /** Fetch flags again, for example after a person property they depend on changed. */ reload(): void; } /** Options for `insights.verify()`. */ export interface InsightsVerifyOptions { /** Don't print the summary to the console. */ silent?: boolean; /** How long to wait for the API host, in milliseconds. Defaults to 5000. */ timeoutMs?: number; } /** The result of `insights.verify()`. */ export interface InsightsVerifyResult { /** `true` when no issues were found. */ ok: boolean; /** The API host that was checked. */ host: string; /** * The host's region: `'us'` for https://us.oxvo.com, `'eu'` for https://eu.oxvo.com, or * `'unknown'` for any other host. It's only a label: events always go to the host as given. */ region: 'us' | 'eu' | 'unknown'; /** Where consent stands. */ consent: InsightsConsentStatus; /** Whether events are being captured right now. */ capturing: boolean; /** Problems found, each with what to do next. Empty when `ok` is `true`. */ issues: string[]; } export {}; //# sourceMappingURL=insights.d.ts.map