Skip to content

Commit 7f9d5a0

Browse files
authored
Settings refactor (#1212)
1 parent 22e6b16 commit 7f9d5a0

File tree

18 files changed

+279
-258
lines changed

18 files changed

+279
-258
lines changed

.changeset/chilly-mugs-applaud.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-core': patch
3+
---
4+
5+
Refactor settings

.changeset/old-stingrays-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-core': patch
3+
---
4+
5+
Update types / variable names

packages/browser/src/browser/index.ts

Lines changed: 6 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import { getProcessEnv } from '../lib/get-process-env'
22
import { getCDN, setGlobalCDNUrl } from '../lib/parse-cdn'
3-
4-
import { fetch } from '../lib/fetch'
5-
import { Analytics, NullAnalytics, InitOptions } from '../core/analytics'
3+
import { Analytics, NullAnalytics } from '../core/analytics'
64
import { Context } from '../core/context'
7-
import { Plan } from '../core/events'
85
import { Plugin } from '../core/plugin'
9-
import { MetricsOptions } from '../core/stats/remote-metrics'
106
import { mergedOptions } from '../lib/merged-options'
117
import { createDeferred } from '@segment/analytics-generic-utils'
128
import { envEnrichment } from '../plugins/env-enrichment'
13-
import {
14-
PluginFactory,
15-
remoteLoader,
16-
RemotePlugin,
17-
} from '../plugins/remote-loader'
18-
import type { RoutingRule } from '../plugins/routing-middleware'
9+
import { PluginFactory, remoteLoader } from '../plugins/remote-loader'
1910
import { segmentio, SegmentioSettings } from '../plugins/segmentio'
2011
import {
2112
AnalyticsBuffered,
@@ -31,127 +22,10 @@ import { ClassicIntegrationSource } from '../plugins/ajs-destination/types'
3122
import { attachInspector } from '../core/inspector'
3223
import { Stats } from '../core/stats'
3324
import { setGlobalAnalyticsKey } from '../lib/global-analytics-helper'
25+
import { CDNSettings, AnalyticsBrowserSettings, InitOptions } from './settings'
26+
import { fetch } from '../lib/fetch'
3427

35-
export interface RemoteIntegrationSettings {
36-
/* @deprecated - This does not indicate browser types anymore */
37-
type?: string
38-
39-
versionSettings?: {
40-
version?: string
41-
override?: string
42-
componentTypes?: ('browser' | 'android' | 'ios' | 'server')[]
43-
}
44-
45-
/**
46-
* We know if an integration is device mode if it has `bundlingStatus: 'bundled'` and the `browser` componentType in `versionSettings`.
47-
* History: The term 'bundle' is left over from before action destinations, when a device mode destinations were 'bundled' in a custom bundle for every analytics.js source.
48-
*/
49-
bundlingStatus?: 'bundled' | 'unbundled'
50-
51-
/**
52-
* Consent settings for the integration
53-
*/
54-
consentSettings?: {
55-
/**
56-
* Consent categories for the integration
57-
* @example ["CAT001", "CAT002"]
58-
*/
59-
categories: string[]
60-
}
61-
62-
// Segment.io specific
63-
retryQueue?: boolean
64-
65-
// any extra unknown settings
66-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67-
[key: string]: any
68-
}
69-
70-
/**
71-
* The remote settings object for a source, typically fetched from the Segment CDN.
72-
* Warning: this is an *unstable* object.
73-
*/
74-
export interface CDNSettings {
75-
integrations: {
76-
[creationName: string]: RemoteIntegrationSettings
77-
}
78-
79-
middlewareSettings?: {
80-
routingRules: RoutingRule[]
81-
}
82-
83-
enabledMiddleware?: Record<string, boolean>
84-
metrics?: MetricsOptions
85-
86-
plan?: Plan
87-
88-
legacyVideoPluginsEnabled?: boolean
89-
90-
remotePlugins?: RemotePlugin[]
91-
92-
/**
93-
* Top level consent settings
94-
*/
95-
consentSettings?: {
96-
/**
97-
* All unique consent categories for enabled destinations.
98-
* There can be categories in this array that are important for consent that are not included in any integration (e.g. 2 cloud mode categories).
99-
* @example ["Analytics", "Advertising", "CAT001"]
100-
*/
101-
allCategories: string[]
102-
103-
/**
104-
* Whether or not there are any unmapped destinations for enabled destinations.
105-
*/
106-
hasUnmappedDestinations: boolean
107-
}
108-
/**
109-
* Settings for edge function. Used for signals.
110-
*/
111-
edgeFunction?: // this is technically non-nullable according to ajs-renderer atm, but making it optional because it's strange API choice, and we might want to change it.
112-
| {
113-
/**
114-
* The URL of the edge function (.js file).
115-
* @example 'https://cdn.edgefn.segment.com/MY-WRITEKEY/foo.js',
116-
*/
117-
downloadURL: string
118-
/**
119-
* The version of the edge function
120-
* @example 1
121-
*/
122-
version: number
123-
}
124-
| {}
125-
126-
/**
127-
* Settings for auto instrumentation
128-
*/
129-
autoInstrumentationSettings?: {
130-
sampleRate: number
131-
}
132-
}
133-
134-
export interface AnalyticsBrowserSettings {
135-
writeKey: string
136-
/**
137-
* The settings for the Segment Source.
138-
* If provided, `AnalyticsBrowser` will not fetch remote settings
139-
* for the source.
140-
*/
141-
cdnSettings?: CDNSettings & Record<string, unknown>
142-
/**
143-
* If provided, will override the default Segment CDN (https://cdn.segment.com) for this application.
144-
*/
145-
cdnURL?: string
146-
/**
147-
* Plugins or npm-installed action destinations
148-
*/
149-
plugins?: (Plugin | PluginFactory)[]
150-
/**
151-
* npm-installed classic destinations
152-
*/
153-
classicIntegrations?: ClassicIntegrationSource[]
154-
}
28+
export type { CDNSettings, AnalyticsBrowserSettings }
15529

15630
export function loadCDNSettings(
15731
writeKey: string,
@@ -316,7 +190,7 @@ async function registerPlugins(
316190
basePlugins.push(
317191
await segmentio(
318192
analytics,
319-
mergedSettings['Segment.io'] as SegmentioSettings,
193+
mergedSettings['Segment.io'],
320194
cdnSettings.integrations
321195
)
322196
)
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/**
2+
* These settings will be exposed via the public API
3+
*/
4+
import { IntegrationsOptions, Plan } from '../core/events'
5+
import { MetricsOptions } from '../core/stats/remote-metrics'
6+
import { ClassicIntegrationSource } from '../plugins/ajs-destination/types'
7+
import { PluginFactory, RemotePlugin } from '../plugins/remote-loader'
8+
import { Plugin } from '../core/plugin'
9+
import { RoutingRule } from '../plugins/routing-middleware'
10+
import { CookieOptions, StorageSettings } from '../core/storage'
11+
import { UserOptions } from '../core/user'
12+
import { HighEntropyHint } from '../lib/client-hints/interfaces'
13+
14+
export interface RemoteIntegrationSettings {
15+
/* @deprecated - This does not indicate browser types anymore */
16+
type?: string
17+
18+
versionSettings?: {
19+
version?: string
20+
override?: string
21+
componentTypes?: ('browser' | 'android' | 'ios' | 'server')[]
22+
}
23+
24+
/**
25+
* We know if an integration is device mode if it has `bundlingStatus: 'bundled'` and the `browser` componentType in `versionSettings`.
26+
* History: The term 'bundle' is left over from before action destinations, when a device mode destinations were 'bundled' in a custom bundle for every analytics.js source.
27+
*/
28+
bundlingStatus?: 'bundled' | 'unbundled'
29+
30+
/**
31+
* Consent settings for the integration
32+
*/
33+
consentSettings?: {
34+
/**
35+
* Consent categories for the integration
36+
* @example ["CAT001", "CAT002"]
37+
*/
38+
categories: string[]
39+
}
40+
41+
// Segment.io specific
42+
retryQueue?: boolean
43+
44+
// any extra unknown settings
45+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
46+
[key: string]: any
47+
}
48+
49+
/**
50+
* The remote settings object for a source, typically fetched from the Segment CDN.
51+
* Warning: this is an *unstable* object.
52+
*/
53+
export interface CDNSettings {
54+
integrations: {
55+
[creationName: string]: RemoteIntegrationSettings
56+
}
57+
58+
middlewareSettings?: {
59+
routingRules: RoutingRule[]
60+
}
61+
62+
enabledMiddleware?: Record<string, boolean>
63+
metrics?: MetricsOptions
64+
65+
plan?: Plan
66+
67+
legacyVideoPluginsEnabled?: boolean
68+
69+
remotePlugins?: RemotePlugin[]
70+
71+
/**
72+
* Top level consent settings
73+
*/
74+
consentSettings?: {
75+
/**
76+
* All unique consent categories for enabled destinations.
77+
* There can be categories in this array that are important for consent that are not included in any integration (e.g. 2 cloud mode categories).
78+
* @example ["Analytics", "Advertising", "CAT001"]
79+
*/
80+
allCategories: string[]
81+
82+
/**
83+
* Whether or not there are any unmapped destinations for enabled destinations.
84+
*/
85+
hasUnmappedDestinations: boolean
86+
}
87+
/**
88+
* Settings for edge function. Used for signals.
89+
*/
90+
edgeFunction?: // this is technically non-nullable according to ajs-renderer atm, but making it optional because it's strange API choice, and we might want to change it.
91+
| {
92+
/**
93+
* The URL of the edge function (.js file).
94+
* @example 'https://cdn.edgefn.segment.com/MY-WRITEKEY/foo.js',
95+
*/
96+
downloadURL: string
97+
/**
98+
* The version of the edge function
99+
* @example 1
100+
*/
101+
version: number
102+
}
103+
| {}
104+
105+
/**
106+
* Settings for auto instrumentation
107+
*/
108+
autoInstrumentationSettings?: {
109+
sampleRate: number
110+
}
111+
}
112+
113+
/**
114+
* These are the settings that are the first argument to the npm installed plugin.
115+
*/
116+
export interface AnalyticsBrowserSettings {
117+
// TODO: Having two different configuration patterns for snippet and npm is confusing.
118+
writeKey: string
119+
/**
120+
* The settings for the Segment Source.
121+
* If provided, `AnalyticsBrowser` will not fetch remote settings
122+
* for the source.
123+
*/
124+
cdnSettings?: CDNSettings & Record<string, unknown>
125+
/**
126+
* If provided, will override the default Segment CDN (https://cdn.segment.com) for this application.
127+
*/
128+
cdnURL?: string
129+
/**
130+
* Plugins or npm-installed action destinations
131+
*/
132+
plugins?: (Plugin | PluginFactory)[]
133+
/**
134+
* npm-installed classic destinations
135+
*/
136+
classicIntegrations?: ClassicIntegrationSource[]
137+
}
138+
139+
/**
140+
* The settings that are used to configure the analytics instance
141+
*/
142+
export interface AnalyticsSettings {
143+
writeKey: string
144+
cdnSettings?: CDNSettings
145+
cdnURL?: string
146+
}
147+
148+
export interface InitOptions {
149+
/**
150+
* Disables storing any data on the client-side via cookies or localstorage.
151+
* Defaults to `false`.
152+
*
153+
*/
154+
disableClientPersistence?: boolean
155+
/**
156+
* Disables automatically converting ISO string event properties into Dates.
157+
* ISO string to Date conversions occur right before sending events to a classic device mode integration,
158+
* after any destination middleware have been ran.
159+
* Defaults to `false`.
160+
*/
161+
disableAutoISOConversion?: boolean
162+
initialPageview?: boolean
163+
cookie?: CookieOptions
164+
storage?: StorageSettings
165+
user?: UserOptions
166+
group?: UserOptions
167+
integrations?: IntegrationsOptions
168+
plan?: Plan
169+
retryQueue?: boolean
170+
obfuscate?: boolean
171+
/**
172+
* This callback allows you to update/mutate CDN Settings.
173+
* This is called directly after settings are fetched from the CDN.
174+
* @internal
175+
*/
176+
updateCDNSettings?: (unstableCDNSettings: CDNSettings) => CDNSettings
177+
/**
178+
* Disables or sets constraints on processing of query string parameters
179+
*/
180+
useQueryString?:
181+
| boolean
182+
| {
183+
aid?: RegExp
184+
uid?: RegExp
185+
}
186+
/**
187+
* Array of high entropy Client Hints to request. These may be rejected by the user agent - only required hints should be requested.
188+
*/
189+
highEntropyValuesClientHints?: HighEntropyHint[]
190+
/**
191+
* When using the snippet, this is the key that points to the global analytics instance (e.g. window.analytics).
192+
* default: analytics
193+
*/
194+
globalAnalyticsKey?: string
195+
196+
/**
197+
* Disable sending any data to Segment's servers. All emitted events and API calls (including .ready()), will be no-ops, and no cookies or localstorage will be used.
198+
*
199+
* @example
200+
* ```ts
201+
* disable: process.env.NODE_ENV === 'test'
202+
* ```
203+
*/
204+
disable?:
205+
| boolean
206+
| ((unstableCDNSettings: CDNSettings) => boolean | Promise<boolean>)
207+
}

0 commit comments

Comments
 (0)