diff --git a/.changeset/tasty-badgers-fry.md b/.changeset/tasty-badgers-fry.md new file mode 100644 index 000000000..5595d9226 --- /dev/null +++ b/.changeset/tasty-badgers-fry.md @@ -0,0 +1,5 @@ +--- +'@segment/analytics-next': minor +--- + +Make keep-alive configurable and default to false diff --git a/packages/browser/src/plugins/segmentio/__tests__/index.test.ts b/packages/browser/src/plugins/segmentio/__tests__/index.test.ts index a7cea2f78..590d593dc 100644 --- a/packages/browser/src/plugins/segmentio/__tests__/index.test.ts +++ b/packages/browser/src/plugins/segmentio/__tests__/index.test.ts @@ -65,6 +65,40 @@ describe('Segment.io', () => { }) }) + describe('configuring a keep alive', () => { + it('should accept keepalive configuration', async () => { + const analytics = new Analytics({ writeKey: 'foo' }) + + await analytics.register( + segmentio(analytics, { + apiKey: '', + deliveryStrategy: { + config: { + keepalive: true, + }, + }, + }) + ) + + await analytics.track('foo') + const [_, params] = spyMock.mock.lastCall + expect(params.keepalive).toBe(true) + }) + + it('should default to no keepalive', async () => { + const analytics = new Analytics({ writeKey: 'foo' }) + + const segment = segmentio(analytics, { + apiKey: '', + }) + await analytics.register(segment) + await analytics.track('foo') + + const [_, params] = spyMock.mock.lastCall + expect(params.keepalive).toBeUndefined() + }) + }) + describe('#page', () => { it('should enqueue section, name and properties', async () => { await analytics.page('section', 'name', { property: true }, { opt: true }) diff --git a/packages/browser/src/plugins/segmentio/batched-dispatcher.ts b/packages/browser/src/plugins/segmentio/batched-dispatcher.ts index 10f2c0032..4a0193642 100644 --- a/packages/browser/src/plugins/segmentio/batched-dispatcher.ts +++ b/packages/browser/src/plugins/segmentio/batched-dispatcher.ts @@ -2,7 +2,7 @@ import { SegmentEvent } from '../../core/events' import { fetch } from '../../lib/fetch' import { onPageLeave } from '../../lib/on-page-leave' -type BatchingConfig = { +export type BatchingDispatchConfig = { size?: number timeout?: number } @@ -43,7 +43,10 @@ function chunks(batch: object[]): Array { return result } -export default function batch(apiHost: string, config?: BatchingConfig) { +export default function batch( + apiHost: string, + config?: BatchingDispatchConfig +) { let buffer: object[] = [] let pageUnloaded = false diff --git a/packages/browser/src/plugins/segmentio/fetch-dispatcher.ts b/packages/browser/src/plugins/segmentio/fetch-dispatcher.ts index bab74de85..d4118680c 100644 --- a/packages/browser/src/plugins/segmentio/fetch-dispatcher.ts +++ b/packages/browser/src/plugins/segmentio/fetch-dispatcher.ts @@ -2,10 +2,16 @@ import { fetch } from '../../lib/fetch' export type Dispatcher = (url: string, body: object) => Promise -export default function (): { dispatch: Dispatcher } { +export type StandardDispatcherConfig = { + keepalive?: boolean +} + +export default function (config?: StandardDispatcherConfig): { + dispatch: Dispatcher +} { function dispatch(url: string, body: object): Promise { return fetch(url, { - keepalive: true, + keepalive: config?.keepalive, headers: { 'Content-Type': 'text/plain' }, method: 'post', body: JSON.stringify(body), diff --git a/packages/browser/src/plugins/segmentio/index.ts b/packages/browser/src/plugins/segmentio/index.ts index 2a9714ee1..1e4d8eaaf 100644 --- a/packages/browser/src/plugins/segmentio/index.ts +++ b/packages/browser/src/plugins/segmentio/index.ts @@ -7,11 +7,21 @@ 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 from './batched-dispatcher' -import standard from './fetch-dispatcher' +import batch, { BatchingDispatchConfig } from './batched-dispatcher' +import standard, { StandardDispatcherConfig } from './fetch-dispatcher' import { normalize } from './normalize' import { scheduleFlush } from './schedule-flush' +type DeliveryStrategy = + | { + strategy?: 'standard' + config?: StandardDispatcherConfig + } + | { + strategy?: 'batching' + config?: BatchingDispatchConfig + } + export type SegmentioSettings = { apiKey: string apiHost?: string @@ -24,13 +34,7 @@ export type SegmentioSettings = { maybeBundledConfigIds?: Record - deliveryStrategy?: { - strategy?: 'standard' | 'batching' - config?: { - size?: number - timeout?: number - } - } + deliveryStrategy?: DeliveryStrategy } type JSON = ReturnType @@ -71,10 +75,11 @@ export function segmentio( const protocol = settings?.protocol ?? 'https' const remote = `${protocol}://${apiHost}` + const deliveryStrategy = settings?.deliveryStrategy const client = - settings?.deliveryStrategy?.strategy === 'batching' - ? batch(apiHost, settings?.deliveryStrategy?.config) - : standard() + deliveryStrategy?.strategy === 'batching' + ? batch(apiHost, deliveryStrategy.config) + : standard(deliveryStrategy?.config as StandardDispatcherConfig) async function send(ctx: Context): Promise { if (isOffline()) {