From 64ad0f53f494f70584699ceec8fcf6b71f4e7507 Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:35:20 -0600 Subject: [PATCH] allow screen events to have source middleware --- .changeset/hip-cameras-brake.md | 5 +++ .../src/browser/__tests__/integration.test.ts | 43 ++++++++++--------- .../browser/src/plugins/middleware/index.ts | 1 + 3 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 .changeset/hip-cameras-brake.md diff --git a/.changeset/hip-cameras-brake.md b/.changeset/hip-cameras-brake.md new file mode 100644 index 000000000..4cf2bf211 --- /dev/null +++ b/.changeset/hip-cameras-brake.md @@ -0,0 +1,5 @@ +--- +'@segment/analytics-next': patch +--- + +Support addSourceMiddleware for screen events diff --git a/packages/browser/src/browser/__tests__/integration.test.ts b/packages/browser/src/browser/__tests__/integration.test.ts index bfdb46be8..9d99855d1 100644 --- a/packages/browser/src/browser/__tests__/integration.test.ts +++ b/packages/browser/src/browser/__tests__/integration.test.ts @@ -789,28 +789,31 @@ describe('setAnonymousId', () => { }) describe('addSourceMiddleware', () => { - it('supports registering source middlewares', async () => { - const [analytics] = await AnalyticsBrowser.load({ - writeKey, - }) - - await analytics - .addSourceMiddleware(({ next, payload }) => { - payload.obj.context = { - hello: 'from the other side', - } - next(payload) - }) - .catch((err) => { - throw err + it.each(['track', 'screen'] as const)( + 'supports registering source middlewares for %s', + async (type) => { + const [analytics] = await AnalyticsBrowser.load({ + writeKey, }) - const ctx = await analytics.track('Hello!') - - expect(ctx.event.context).toMatchObject({ - hello: 'from the other side', - }) - }) + await analytics + .addSourceMiddleware(({ next, payload }) => { + payload.obj.context = { + hello: 'from the other side', + } + next(payload) + }) + .catch((err) => { + throw err + }) + + const ctx = await analytics[type]('Hello!') + + expect(ctx.event.context).toMatchObject({ + hello: 'from the other side', + }) + } + ) }) describe('addDestinationMiddleware', () => { diff --git a/packages/browser/src/plugins/middleware/index.ts b/packages/browser/src/plugins/middleware/index.ts index 9aa9c65ad..66c2a84f3 100644 --- a/packages/browser/src/plugins/middleware/index.ts +++ b/packages/browser/src/plugins/middleware/index.ts @@ -124,6 +124,7 @@ export function sourceMiddlewarePlugin( track: apply, page: apply, + screen: apply, identify: apply, alias: apply, group: apply,