Skip to content

Commit

Permalink
allow screen events to have source middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Jan 13, 2025
1 parent 7f9d5a0 commit 64ad0f5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-cameras-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Support addSourceMiddleware for screen events
43 changes: 23 additions & 20 deletions packages/browser/src/browser/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/plugins/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export function sourceMiddlewarePlugin(

track: apply,
page: apply,
screen: apply,
identify: apply,
alias: apply,
group: apply,
Expand Down

0 comments on commit 64ad0f5

Please sign in to comment.