Skip to content

Commit

Permalink
adding change to bucket web integration from PR 1777
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-ayoub-segment committed Jan 8, 2024
1 parent 71aeac0 commit c2c349b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
<script
src="https://cdn.jsdelivr.net/npm/@bucketco/tracking-sdk@latest"
status="loaded"
type="text/javascript"
/>,
<script>
// the emptiness
</script>,
]
`)

expect(getBucketCallLog()).toStrictEqual([{ method: 'init', args: ['testTrackingKey', {}] }])
})

describe('when not logged in', () => {
it('initializes Bucket SDK', async () => {
const [instance] = await bucketWebDestination({
Expand All @@ -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', {}] }])
})
})

Expand All @@ -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 }] }
])
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('Bucket.company', () => {
)

expect(getBucketCallLog()).toStrictEqual([
{ method: 'init', args: ['testTrackingKey'] },
{ method: 'init', args: ['testTrackingKey', {}] },
{
method: 'user',
args: ['user-id-1']
Expand Down Expand Up @@ -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', {}] }])
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('Bucket.user', () => {
)

expect(getBucketCallLog()).toStrictEqual([
{ method: 'init', args: ['testTrackingKey'] },
{ method: 'init', args: ['testTrackingKey', {}] },
{
method: 'user',
args: [
Expand Down
15 changes: 13 additions & 2 deletions packages/browser-destinations/destinations/bucket/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,21 @@ export const destination: BrowserDestinationDefinition<Settings, Bucket> = {
},

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@ 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 () {
if (!nock.isDone()) {
// @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(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('trackEvent', () => {
)

expect(getBucketCallLog()).toStrictEqual([
{ method: 'init', args: ['testTrackingKey'] },
{ method: 'init', args: ['testTrackingKey', {}] },
{
method: 'user',
args: ['user-id-1']
Expand Down Expand Up @@ -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', {}] }])
})
})
})

0 comments on commit c2c349b

Please sign in to comment.