Skip to content

Commit bedea03

Browse files
Adding apiHost from segment.io settings, or default (#1160)
Co-authored-by: Seth Silesky <[email protected]>
1 parent 58ca71a commit bedea03

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

.changeset/strong-worms-clap.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+
Adding apiHost to the readonly data available from Analytics.settings

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,12 +1041,15 @@ describe('public settings api', () => {
10411041
cdnSettings: cdnSettingsMinimal,
10421042
})
10431043

1044-
expect(analytics.settings).toEqual({
1045-
writeKey,
1046-
cdnSettings: cdnSettingsMinimal,
1047-
timeout: 300,
1048-
cdnURL: 'https://cdn.segment.com',
1049-
})
1044+
expect(analytics.settings).toEqual(
1045+
expect.objectContaining({
1046+
writeKey,
1047+
cdnSettings: cdnSettingsMinimal,
1048+
timeout: 300,
1049+
cdnURL: 'https://cdn.segment.com',
1050+
apiHost: 'api.segment.io/v1',
1051+
})
1052+
)
10501053
})
10511054

10521055
it('should have a writeKey', async () => {

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ import {
5151
} from '../storage'
5252
import { setGlobalAnalytics } from '../../lib/global-analytics-helper'
5353
import { popPageContext } from '../buffer'
54+
import {
55+
isSegmentPlugin,
56+
SegmentIOPluginMetadata,
57+
} from '../../plugins/segmentio'
5458

5559
const deprecationWarning =
5660
'This is being deprecated and will be not be available in future releases of Analytics JS'
@@ -81,13 +85,19 @@ export class AnalyticsInstanceSettings {
8185
*/
8286
readonly cdnSettings: CDNSettings
8387
readonly cdnURL?: string
88+
get apiHost(): string | undefined {
89+
return this._getSegmentPluginMetadata?.()?.apiHost
90+
}
91+
private _getSegmentPluginMetadata?: () => SegmentIOPluginMetadata | undefined
8492

8593
/**
8694
* Auto-track specific timeout setting for legacy purposes.
8795
*/
8896
timeout = 300
8997

90-
constructor(settings: AnalyticsSettings) {
98+
constructor(settings: AnalyticsSettings, queue: EventQueue) {
99+
this._getSegmentPluginMetadata = () =>
100+
queue.plugins.find(isSegmentPlugin)?.metadata
91101
this.writeKey = settings.writeKey
92102
this.cdnSettings = settings.cdnSettings ?? {
93103
integrations: {},
@@ -201,14 +211,15 @@ export class Analytics
201211
super()
202212
const cookieOptions = options?.cookie
203213
const disablePersistance = options?.disableClientPersistence ?? false
204-
this.settings = new AnalyticsInstanceSettings(settings)
214+
205215
this.queue =
206216
queue ??
207217
createDefaultQueue(
208218
`${settings.writeKey}:event-queue`,
209219
options?.retryQueue,
210220
disablePersistance
211221
)
222+
this.settings = new AnalyticsInstanceSettings(settings, this.queue)
212223

213224
const storageSetting = options?.storage
214225
this._universalStorage = this.createStore(

packages/browser/src/plugins/segmentio/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ function onAlias(analytics: Analytics, json: JSON): JSON {
5050
return json
5151
}
5252

53+
export type SegmentIOPluginMetadata = {
54+
writeKey: string
55+
apiHost: string
56+
protocol: string
57+
}
58+
export interface SegmentIOPlugin extends Plugin {
59+
metadata: SegmentIOPluginMetadata
60+
}
61+
62+
export const isSegmentPlugin = (plugin: Plugin): plugin is SegmentIOPlugin => {
63+
return plugin.name === 'Segment.io'
64+
}
65+
5366
export function segmentio(
5467
analytics: Analytics,
5568
settings?: SegmentioSettings,
@@ -129,7 +142,12 @@ export function segmentio(
129142
})
130143
}
131144

132-
const segmentio: Plugin = {
145+
const segmentio: SegmentIOPlugin = {
146+
metadata: {
147+
writeKey,
148+
apiHost,
149+
protocol,
150+
},
133151
name: 'Segment.io',
134152
type: 'destination',
135153
version: '0.1.0',

0 commit comments

Comments
 (0)