diff --git a/.changeset/strong-worms-clap.md b/.changeset/strong-worms-clap.md new file mode 100644 index 000000000..76fd2b85b --- /dev/null +++ b/.changeset/strong-worms-clap.md @@ -0,0 +1,5 @@ +--- +'@segment/analytics-next': minor +--- + +Adding apiHost to the readonly data available from Analytics.settings diff --git a/packages/browser/src/browser/__tests__/integration.test.ts b/packages/browser/src/browser/__tests__/integration.test.ts index 913b1a471..1254962fc 100644 --- a/packages/browser/src/browser/__tests__/integration.test.ts +++ b/packages/browser/src/browser/__tests__/integration.test.ts @@ -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 () => { diff --git a/packages/browser/src/core/analytics/index.ts b/packages/browser/src/core/analytics/index.ts index 976f3c8a1..1cdf12e95 100644 --- a/packages/browser/src/core/analytics/index.ts +++ b/packages/browser/src/core/analytics/index.ts @@ -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' @@ -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: {}, @@ -201,7 +211,7 @@ export class Analytics super() const cookieOptions = options?.cookie const disablePersistance = options?.disableClientPersistence ?? false - this.settings = new AnalyticsInstanceSettings(settings) + this.queue = queue ?? createDefaultQueue( @@ -209,6 +219,7 @@ export class Analytics options?.retryQueue, disablePersistance ) + this.settings = new AnalyticsInstanceSettings(settings, this.queue) const storageSetting = options?.storage this._universalStorage = this.createStore( diff --git a/packages/browser/src/plugins/segmentio/index.ts b/packages/browser/src/plugins/segmentio/index.ts index 08116b923..20186b9bb 100644 --- a/packages/browser/src/plugins/segmentio/index.ts +++ b/packages/browser/src/plugins/segmentio/index.ts @@ -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, @@ -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',