Skip to content

Commit 3be7b87

Browse files
committed
wip
1 parent 2299e9a commit 3be7b87

File tree

6 files changed

+107
-35
lines changed

6 files changed

+107
-35
lines changed

.changeset/violet-actors-pump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': minor
3+
---
4+
5+
Add public settings API

packages/browser/src/browser/__tests__/integration.test.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
2-
import { cdnSettingsKitchenSink } from '../../test-helpers/fixtures/cdn-settings'
2+
import {
3+
cdnSettingsKitchenSink,
4+
cdnSettingsMinimal,
5+
} from '../../test-helpers/fixtures/cdn-settings'
36
import { createMockFetchImplementation } from '../../test-helpers/fixtures/create-fetch-method'
47
import { Context } from '../../core/context'
58
import { Plugin } from '../../core/plugin'
@@ -23,7 +26,8 @@ import {
2326
highEntropyTestData,
2427
lowEntropyTestData,
2528
} from '../../test-helpers/fixtures/client-hints'
26-
import { getGlobalAnalytics, NullAnalytics } from '../..'
29+
import { getGlobalAnalytics } from '../../lib/global-analytics-helper'
30+
import { NullAnalytics } from '../../core/analytics'
2731
import { recordIntegrationMetric } from '../../core/stats/metric-helpers'
2832

2933
let fetchCalls: ReturnType<typeof parseFetchCall>[] = []
@@ -1030,24 +1034,47 @@ describe('use', () => {
10301034
})
10311035
})
10321036

1033-
describe('timeout', () => {
1034-
it('has a default timeout value', async () => {
1037+
describe('public settings api', () => {
1038+
it('has expected settings', async () => {
10351039
const [analytics] = await AnalyticsBrowser.load({
10361040
writeKey,
1041+
cdnSettings: cdnSettingsMinimal,
1042+
})
1043+
1044+
expect(analytics.settings).toEqual({
1045+
writeKey,
1046+
cdnSettings: cdnSettingsMinimal,
1047+
timeout: 300,
10371048
})
1038-
//@ts-ignore
1039-
expect(analytics.settings.timeout).toEqual(300)
1049+
})
1050+
1051+
it('should have a writeKey', async () => {
1052+
const [analytics] = await AnalyticsBrowser.load({
1053+
writeKey,
1054+
})
1055+
1056+
expect(analytics.settings.writeKey).toBe(writeKey)
1057+
})
1058+
1059+
it('should have cdn settings', async () => {
1060+
const [analytics] = await AnalyticsBrowser.load({
1061+
writeKey,
1062+
cdnSettings: cdnSettingsMinimal,
1063+
})
1064+
1065+
expect(analytics.settings.cdnSettings).toEqual(cdnSettingsMinimal)
10401066
})
10411067

10421068
it('can set a timeout value', async () => {
10431069
const [analytics] = await AnalyticsBrowser.load({
10441070
writeKey,
10451071
})
1072+
expect(analytics.settings.timeout).toEqual(300)
10461073
analytics.timeout(50)
1047-
//@ts-ignore
10481074
expect(analytics.settings.timeout).toEqual(50)
10491075
})
10501076
})
1077+
10511078
describe('register', () => {
10521079
it('will not invoke any plugins that have initialization errors', async () => {
10531080
const analytics = AnalyticsBrowser.load({

packages/browser/src/browser/index.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import { getProcessEnv } from '../lib/get-process-env'
22
import { getCDN, setGlobalCDNUrl } from '../lib/parse-cdn'
33

44
import { fetch } from '../lib/fetch'
5-
import {
6-
Analytics,
7-
AnalyticsSettings,
8-
NullAnalytics,
9-
InitOptions,
10-
} from '../core/analytics'
5+
import { Analytics, NullAnalytics, InitOptions } from '../core/analytics'
116
import { Context } from '../core/context'
127
import { Plan } from '../core/events'
138
import { Plugin } from '../core/plugin'
@@ -111,7 +106,8 @@ export interface CDNSettings {
111106
}
112107
}
113108

114-
export interface AnalyticsBrowserSettings extends AnalyticsSettings {
109+
export interface AnalyticsBrowserSettings {
110+
writeKey: string
115111
/**
116112
* The settings for the Segment Source.
117113
* If provided, `AnalyticsBrowser` will not fetch remote settings
@@ -122,6 +118,14 @@ export interface AnalyticsBrowserSettings extends AnalyticsSettings {
122118
* If provided, will override the default Segment CDN (https://cdn.segment.com) for this application.
123119
*/
124120
cdnURL?: string
121+
/**
122+
* Plugins or npm-installed action destinations
123+
*/
124+
plugins?: (Plugin | PluginFactory)[]
125+
/**
126+
* npm-installed classic destinations
127+
*/
128+
classicIntegrations?: ClassicIntegrationSource[]
125129
}
126130

127131
export function loadLegacySettings(
@@ -330,31 +334,31 @@ async function loadAnalytics(
330334
preInitBuffer.push(new PreInitMethodCall('page', []))
331335
}
332336

333-
let legacySettings =
337+
let cdnSettings =
334338
settings.cdnSettings ??
335339
(await loadLegacySettings(settings.writeKey, settings.cdnURL))
336340

337341
if (options.updateCDNSettings) {
338-
legacySettings = options.updateCDNSettings(legacySettings)
342+
cdnSettings = options.updateCDNSettings(cdnSettings)
339343
}
340344

341345
// if options.disable is a function, we allow user to disable analytics based on CDN Settings
342346
if (typeof options.disable === 'function') {
343-
const disabled = await options.disable(legacySettings)
347+
const disabled = await options.disable(cdnSettings)
344348
if (disabled) {
345349
return [new NullAnalytics(), Context.system()]
346350
}
347351
}
348352

349353
const retryQueue: boolean =
350-
legacySettings.integrations['Segment.io']?.retryQueue ?? true
354+
cdnSettings.integrations['Segment.io']?.retryQueue ?? true
351355

352356
options = {
353357
retryQueue,
354358
...options,
355359
}
356360

357-
const analytics = new Analytics(settings, options)
361+
const analytics = new Analytics({ ...settings, cdnSettings }, options)
358362

359363
attachInspector(analytics)
360364

@@ -367,8 +371,8 @@ async function loadAnalytics(
367371
| undefined
368372

369373
Stats.initRemoteMetrics({
370-
...legacySettings.metrics,
371-
host: segmentLoadOptions?.apiHost ?? legacySettings.metrics?.host,
374+
...cdnSettings.metrics,
375+
host: segmentLoadOptions?.apiHost ?? cdnSettings.metrics?.host,
372376
protocol: segmentLoadOptions?.protocol,
373377
})
374378

@@ -377,7 +381,7 @@ async function loadAnalytics(
377381

378382
const ctx = await registerPlugins(
379383
settings.writeKey,
380-
legacySettings,
384+
cdnSettings,
381385
analytics,
382386
options,
383387
plugins,

packages/browser/src/core/analytics/index.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ import { EventQueue } from '../queue/event-queue'
2828
import { Group, ID, User, UserOptions } from '../user'
2929
import autoBind from '../../lib/bind-all'
3030
import { PersistedPriorityQueue } from '../../lib/priority-queue/persisted'
31-
import type {
32-
LegacyIntegration,
33-
ClassicIntegrationSource,
34-
} from '../../plugins/ajs-destination/types'
31+
import type { LegacyIntegration } from '../../plugins/ajs-destination/types'
3532
import type {
3633
DestinationMiddlewareFunction,
3734
MiddlewareFunction,
@@ -52,7 +49,6 @@ import {
5249
initializeStorages,
5350
isArrayOfStoreType,
5451
} from '../storage'
55-
import { PluginFactory } from '../../plugins/remote-loader'
5652
import { setGlobalAnalytics } from '../../lib/global-analytics-helper'
5753
import { popPageContext } from '../buffer'
5854

@@ -75,11 +71,34 @@ function createDefaultQueue(
7571
return new EventQueue(priorityQueue)
7672
}
7773

74+
/**
75+
* The public settings that are set on the analytics instance
76+
*/
77+
export class AnalyticsInstanceSettings {
78+
readonly writeKey: string
79+
/**
80+
* This is an unstable API, it may change in the future without warning.
81+
*/
82+
readonly cdnSettings: CDNSettings
83+
/**
84+
* The timeout for the analytics request in milliseconds
85+
*/
86+
// this is an odd setting --
87+
// it's only used in autotrack? Not sure what the history is, or why it's publicly exposed.
88+
readonly timeout = 300
89+
90+
constructor(settings: AnalyticsSettings) {
91+
this.writeKey = settings.writeKey
92+
this.cdnSettings = settings.cdnSettings ?? { integrations: {} }
93+
}
94+
}
95+
96+
/**
97+
* The settings that are used to configure the analytics instance
98+
*/
7899
export interface AnalyticsSettings {
79100
writeKey: string
80-
timeout?: number
81-
plugins?: (Plugin | PluginFactory)[]
82-
classicIntegrations?: ClassicIntegrationSource[]
101+
cdnSettings?: CDNSettings
83102
}
84103

85104
export interface InitOptions {
@@ -155,7 +174,7 @@ export class Analytics
155174
extends Emitter
156175
implements AnalyticsCore, AnalyticsClassic
157176
{
158-
protected settings: AnalyticsSettings
177+
settings: AnalyticsInstanceSettings
159178
private _user: User
160179
private _group: Group
161180
private eventFactory: EventFactory
@@ -177,8 +196,7 @@ export class Analytics
177196
super()
178197
const cookieOptions = options?.cookie
179198
const disablePersistance = options?.disableClientPersistence ?? false
180-
this.settings = settings
181-
this.settings.timeout = this.settings.timeout ?? 300
199+
this.settings = new AnalyticsInstanceSettings(settings)
182200
this.queue =
183201
queue ??
184202
createDefaultQueue(

packages/browser/src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
export * from './core/analytics'
2-
export * from './browser'
1+
export { Analytics, AnalyticsSettings, InitOptions } from './core/analytics'
2+
export {
3+
AnalyticsBrowser,
4+
AnalyticsBrowserSettings,
5+
CDNSettings,
6+
RemoteIntegrationSettings,
7+
// Unclear why this is in the public API, but @cradek said that he saw a customer using this
8+
loadLegacySettings,
9+
} from './browser'
310
export * from './node'
411

512
export * from './core/context'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Analytics, AnalyticsSettings } from '../core/analytics'
2+
import { cdnSettingsMinimal } from './fixtures'
3+
4+
export class TestAnalytics extends Analytics {
5+
constructor(settings: Partial<AnalyticsSettings> = {}, ...args: any[]) {
6+
super(
7+
{ writeKey: 'test', cdnSettings: cdnSettingsMinimal, ...settings },
8+
...args
9+
)
10+
}
11+
}

0 commit comments

Comments
 (0)