Skip to content

Commit

Permalink
Adding apiHost from segment.io settings, or default (#1160)
Browse files Browse the repository at this point in the history
Co-authored-by: Seth Silesky <[email protected]>
  • Loading branch information
MichaelGHSeg and silesky authored Sep 27, 2024
1 parent 58ca71a commit bedea03
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-worms-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': minor
---

Adding apiHost to the readonly data available from Analytics.settings
15 changes: 9 additions & 6 deletions packages/browser/src/browser/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,12 +1041,15 @@ describe('public settings api', () => {
cdnSettings: cdnSettingsMinimal,
})

expect(analytics.settings).toEqual({
writeKey,
cdnSettings: cdnSettingsMinimal,
timeout: 300,
cdnURL: 'https://cdn.segment.com',
})
expect(analytics.settings).toEqual(
expect.objectContaining({
writeKey,
cdnSettings: cdnSettingsMinimal,
timeout: 300,
cdnURL: 'https://cdn.segment.com',
apiHost: 'api.segment.io/v1',
})
)
})

it('should have a writeKey', async () => {
Expand Down
15 changes: 13 additions & 2 deletions packages/browser/src/core/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ import {
} from '../storage'
import { setGlobalAnalytics } from '../../lib/global-analytics-helper'
import { popPageContext } from '../buffer'
import {
isSegmentPlugin,
SegmentIOPluginMetadata,
} from '../../plugins/segmentio'

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

/**
* Auto-track specific timeout setting for legacy purposes.
*/
timeout = 300

constructor(settings: AnalyticsSettings) {
constructor(settings: AnalyticsSettings, queue: EventQueue) {
this._getSegmentPluginMetadata = () =>
queue.plugins.find(isSegmentPlugin)?.metadata
this.writeKey = settings.writeKey
this.cdnSettings = settings.cdnSettings ?? {
integrations: {},
Expand Down Expand Up @@ -201,14 +211,15 @@ export class Analytics
super()
const cookieOptions = options?.cookie
const disablePersistance = options?.disableClientPersistence ?? false
this.settings = new AnalyticsInstanceSettings(settings)

this.queue =
queue ??
createDefaultQueue(
`${settings.writeKey}:event-queue`,
options?.retryQueue,
disablePersistance
)
this.settings = new AnalyticsInstanceSettings(settings, this.queue)

const storageSetting = options?.storage
this._universalStorage = this.createStore(
Expand Down
20 changes: 19 additions & 1 deletion packages/browser/src/plugins/segmentio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ function onAlias(analytics: Analytics, json: JSON): JSON {
return json
}

export type SegmentIOPluginMetadata = {
writeKey: string
apiHost: string
protocol: string
}
export interface SegmentIOPlugin extends Plugin {
metadata: SegmentIOPluginMetadata
}

export const isSegmentPlugin = (plugin: Plugin): plugin is SegmentIOPlugin => {
return plugin.name === 'Segment.io'
}

export function segmentio(
analytics: Analytics,
settings?: SegmentioSettings,
Expand Down Expand Up @@ -129,7 +142,12 @@ export function segmentio(
})
}

const segmentio: Plugin = {
const segmentio: SegmentIOPlugin = {
metadata: {
writeKey,
apiHost,
protocol,
},
name: 'Segment.io',
type: 'destination',
version: '0.1.0',
Expand Down

0 comments on commit bedea03

Please sign in to comment.