/** * Segment integration types */ import type { Properties } from './common' /** * Segment user object */ export type SegmentUser = { anonymousId(): string | undefined id(): string | undefined } type SegmentAnalyticsUser = { anonymousId(): string | null | undefined id(): string | null | undefined } /** * Segment analytics object used for integration with OxvoInsights */ export type SegmentAnalytics = { user: () => SegmentAnalyticsUser | Promise /** * Segment SDK versions use different plugin and resolved value types. `any` keeps current SDK types assignable * without breaking legacy consumers that expect `Promise`. */ register: (integration: any) => Promise } /** * Filters non-colliding properties generated by OxvoInsights before they are added to a Segment event. * Original Segment properties are excluded. Returning `null` skips OxvoInsights enrichment for that event. */ export type SegmentEnrichmentFilterFn = (properties: Properties) => Properties | null /** * Configuration for the OxvoInsights Segment integration. */ export type SegmentIntegrationConfig = { /** The Segment analytics object. */ analytics: SegmentAnalytics /** * A function or array of functions called with the properties generated by OxvoInsights before they are added to a * Segment event. Return the properties to add, or `null` to leave the original Segment event unenriched. * * Original Segment properties are not passed to these functions. They are merged afterward and take precedence * over properties returned by this hook. If a function throws, the original Segment event is left unchanged. */ filterProperties?: SegmentEnrichmentFilterFn | SegmentEnrichmentFilterFn[] } /** * Segment plugin function type */ export type SegmentFunction = (ctx: SegmentContext) => Promise | SegmentContext /** * Segment plugin interface * Loosely based on https://github.com/segmentio/analytics-next/blob/master/packages/core/src/plugins/index.ts */ export interface SegmentPlugin { name: string version: string type: 'enrichment' isLoaded: () => boolean load: (ctx: SegmentContext, instance: any, config?: any) => Promise unload?: (ctx: SegmentContext, instance: any) => Promise | unknown ready?: () => Promise track?: SegmentFunction identify?: SegmentFunction page?: SegmentFunction group?: SegmentFunction alias?: SegmentFunction screen?: SegmentFunction } /** * Segment context object */ export interface SegmentContext { event: { event: string userId?: string anonymousId?: string properties: any } }