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

Add updateCDNSettings() option #870

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/polite-bottles-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': minor
---

Add updateCDNSettings option
70 changes: 70 additions & 0 deletions packages/browser/src/browser/__tests__/update-cdn-settings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { AnalyticsBrowser } from '../..'
import { setGlobalCDNUrl } from '../../lib/parse-cdn'
import { remoteLoader } from '../../plugins/remote-loader'
import unfetch from 'unfetch'
import { createSuccess } from '../../test-helpers/factories'
jest.mock('unfetch')

const INTG_TO_DELETE = 'deleteMe'

const cdnSettings = {
integrations: {
[INTG_TO_DELETE]: { bar: true },
otherIntegration: { foo: true },
},
}
const mockFetchSettingsSuccessResponse = (cdnSettings: any) => {
return jest
.mocked(unfetch)
.mockImplementation(() => createSuccess(cdnSettings))
}

jest.mock('../../plugins/remote-loader')
const remoteLoaderSpy = jest.fn().mockResolvedValue([])
jest.mocked(remoteLoader).mockImplementation(remoteLoaderSpy)

describe('updateCDNSettings configuration option', () => {
beforeEach(() => {
setGlobalCDNUrl(undefined as any)
;(window as any).analytics = undefined
})
it('should update the configuration options if they are passed directly', async () => {
await AnalyticsBrowser.load(
{
writeKey: 'foo',
cdnSettings,
},
{
updateCDNSettings: (settings) => {
delete settings.integrations.deleteMe
return settings
},
}
)
const [arg1] = remoteLoaderSpy.mock.lastCall
expect(arg1.integrations.otherIntegration).toEqual(
cdnSettings.integrations.otherIntegration
)
expect(arg1.integrations[INTG_TO_DELETE]).toBeUndefined()
})

it('should update the configuration options if they are fetched', async () => {
mockFetchSettingsSuccessResponse(cdnSettings)
await AnalyticsBrowser.load(
{
writeKey: 'foo',
},
{
updateCDNSettings: (settings) => {
delete settings.integrations.deleteMe
return settings
},
}
)
const [arg1] = remoteLoaderSpy.mock.lastCall
expect(arg1.integrations.otherIntegration).toEqual(
cdnSettings.integrations.otherIntegration
)
expect(arg1.integrations[INTG_TO_DELETE]).toBeUndefined()
})
})
6 changes: 5 additions & 1 deletion packages/browser/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,14 @@ async function loadAnalytics(
// this is an ugly side-effect, but it's for the benefits of the plugins that get their cdn via getCDN()
if (settings.cdnURL) setGlobalCDNUrl(settings.cdnURL)

const legacySettings =
let legacySettings =
settings.cdnSettings ??
(await loadLegacySettings(settings.writeKey, settings.cdnURL))

if (options.updateCDNSettings) {
legacySettings = options.updateCDNSettings(legacySettings)
}

const retryQueue: boolean =
legacySettings.integrations['Segment.io']?.retryQueue ?? true

Expand Down
6 changes: 6 additions & 0 deletions packages/browser/src/core/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { PriorityQueue } from '../../lib/priority-queue'
import { getGlobal } from '../../lib/get-global'
import { AnalyticsClassic, AnalyticsCore } from './interfaces'
import { HighEntropyHint } from '../../lib/client-hints/interfaces'
import type { LegacySettings } from '../../browser'

const deprecationWarning =
'This is being deprecated and will be not be available in future releases of Analytics JS'
Expand Down Expand Up @@ -98,6 +99,11 @@ export interface InitOptions {
plan?: Plan
retryQueue?: boolean
obfuscate?: boolean
/**
* This callback allows you to update/mutate CDN Settings.
* This is called directly after settings are fetched from the CDN.
*/
updateCDNSettings?: (settings: LegacySettings) => LegacySettings
/**
* Disables or sets constraints on processing of query string parameters
*/
Expand Down