import { Facade } from '@segment/facade' import { Analytics } from '../../core/analytics' import { LegacySettings } from '../../browser' import { isOffline } from '../../core/connection' import { Context } from '../../core/context' import { Plugin } from '../../core/plugin' import { PriorityQueue } from '../../lib/priority-queue' import { PersistedPriorityQueue } from '../../lib/priority-queue/persisted' import { toFacade } from '../../lib/to-facade' import batch, { BatchingDispatchConfig } from './batched-dispatcher' import standard, { StandardDispatcherConfig } from './fetch-dispatcher' import { normalize } from './normalize' import { scheduleFlush } from './schedule-flush' import { acceptDevice, deviceDeliveryEvent, finalizeDevice, isDeviceEvent, } from './device-delivery' import { deviceSupersession } from './device-supersession' type DeliveryStrategy = | { strategy?: 'standard' config?: StandardDispatcherConfig } | { strategy?: 'batching' config?: BatchingDispatchConfig } export type CustomerioSettings = { apiKey: string apiHost?: string protocol?: 'http' | 'https' addBundledMetadata?: boolean unbundledIntegrations?: string[] bundledConfigIds?: string[] unbundledConfigIds?: string[] maybeBundledConfigIds?: Record deliveryStrategy?: DeliveryStrategy } type JSON = ReturnType function onAlias(analytics: Analytics, json: JSON): JSON { const user = analytics.user() json.previousId = json.previousId ?? json.from ?? user.id() ?? user.anonymousId() json.userId = json.userId ?? json.to delete json.from delete json.to return json } export function customerio( analytics: Analytics, settings?: CustomerioSettings, integrations?: LegacySettings['integrations'] ): Plugin { // Attach `pagehide` before buffer is created so that inflight events are added // to the buffer before the buffer persists events in its own `pagehide` handler. window.addEventListener('pagehide', () => { buffer.push(...Array.from(inflightEvents)) inflightEvents.clear() }) const buffer = analytics.options.disableClientPersistence ? new PriorityQueue(analytics.queue.queue.maxAttempts, []) : new PersistedPriorityQueue( analytics.queue.queue.maxAttempts, `dest-Customer.io-Data-Pipelines` ) const supersession = deviceSupersession() const inflightEvents = new Set() const flushing = false const apiHost = settings?.apiHost ?? 'cdp.customer.io/v1' const protocol = settings?.protocol ?? 'https' const remote = `${protocol}://${apiHost}` const deliveryStrategy = settings?.deliveryStrategy const client = deliveryStrategy?.strategy === 'batching' ? batch(remote, deliveryStrategy.config) : standard(deliveryStrategy?.config as StandardDispatcherConfig) // Device lifecycle calls require a real HTTP acknowledgement, even when // ordinary analytics events use fire-and-forget batching. const deviceClient = standard({ keepalive: true }) async function send(ctx: Context): Promise { supersession.supersede(ctx, (predicate) => { const analyticsRemoved = analytics.queue.queue.remove(predicate) // Resolve dispatch waiters for events cancelled before core delivery. analyticsRemoved.forEach((item) => analytics.queue.emit('flush', item, false) ) const removed = [ ...buffer.remove(predicate), ...analyticsRemoved, ...Array.from(inflightEvents).filter(predicate), ] removed.forEach((item) => inflightEvents.delete(item)) return removed }) if (supersession.stale(ctx)) return ctx const lifecycle = isDeviceEvent(ctx) if (isOffline()) { if (lifecycle) analytics.emit(deviceDeliveryEvent, ctx) buffer.push(ctx) // eslint-disable-next-line @typescript-eslint/no-use-before-define scheduleFlush(flushing, buffer, customerio, scheduleFlush) return ctx } inflightEvents.add(ctx) const path = ctx.event.type.charAt(0) let json = toFacade(ctx.event).json() if (ctx.event.type === 'track') { delete json.traits } if (ctx.event.type === 'alias') { json = onAlias(analytics, json) } return (lifecycle ? deviceClient : client) .dispatch( `${remote}/${path}`, normalize(analytics, json, settings, integrations) ) .then((response) => { if (lifecycle) { const { ok, status } = response as Response const terminalDelete = ctx.event.event === 'Device Deleted' && status >= 400 && status < 500 && status !== 408 && status !== 429 if (!ok && !terminalDelete) throw new Error('Web push device request was not accepted.') if (ok) acceptDevice(ctx) else finalizeDevice(ctx) analytics.emit(deviceDeliveryEvent, ctx) } return ctx }) .catch(() => { if (supersession.stale(ctx)) return ctx if (lifecycle) analytics.emit(deviceDeliveryEvent, ctx) buffer.pushWithBackoff(ctx) // eslint-disable-next-line @typescript-eslint/no-use-before-define scheduleFlush(flushing, buffer, customerio, scheduleFlush) return ctx }) .finally(() => { inflightEvents.delete(ctx) }) } const customerio: Plugin = { name: 'Customer.io Data Pipelines', type: 'after', version: '0.1.0', isLoaded: (): boolean => true, load: (): Promise => Promise.resolve(), track: send, identify: send, page: send, alias: send, group: send, screen: send, } // Buffer may already have items if they were previously stored in localStorage. // Start flushing them immediately. if (buffer.todo) { scheduleFlush(flushing, buffer, customerio, scheduleFlush) } return customerio }