Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding apiHost from segment.io settings, or default #1160

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions packages/browser/src/browser/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,7 @@ describe('public settings api', () => {
cdnSettings: cdnSettingsMinimal,
timeout: 300,
cdnURL: 'https://cdn.segment.com',
apiHost: 'api.segment.io/v1',
})
})

Expand Down
14 changes: 12 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,8 @@ import {
} from '../storage'
import { setGlobalAnalytics } from '../../lib/global-analytics-helper'
import { popPageContext } from '../buffer'
import { SegmentioSettings } from '../../plugins/segmentio'
import { SEGMENT_API_HOST } from '../constants'

const deprecationWarning =
'This is being deprecated and will be not be available in future releases of Analytics JS'
Expand Down Expand Up @@ -81,19 +83,21 @@ export class AnalyticsInstanceSettings {
*/
readonly cdnSettings: CDNSettings
readonly cdnURL?: string
readonly apiHost?: string

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

constructor(settings: AnalyticsSettings) {
constructor(settings: AnalyticsSettings, apiHost?: string) {
this.writeKey = settings.writeKey
this.cdnSettings = settings.cdnSettings ?? {
integrations: {},
edgeFunction: {},
}
this.cdnURL = settings.cdnURL
this.apiHost = apiHost ?? SEGMENT_API_HOST
}
}

Expand Down Expand Up @@ -201,7 +205,13 @@ export class Analytics
super()
const cookieOptions = options?.cookie
const disablePersistance = options?.disableClientPersistence ?? false
this.settings = new AnalyticsInstanceSettings(settings)

// Extract apiHost from Segment.io settings if available
const segmentLoadOptions = options?.integrations?.['Segment.io'] as
| SegmentioSettings
| undefined
const apiHost = segmentLoadOptions?.apiHost
Copy link
Contributor

@silesky silesky Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the final value afaik? these loadOptions are just overrides -- we would need to take into account the cdnSettings apiHost value.

One option is to just make this a getter to analytics.queue and create a metadata object on the plugin -- that way, don't have to recreate the logic in two places.

this.settings = new AnalyticsInstanceSettings(settings, apiHost)
this.queue =
queue ??
createDefaultQueue(
Expand Down
Loading