Skip to content

Commit

Permalink
- add better search
Browse files Browse the repository at this point in the history
- fix console warnings in UniversalStorage tests
  • Loading branch information
silesky committed Sep 22, 2023
1 parent 0a8c30a commit 519fae6
Show file tree
Hide file tree
Showing 31 changed files with 1,442 additions and 862 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-crabs-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': minor
---

Fix stale page context information for buffered events, so page data is resilient to quick navigation changes. Remove page enrichment plugin. Fix bug in track event where properties like `{ title: 'foo', url: 'bar' }` would override context.page.title and context.page.url.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { sleep } from '@segment/analytics-core'
import { bufferedPageCtxFixture } from '../../test-helpers/fixtures'
import unfetch from 'unfetch'
import { AnalyticsBrowser } from '..'
import { Analytics } from '../../core/analytics'
Expand Down Expand Up @@ -27,7 +28,7 @@ describe('Lazy initialization', () => {
expect(trackSpy).not.toBeCalled()
analytics.load({ writeKey: 'abc' })
await track
expect(trackSpy).toBeCalledWith('foo')
expect(trackSpy).toBeCalledWith('foo', bufferedPageCtxFixture)
})

it('.load method return an analytics instance', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Factory from '../../test-helpers/factories'
import { sleep } from '../../lib/sleep'
import { setGlobalCDNUrl } from '../../lib/parse-cdn'
import { User } from '../../core/user'
import { bufferedPageCtxFixture } from '../../test-helpers/fixtures'

jest.mock('unfetch')

Expand Down Expand Up @@ -61,7 +62,11 @@ describe('Pre-initialization', () => {
const trackCtxPromise = ajsBrowser.track('foo', { name: 'john' })
const result = await trackCtxPromise
expect(result).toBeInstanceOf(Context)
expect(trackSpy).toBeCalledWith('foo', { name: 'john' })
expect(trackSpy).toBeCalledWith(
'foo',
{ name: 'john' },
bufferedPageCtxFixture
)
expect(trackSpy).toBeCalledTimes(1)
})

Expand Down Expand Up @@ -107,11 +112,19 @@ describe('Pre-initialization', () => {

await Promise.all([trackCtxPromise, trackCtxPromise2, identifyCtxPromise])

expect(trackSpy).toBeCalledWith('foo', { name: 'john' })
expect(trackSpy).toBeCalledWith('bar', { age: 123 })
expect(trackSpy).toBeCalledWith(
'foo',
{ name: 'john' },
bufferedPageCtxFixture
)
expect(trackSpy).toBeCalledWith(
'bar',
{ age: 123 },
bufferedPageCtxFixture
)
expect(trackSpy).toBeCalledTimes(2)

expect(identifySpy).toBeCalledWith('hello')
expect(identifySpy).toBeCalledWith('hello', bufferedPageCtxFixture)
expect(identifySpy).toBeCalledTimes(1)
})

Expand Down Expand Up @@ -234,8 +247,8 @@ describe('Pre-initialization', () => {
await AnalyticsBrowser.standalone(writeKey)

await sleep(100) // the snippet does not return a promise (pre-initialization) ... it sometimes has a callback as the third argument.
expect(trackSpy).toBeCalledWith('foo')
expect(trackSpy).toBeCalledWith('bar')
expect(trackSpy).toBeCalledWith('foo', bufferedPageCtxFixture)
expect(trackSpy).toBeCalledWith('bar', bufferedPageCtxFixture)
expect(trackSpy).toBeCalledTimes(2)

expect(identifySpy).toBeCalledTimes(1)
Expand All @@ -262,11 +275,11 @@ describe('Pre-initialization', () => {
await AnalyticsBrowser.standalone(writeKey)

await sleep(100) // the snippet does not return a promise (pre-initialization) ... it sometimes has a callback as the third argument.
expect(trackSpy).toBeCalledWith('foo')
expect(trackSpy).toBeCalledWith('bar')
expect(trackSpy).toBeCalledWith('foo', bufferedPageCtxFixture)
expect(trackSpy).toBeCalledWith('bar', bufferedPageCtxFixture)
expect(trackSpy).toBeCalledTimes(2)

expect(identifySpy).toBeCalledWith()
expect(identifySpy).toBeCalledWith(bufferedPageCtxFixture)
expect(identifySpy).toBeCalledTimes(1)
expect(consoleErrorSpy).toBeCalledTimes(1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('Inspector', () => {

await deliveryPromise

expect(enrichedFn).toHaveBeenCalledTimes(2)
expect(enrichedFn).toHaveBeenCalledTimes(1) // will be called once for every before or enrichment plugin.
expect(deliveredFn).toHaveBeenCalledTimes(1)
})

Expand Down
1 change: 0 additions & 1 deletion packages/browser/src/browser/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,6 @@ describe('Dispatch', () => {
"message_dispatched",
"plugin_time",
"plugin_time",
"plugin_time",
"message_delivered",
"plugin_time",
"delivered",
Expand Down
52 changes: 36 additions & 16 deletions packages/browser/src/browser/__tests__/standalone-analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { sleep } from '../../lib/sleep'
import * as Factory from '../../test-helpers/factories'
import { EventQueue } from '../../core/queue/event-queue'
import { AnalyticsStandalone } from '../standalone-interface'
import { bufferedPageCtxFixture } from '../../test-helpers/fixtures'

const track = jest.fn()
const identify = jest.fn()
Expand All @@ -28,9 +29,7 @@ jest.mock('../../core/analytics', () => ({
register,
emit: jest.fn(),
on,
queue: new EventQueue(
new PersistedPriorityQueue(1, 'foo:event-queue') as any
),
queue: new EventQueue(new PersistedPriorityQueue(1, 'event-queue') as any),
options,
}),
}))
Expand Down Expand Up @@ -68,6 +67,7 @@ describe('standalone bundle', () => {
`.trim()

const virtualConsole = new jsdom.VirtualConsole()

const jsd = new JSDOM(html, {
runScripts: 'dangerously',
resources: 'usable',
Expand Down Expand Up @@ -159,12 +159,20 @@ describe('standalone bundle', () => {

await sleep(0)

expect(track).toHaveBeenCalledWith('fruit basket', {
fruits: ['🍌', '🍇'],
})
expect(identify).toHaveBeenCalledWith('netto', {
employer: 'segment',
})
expect(track).toHaveBeenCalledWith(
'fruit basket',
{
fruits: ['🍌', '🍇'],
},
bufferedPageCtxFixture
)
expect(identify).toHaveBeenCalledWith(
'netto',
{
employer: 'segment',
},
bufferedPageCtxFixture
)

expect(page).toHaveBeenCalled()
})
Expand Down Expand Up @@ -270,13 +278,25 @@ describe('standalone bundle', () => {

await sleep(0)

expect(track).toHaveBeenCalledWith('fruit basket', {
fruits: ['🍌', '🍇'],
})
expect(track).toHaveBeenCalledWith('race conditions', { foo: 'bar' })
expect(identify).toHaveBeenCalledWith('netto', {
employer: 'segment',
})
expect(track).toHaveBeenCalledWith(
'fruit basket',
{
fruits: ['🍌', '🍇'],
},
{ ...bufferedPageCtxFixture }
)
expect(track).toHaveBeenCalledWith(
'race conditions',
{ foo: 'bar' },
{ ...bufferedPageCtxFixture }
)
expect(identify).toHaveBeenCalledWith(
'netto',
{
employer: 'segment',
},
{ ...bufferedPageCtxFixture }
)

expect(page).toHaveBeenCalled()
})
Expand Down
7 changes: 1 addition & 6 deletions packages/browser/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Plugin } from '../core/plugin'
import { MetricsOptions } from '../core/stats/remote-metrics'
import { mergedOptions } from '../lib/merged-options'
import { createDeferred } from '../lib/create-deferred'
import { pageEnrichment } from '../plugins/page-enrichment'

import {
PluginFactory,
remoteLoader,
Expand All @@ -26,7 +26,6 @@ import {
flushSetAnonymousID,
flushOn,
} from '../core/buffer'
import { popSnippetWindowBuffer } from '../core/buffer/snippet'
import { ClassicIntegrationSource } from '../plugins/ajs-destination/types'
import { attachInspector } from '../core/inspector'
import { Stats } from '../core/stats'
Expand Down Expand Up @@ -155,7 +154,6 @@ function flushPreBuffer(
analytics: Analytics,
buffer: PreInitMethodCallBuffer
): void {
buffer.push(...popSnippetWindowBuffer())
flushSetAnonymousID(analytics, buffer)
flushOn(analytics, buffer)
}
Expand All @@ -169,9 +167,7 @@ async function flushFinalBuffer(
): Promise<void> {
// Call popSnippetWindowBuffer before each flush task since there may be
// analytics calls during async function calls.
buffer.push(...popSnippetWindowBuffer())
await flushAddSourceMiddleware(analytics, buffer)
buffer.push(...popSnippetWindowBuffer())
flushAnalyticsCallsInNewTask(analytics, buffer)
// Clear buffer, just in case analytics is loaded twice; we don't want to fire events off again.
buffer.clear()
Expand Down Expand Up @@ -250,7 +246,6 @@ async function registerPlugins(

const toRegister = [
validation,
pageEnrichment,
...plugins,
...legacyDestinations,
...remotePlugins,
Expand Down
Loading

0 comments on commit 519fae6

Please sign in to comment.