Skip to content

Commit 6bfaa3e

Browse files
authored
Fix conflict between window.analytics AND npm library in same scope. (#1127)
1 parent 7aed96e commit 6bfaa3e

File tree

5 files changed

+23
-0
lines changed

5 files changed

+23
-0
lines changed

.changeset/green-moles-provide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
If npm version, do not read buffered events from window.analytics

packages/browser/src/browser/__tests__/analytics-pre-init.integration.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { sleep } from '../../lib/sleep'
77
import { setGlobalCDNUrl } from '../../lib/parse-cdn'
88
import { User } from '../../core/user'
99
import { getBufferedPageCtxFixture } from '../../test-helpers/fixtures'
10+
import { setVersionType } from '../../lib/version-type'
1011

1112
jest.mock('unfetch')
1213

@@ -235,6 +236,9 @@ describe('Pre-initialization', () => {
235236
})
236237

237238
describe('Snippet / standalone', () => {
239+
beforeAll(() => {
240+
setVersionType('web')
241+
})
238242
test('If a snippet user sends multiple events, all of those event gets flushed', async () => {
239243
const onTrackCb = jest.fn()
240244
const onTrack = ['on', 'track', onTrackCb]

packages/browser/src/browser/__tests__/standalone-analytics.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as Factory from '../../test-helpers/factories'
1010
import { EventQueue } from '../../core/queue/event-queue'
1111
import { AnalyticsStandalone } from '../standalone-interface'
1212
import { getBufferedPageCtxFixture } from '../../test-helpers/fixtures'
13+
import { setVersionType } from '../../lib/version-type'
1314

1415
const track = jest.fn()
1516
const identify = jest.fn()
@@ -45,6 +46,7 @@ describe('standalone bundle', () => {
4546
const segmentDotCom = `foo`
4647

4748
beforeEach(async () => {
49+
setVersionType('web')
4850
;(window as any).analytics = undefined
4951
const html = `
5052
<!DOCTYPE html>

packages/browser/src/core/buffer/__tests__/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import { sleep } from '../../../lib/sleep'
1212
import { User } from '../../user'
1313
import { getBufferedPageCtxFixture } from '../../../test-helpers/fixtures'
1414
import * as GlobalAnalytics from '../../../lib/global-analytics-helper'
15+
import { setVersionType } from '../../../lib/version-type'
1516

1617
describe(PreInitMethodCallBuffer, () => {
1718
beforeEach(() => {
19+
setVersionType('npm')
1820
GlobalAnalytics.setGlobalAnalytics(undefined as any)
1921
})
2022

@@ -28,6 +30,7 @@ describe(PreInitMethodCallBuffer, () => {
2830
})
2931

3032
it('should also read from global analytics buffer', () => {
33+
setVersionType('web')
3134
const call1 = new PreInitMethodCall('identify', ['foo'], jest.fn())
3235
;(window as any).analytics = [['track', 'snippet']]
3336

@@ -75,6 +78,7 @@ describe(PreInitMethodCallBuffer, () => {
7578
expect(buffer.getCalls('group')).toEqual([call3])
7679
})
7780
it('should read from Snippet Buffer', () => {
81+
setVersionType('web')
7882
const call1 = new PreInitMethodCall('identify', ['foo'], jest.fn())
7983
GlobalAnalytics.setGlobalAnalytics([['identify', 'snippet']] as any)
8084

@@ -92,6 +96,7 @@ describe(PreInitMethodCallBuffer, () => {
9296
})
9397
describe('clear()', () => {
9498
it('should clear calls', () => {
99+
setVersionType('web')
95100
const call1 = new PreInitMethodCall('identify', [], jest.fn())
96101
const call2 = new PreInitMethodCall('identify', [], jest.fn())
97102
const call3 = new PreInitMethodCall('group', [], jest.fn())
@@ -105,6 +110,7 @@ describe(PreInitMethodCallBuffer, () => {
105110

106111
describe('Snippet buffer (method calls)', () => {
107112
it('should be read from the global analytics instance', () => {
113+
setVersionType('web')
108114
const getGlobalAnalyticsSpy = jest.spyOn(
109115
GlobalAnalytics,
110116
'getGlobalAnalytics'

packages/browser/src/core/buffer/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
createPageContext,
1212
PageContext,
1313
} from '../page'
14+
import { getVersionType } from '../../lib/version-type'
1415

1516
/**
1617
* The names of any AnalyticsBrowser methods that also exist on Analytics
@@ -220,6 +221,11 @@ export class PreInitMethodCallBuffer {
220221
* This removes existing buffered calls from the window object.
221222
*/
222223
private _pushSnippetWindowBuffer(): void {
224+
// if this is the npm version, we don't want to read from the window object.
225+
// This avoids namespace conflicts if there is a seperate analytics library on the page.
226+
if (getVersionType() === 'npm') {
227+
return undefined
228+
}
223229
const wa = getGlobalAnalytics()
224230
if (!Array.isArray(wa)) return undefined
225231
const buffered: SnippetBuffer = wa.splice(0, wa.length)

0 commit comments

Comments
 (0)