-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analytics-browser): consume remote config (#769)
This reverts commit d00981e.
- Loading branch information
Showing
19 changed files
with
353 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Mock [IndexedDB API](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) as jsdom doesn't support it | ||
require('fake-indexeddb/auto'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
jest.mock('@amplitude/analytics-remote-config', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const originalModule = jest.requireActual('@amplitude/analytics-browser'); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return { | ||
...originalModule, | ||
createRemoteConfigFetch: jest.fn().mockImplementation(() => ({ | ||
getRemoteConfig: jest.fn().mockImplementation(() => { | ||
// Mock it as no remote config is set | ||
// Thus return an empty object | ||
return Promise.resolve({}); | ||
}) | ||
})) | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { BrowserConfig as IBrowserConfig } from '@amplitude/analytics-types'; | ||
import { createRemoteConfigFetch, RemoteConfigFetch } from '@amplitude/analytics-remote-config'; | ||
import { BrowserRemoteConfig } from './types'; | ||
|
||
export class BrowserJoinedConfigGenerator { | ||
// Local config before generateJoinedConfig is called | ||
// Joined config after generateJoinedConfig is called | ||
config: IBrowserConfig; | ||
remoteConfigFetch: RemoteConfigFetch<BrowserRemoteConfig> | undefined; | ||
|
||
constructor(localConfig: IBrowserConfig) { | ||
this.config = localConfig; | ||
this.config.loggerProvider.debug( | ||
'Local configuration before merging with remote config', | ||
JSON.stringify(this.config, null, 2), | ||
); | ||
} | ||
async initialize() { | ||
this.remoteConfigFetch = await createRemoteConfigFetch<BrowserRemoteConfig>({ | ||
localConfig: this.config, | ||
configKeys: ['analyticsSDK'], | ||
}); | ||
} | ||
|
||
async generateJoinedConfig(): Promise<IBrowserConfig> { | ||
const remoteConfig = | ||
this.remoteConfigFetch && | ||
(await this.remoteConfigFetch.getRemoteConfig('analyticsSDK', 'browserSDK', this.config.sessionId)); | ||
this.config.loggerProvider.debug('Remote configuration:', JSON.stringify(remoteConfig, null, 2)); | ||
if (remoteConfig && remoteConfig.defaultTracking) { | ||
this.config.defaultTracking = remoteConfig.defaultTracking; | ||
} | ||
this.config.loggerProvider.debug('Joined configuration: ', JSON.stringify(remoteConfig, null, 2)); | ||
return this.config; | ||
} | ||
} | ||
|
||
export const createBrowserJoinedConfigGenerator = async (localConfig: IBrowserConfig) => { | ||
const joinedConfigGenerator = new BrowserJoinedConfigGenerator(localConfig); | ||
await joinedConfigGenerator.initialize(); | ||
return joinedConfigGenerator; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { DefaultTrackingOptions } from '@amplitude/analytics-types'; | ||
import { AutocaptureOptions } from '@amplitude/plugin-autocapture-browser'; | ||
|
||
export type BrowserRemoteConfig = { | ||
browserSDK: { | ||
autoCapture?: AutocaptureOptions; | ||
defaultTracking?: DefaultTrackingOptions; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/analytics-browser/test/config/joined-config.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { createRemoteConfigFetch, RemoteConfigFetch } from '@amplitude/analytics-remote-config'; | ||
import { BrowserConfig as IBrowserConfig } from '@amplitude/analytics-types'; | ||
import { BrowserJoinedConfigGenerator, createBrowserJoinedConfigGenerator } from '../../src/config/joined-config'; | ||
import { createConfigurationMock } from '../helpers/mock'; | ||
import { BrowserRemoteConfig } from '../../lib/scripts/config/types'; | ||
|
||
jest.mock('@amplitude/analytics-remote-config', () => ({ | ||
createRemoteConfigFetch: jest.fn(), | ||
})); | ||
|
||
describe('joined-config', () => { | ||
let localConfig: IBrowserConfig; | ||
let mockRemoteConfigFetch: RemoteConfigFetch<BrowserRemoteConfig>; | ||
let generator: BrowserJoinedConfigGenerator; | ||
|
||
beforeEach(() => { | ||
localConfig = { ...createConfigurationMock(), defaultTracking: false }; | ||
|
||
mockRemoteConfigFetch = { | ||
getRemoteConfig: jest.fn().mockResolvedValue({ | ||
defaultTracking: true, | ||
}), | ||
// TODO(xinyi): uncomment this line when fetchTime is used in the joined config | ||
// fetchTime: 23, | ||
}; | ||
|
||
// Mock the createRemoteConfigFetch to return the mockRemoteConfigFetch | ||
(createRemoteConfigFetch as jest.MockedFunction<typeof createRemoteConfigFetch>).mockResolvedValue( | ||
mockRemoteConfigFetch, | ||
); | ||
|
||
generator = new BrowserJoinedConfigGenerator(localConfig); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('BrowserJoinedConfigGenerator', () => { | ||
describe('constructor', () => { | ||
test('should set localConfig', () => { | ||
expect(generator.config).toEqual(localConfig); | ||
expect(generator.remoteConfigFetch).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('initialize', () => { | ||
test('should set remoteConfigFetch', async () => { | ||
await generator.initialize(); | ||
|
||
expect(generator.remoteConfigFetch).not.toBeUndefined(); | ||
expect(createRemoteConfigFetch).toHaveBeenCalledWith({ | ||
localConfig, | ||
configKeys: ['analyticsSDK'], | ||
}); | ||
expect(generator.remoteConfigFetch).toBe(mockRemoteConfigFetch); | ||
}); | ||
}); | ||
|
||
describe('generateJoinedConfig', () => { | ||
test('should merge local and remote config', async () => { | ||
await generator.initialize(); | ||
expect(generator.config.defaultTracking).toBe(false); | ||
const joinedConfig = await generator.generateJoinedConfig(); | ||
const expectedConfig = localConfig; | ||
expectedConfig.defaultTracking = true; | ||
|
||
expect(mockRemoteConfigFetch.getRemoteConfig).toHaveBeenCalledWith( | ||
'analyticsSDK', | ||
'browserSDK', | ||
localConfig.sessionId, | ||
); | ||
// expectedConfig also includes protected properties | ||
expect(joinedConfig).toEqual(expectedConfig); | ||
}); | ||
|
||
test('should use local config if remoteConfigFetch is not set', async () => { | ||
expect(generator.remoteConfigFetch).toBeUndefined(); | ||
const joinedConfig = await generator.generateJoinedConfig(); | ||
expect(joinedConfig).toEqual(localConfig); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('createBrowserJoinedConfigGenerator', () => { | ||
test('should create joined config generator', async () => { | ||
const generator = await createBrowserJoinedConfigGenerator(localConfig); | ||
|
||
expect(generator.config).toEqual(localConfig); | ||
expect(generator.remoteConfigFetch).toBe(mockRemoteConfigFetch); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.