Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow screen events to have source middleware applied #1213

Merged
merged 1 commit into from
Jan 13, 2025
Merged
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
@@ -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', () => {
1 change: 1 addition & 0 deletions packages/browser/src/plugins/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -124,6 +124,7 @@ export function sourceMiddlewarePlugin(

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