From 41645f8f470411853c551e854cffd1640c7dfce5 Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Thu, 13 Jul 2023 10:42:39 -0600 Subject: [PATCH 1/6] Fix query bug --- packages/browser/src/plugins/segmentio/normalize.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/browser/src/plugins/segmentio/normalize.ts b/packages/browser/src/plugins/segmentio/normalize.ts index eb1b6ceed..e5732ddd4 100644 --- a/packages/browser/src/plugins/segmentio/normalize.ts +++ b/packages/browser/src/plugins/segmentio/normalize.ts @@ -131,7 +131,7 @@ export function normalize( const ctx = json.context // This guard against missing ctx.page should not be neccessary, since context.page is always defined - const query: string = ctx.page?.search || '' + const query: string | any = ctx.page?.search || '' delete json.options json.writeKey = settings?.apiKey @@ -160,7 +160,7 @@ export function normalize( } } - if (query && !ctx.campaign) { + if (query && typeof query === 'string' && !ctx.campaign) { ctx.campaign = utm(query) } From 38ed4566d013bced0690d2235af689b7f7368aaf Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Thu, 13 Jul 2023 10:57:35 -0600 Subject: [PATCH 2/6] changeset --- .changeset/neat-emus-call.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/neat-emus-call.md diff --git a/.changeset/neat-emus-call.md b/.changeset/neat-emus-call.md new file mode 100644 index 000000000..e1f4da6b4 --- /dev/null +++ b/.changeset/neat-emus-call.md @@ -0,0 +1,5 @@ +--- +'@segment/analytics-next': patch +--- + +Fix query string parsing bug that was causing events containing the 'search' property with a non string value to be dropped From 9af8b89261818c2987d9ff6026bfe4b9adcea123 Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Thu, 13 Jul 2023 11:05:30 -0600 Subject: [PATCH 3/6] make sure query is string --- packages/browser/src/plugins/segmentio/normalize.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/browser/src/plugins/segmentio/normalize.ts b/packages/browser/src/plugins/segmentio/normalize.ts index e5732ddd4..61979958c 100644 --- a/packages/browser/src/plugins/segmentio/normalize.ts +++ b/packages/browser/src/plugins/segmentio/normalize.ts @@ -131,7 +131,8 @@ export function normalize( const ctx = json.context // This guard against missing ctx.page should not be neccessary, since context.page is always defined - const query: string | any = ctx.page?.search || '' + const query: string = + typeof ctx.page?.search === 'string' ? ctx.page?.search : '' delete json.options json.writeKey = settings?.apiKey @@ -160,7 +161,7 @@ export function normalize( } } - if (query && typeof query === 'string' && !ctx.campaign) { + if (query && !ctx.campaign) { ctx.campaign = utm(query) } From f7a4f778a4e4b054ed16dbba8379e83ca0abf303 Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Tue, 18 Jul 2023 11:03:16 -0600 Subject: [PATCH 4/6] Dont override non page calls context.page --- .../page-enrichment/__tests__/index.test.ts | 24 +++++++++++++++---- .../src/plugins/page-enrichment/index.ts | 21 +++++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/packages/browser/src/plugins/page-enrichment/__tests__/index.test.ts b/packages/browser/src/plugins/page-enrichment/__tests__/index.test.ts index 584f61243..21cc6bf23 100644 --- a/packages/browser/src/plugins/page-enrichment/__tests__/index.test.ts +++ b/packages/browser/src/plugins/page-enrichment/__tests__/index.test.ts @@ -60,23 +60,39 @@ describe('Page Enrichment', () => { test('special page properties in event.properties (url, referrer, etc) are copied to context.page', async () => { const eventProps = { ...helpers.pageProps } ;(eventProps as any)['should_not_show_up'] = 'hello' - const ctx = await ajs.track('My Event', eventProps) + const ctx = await ajs.page('My Event', eventProps) const page = ctx.event.context!.page expect(page).toEqual( pick(eventProps, ['url', 'path', 'referrer', 'search', 'title']) ) }) - test('event page properties should not be mutated', async () => { + test('special page properties in event.properties (url, referrer, etc) are not copied to context.page in non-page calls', async () => { const eventProps = { ...helpers.pageProps } + ;(eventProps as any)['should_not_show_up'] = 'hello' const ctx = await ajs.track('My Event', eventProps) const page = ctx.event.context!.page + expect(page).toMatchInlineSnapshot(` + Object { + "path": "/", + "referrer": "", + "search": "", + "title": "", + "url": "http://localhost/", + } + `) + }) + + test('event page properties should not be mutated', async () => { + const eventProps = { ...helpers.pageProps } + const ctx = await ajs.page('My Event', eventProps) + const page = ctx.event.context!.page expect(page).toEqual(eventProps) }) test('page properties should have defaults', async () => { const eventProps = pick(helpers.pageProps, ['path', 'referrer']) - const ctx = await ajs.track('My Event', eventProps) + const ctx = await ajs.page('My Event', eventProps) const page = ctx.event.context!.page expect(page).toEqual({ ...eventProps, @@ -91,7 +107,7 @@ describe('Page Enrichment', () => { eventProps.referrer = '' eventProps.path = undefined as any eventProps.title = null as any - const ctx = await ajs.track('My Event', eventProps) + const ctx = await ajs.page('My Event', eventProps) const page = ctx.event.context!.page expect(page).toEqual( expect.objectContaining({ referrer: '', path: undefined, title: null }) diff --git a/packages/browser/src/plugins/page-enrichment/index.ts b/packages/browser/src/plugins/page-enrichment/index.ts index 6a1410057..7984d20a2 100644 --- a/packages/browser/src/plugins/page-enrichment/index.ts +++ b/packages/browser/src/plugins/page-enrichment/index.ts @@ -1,6 +1,7 @@ import { pick } from '../../lib/pick' import type { Context } from '../../core/context' import type { Plugin } from '../../core/plugin' +import { EventProperties } from '@segment/analytics-core' interface PageDefault { [key: string]: unknown @@ -75,16 +76,14 @@ function enrichPageContext(ctx: Context): Context { const defaultPageContext = pageDefaults() - const pageContextFromEventProps = - event.properties && pick(event.properties, Object.keys(defaultPageContext)) - - event.context.page = { - ...defaultPageContext, - ...pageContextFromEventProps, - ...event.context.page, - } + let pageContextFromEventProps: Pick | undefined = + undefined if (event.type === 'page') { + pageContextFromEventProps = + event.properties && + pick(event.properties, Object.keys(defaultPageContext)) + event.properties = { ...defaultPageContext, ...event.properties, @@ -92,6 +91,12 @@ function enrichPageContext(ctx: Context): Context { } } + event.context.page = { + ...defaultPageContext, + ...pageContextFromEventProps, + ...event.context.page, + } + return ctx } From eeb5577416c689746a794857321ba1a52c540083 Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Wed, 19 Jul 2023 13:50:04 -0600 Subject: [PATCH 5/6] add override unit test --- .../plugins/segmentio/__tests__/normalize.test.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/browser/src/plugins/segmentio/__tests__/normalize.test.ts b/packages/browser/src/plugins/segmentio/__tests__/normalize.test.ts index 9f4835bdd..afdeba2dd 100644 --- a/packages/browser/src/plugins/segmentio/__tests__/normalize.test.ts +++ b/packages/browser/src/plugins/segmentio/__tests__/normalize.test.ts @@ -55,7 +55,7 @@ describe('before loading', () => { describe('#normalize', () => { let object: SegmentEvent let defaultCtx: any - const withSearchParams = (search?: string) => { + const withSearchParams = (search?: any) => { object.context = { page: { search } } } @@ -285,6 +285,17 @@ describe('before loading', () => { assert(obj.context.campaign.name === 'overrideName') }) + it('should allow override of .search with object', () => { + withSearchParams({ + someObject: 'foo', + }) + + normalize(analytics, object, options, {}) + assert(object) + assert(object.context) + assert(object.context.campaign === undefined) + }) + it('should add .referrer.id and .referrer.type (cookies)', () => { withSearchParams('?utm_source=source&urid=medium') From d4563020c295d7f5704eec6d2c530c1b40f66e47 Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Wed, 19 Jul 2023 13:52:24 -0600 Subject: [PATCH 6/6] update --- .../browser/src/plugins/segmentio/__tests__/normalize.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/browser/src/plugins/segmentio/__tests__/normalize.test.ts b/packages/browser/src/plugins/segmentio/__tests__/normalize.test.ts index afdeba2dd..5147ae850 100644 --- a/packages/browser/src/plugins/segmentio/__tests__/normalize.test.ts +++ b/packages/browser/src/plugins/segmentio/__tests__/normalize.test.ts @@ -294,6 +294,7 @@ describe('before loading', () => { assert(object) assert(object.context) assert(object.context.campaign === undefined) + assert(object.context.referrer === undefined) }) it('should add .referrer.id and .referrer.type (cookies)', () => {