Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading