From c2c349b36571d450bf21168f8cfd26f2660a72d4 Mon Sep 17 00:00:00 2001 From: joe-ayoub-segment <45374896+joe-ayoub-segment@users.noreply.github.com> Date: Mon, 8 Jan 2024 14:07:11 +0100 Subject: [PATCH] adding change to bucket web integration from PR 1777 --- .../bucket/src/__tests__/index.test.ts | 50 +++++++++++++++++-- .../bucket/src/group/__tests__/index.test.ts | 6 +-- .../src/identifyUser/__tests__/index.test.ts | 2 +- .../destinations/bucket/src/index.ts | 15 +++++- .../destinations/bucket/src/test-utils.ts | 7 ++- .../src/trackEvent/__tests__/index.test.ts | 6 +-- 6 files changed, 72 insertions(+), 14 deletions(-) diff --git a/packages/browser-destinations/destinations/bucket/src/__tests__/index.test.ts b/packages/browser-destinations/destinations/bucket/src/__tests__/index.test.ts index 723ab6037b..b47472f8a4 100644 --- a/packages/browser-destinations/destinations/bucket/src/__tests__/index.test.ts +++ b/packages/browser-destinations/destinations/bucket/src/__tests__/index.test.ts @@ -70,11 +70,55 @@ describe('Bucket', () => { analyticsInstance.reset() expect(getBucketCallLog()).toStrictEqual([ - { method: 'init', args: ['testTrackingKey'] }, + { method: 'init', args: ['testTrackingKey', {}] }, { method: 'reset', args: [] } ]) }) + it('passes options to bucket.init()', async () => { + const [instance] = await bucketWebDestination({ + trackingKey: 'testTrackingKey', + host: 'http://localhost:3200', + subscriptions: subscriptions as unknown as JSONArray + }) + + const analyticsInstance = new Analytics({ writeKey: 'test-writekey' }) + + await instance.load(Context.system(), analyticsInstance) + + expect(getBucketCallLog()).toStrictEqual([ + { method: 'init', args: ['testTrackingKey', { host: 'http://localhost:3200' }] } + ]) + }) + + it('allows sdkVersion override', async () => { + const [instance] = await bucketWebDestination({ + trackingKey: 'testTrackingKey', + sdkVersion: 'latest', + subscriptions: subscriptions as unknown as JSONArray + }) + + const analyticsInstance = new Analytics({ writeKey: 'test-writekey' }) + + await instance.load(Context.system(), analyticsInstance) + + const scripts = Array.from(window.document.querySelectorAll('script')) + expect(scripts).toMatchInlineSnapshot(` + Array [ + , + ] + `) + + expect(getBucketCallLog()).toStrictEqual([{ method: 'init', args: ['testTrackingKey', {}] }]) + }) + describe('when not logged in', () => { it('initializes Bucket SDK', async () => { const [instance] = await bucketWebDestination({ @@ -86,7 +130,7 @@ describe('Bucket', () => { await instance.load(Context.system(), analyticsInstance) - expect(getBucketCallLog()).toStrictEqual([{ method: 'init', args: ['testTrackingKey'] }]) + expect(getBucketCallLog()).toStrictEqual([{ method: 'init', args: ['testTrackingKey', {}] }]) }) }) @@ -108,7 +152,7 @@ describe('Bucket', () => { await instance.load(Context.system(), analyticsInstance) expect(getBucketCallLog()).toStrictEqual([ - { method: 'init', args: ['testTrackingKey'] }, + { method: 'init', args: ['testTrackingKey', {}] }, { method: 'user', args: ['test-user-id-1', {}, { active: false }] } ]) }) diff --git a/packages/browser-destinations/destinations/bucket/src/group/__tests__/index.test.ts b/packages/browser-destinations/destinations/bucket/src/group/__tests__/index.test.ts index e9e4787fd4..fbba8d8467 100644 --- a/packages/browser-destinations/destinations/bucket/src/group/__tests__/index.test.ts +++ b/packages/browser-destinations/destinations/bucket/src/group/__tests__/index.test.ts @@ -71,7 +71,7 @@ describe('Bucket.company', () => { ) expect(getBucketCallLog()).toStrictEqual([ - { method: 'init', args: ['testTrackingKey'] }, + { method: 'init', args: ['testTrackingKey', {}] }, { method: 'user', args: ['user-id-1', {}, { active: false }] @@ -129,7 +129,7 @@ describe('Bucket.company', () => { ) expect(getBucketCallLog()).toStrictEqual([ - { method: 'init', args: ['testTrackingKey'] }, + { method: 'init', args: ['testTrackingKey', {}] }, { method: 'user', args: ['user-id-1'] @@ -180,7 +180,7 @@ describe('Bucket.company', () => { // and then trigger the full flow trhough analytics.group() with only an anonymous ID // expect(destination.actions.group.perform).not.toHaveBeenCalled() - expect(getBucketCallLog()).toStrictEqual([{ method: 'init', args: ['testTrackingKey'] }]) + expect(getBucketCallLog()).toStrictEqual([{ method: 'init', args: ['testTrackingKey', {}] }]) }) }) }) diff --git a/packages/browser-destinations/destinations/bucket/src/identifyUser/__tests__/index.test.ts b/packages/browser-destinations/destinations/bucket/src/identifyUser/__tests__/index.test.ts index 255a2c2050..51a7e58239 100644 --- a/packages/browser-destinations/destinations/bucket/src/identifyUser/__tests__/index.test.ts +++ b/packages/browser-destinations/destinations/bucket/src/identifyUser/__tests__/index.test.ts @@ -61,7 +61,7 @@ describe('Bucket.user', () => { ) expect(getBucketCallLog()).toStrictEqual([ - { method: 'init', args: ['testTrackingKey'] }, + { method: 'init', args: ['testTrackingKey', {}] }, { method: 'user', args: [ diff --git a/packages/browser-destinations/destinations/bucket/src/index.ts b/packages/browser-destinations/destinations/bucket/src/index.ts index c463095200..067679a67c 100644 --- a/packages/browser-destinations/destinations/bucket/src/index.ts +++ b/packages/browser-destinations/destinations/bucket/src/index.ts @@ -60,10 +60,21 @@ export const destination: BrowserDestinationDefinition = { }, initialize: async ({ settings, analytics }, deps) => { - await deps.loadScript('https://cdn.jsdelivr.net/npm/@bucketco/tracking-sdk@2') + const { + // @ts-expect-error versionSettings is not part of the settings object but they are injected by Analytics 2.0, making Braze SDK raise a warning when we initialize it. + versionSettings, + // @ts-expect-error same as above. + subscriptions, + + trackingKey, + // @ts-expect-error Code-only SDK version override. Can be set via analytics.load() integrations overrides + sdkVersion = '2', + ...options + } = settings + await deps.loadScript(`https://cdn.jsdelivr.net/npm/@bucketco/tracking-sdk@${sdkVersion}`) await deps.resolveWhen(() => window.bucket != undefined, 100) - window.bucket.init(settings.trackingKey) + window.bucket.init(settings.trackingKey, options) // If the analytics client already has a logged in user from a // previous session or page, consider the user logged in. diff --git a/packages/browser-destinations/destinations/bucket/src/test-utils.ts b/packages/browser-destinations/destinations/bucket/src/test-utils.ts index 3c4cdb28a9..baabbb6e6d 100644 --- a/packages/browser-destinations/destinations/bucket/src/test-utils.ts +++ b/packages/browser-destinations/destinations/bucket/src/test-utils.ts @@ -38,7 +38,9 @@ export function bucketTestHooks() { }) beforeEach(() => { - nock('https://cdn.jsdelivr.net').get('/npm/@bucketco/tracking-sdk@2').reply(200, bucketTestMock) + nock('https://cdn.jsdelivr.net') + .get((uri) => uri.startsWith('/npm/@bucketco/tracking-sdk@')) + .reply(200, bucketTestMock) }) afterEach(function () { @@ -46,8 +48,9 @@ export function bucketTestHooks() { // @ts-expect-error no-unsafe-call // eslint-disable-next-line @typescript-eslint/no-unsafe-call this.test.error(new Error('Not all nock interceptors were used!')) - nock.cleanAll() } + + nock.cleanAll() }) afterAll(() => { diff --git a/packages/browser-destinations/destinations/bucket/src/trackEvent/__tests__/index.test.ts b/packages/browser-destinations/destinations/bucket/src/trackEvent/__tests__/index.test.ts index d7f583bc20..5c94ef5aab 100644 --- a/packages/browser-destinations/destinations/bucket/src/trackEvent/__tests__/index.test.ts +++ b/packages/browser-destinations/destinations/bucket/src/trackEvent/__tests__/index.test.ts @@ -64,7 +64,7 @@ describe('trackEvent', () => { ) expect(getBucketCallLog()).toStrictEqual([ - { method: 'init', args: ['testTrackingKey'] }, + { method: 'init', args: ['testTrackingKey', {}] }, { method: 'user', args: ['user-id-1', {}, { active: false }] @@ -109,7 +109,7 @@ describe('trackEvent', () => { ) expect(getBucketCallLog()).toStrictEqual([ - { method: 'init', args: ['testTrackingKey'] }, + { method: 'init', args: ['testTrackingKey', {}] }, { method: 'user', args: ['user-id-1'] @@ -153,7 +153,7 @@ describe('trackEvent', () => { // and then trigger the full flow trhough analytics.track() with only an anonymous ID // expect(destination.actions.trackEvent.perform).not.toHaveBeenCalled() - expect(getBucketCallLog()).toStrictEqual([{ method: 'init', args: ['testTrackingKey'] }]) + expect(getBucketCallLog()).toStrictEqual([{ method: 'init', args: ['testTrackingKey', {}] }]) }) }) })