Skip to content

Commit 9d1b042

Browse files
authored
Allow screen events to have source middleware applied (#1213)
1 parent 7f9d5a0 commit 9d1b042

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

.changeset/hip-cameras-brake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
Support addSourceMiddleware for screen events

packages/browser/src/browser/__tests__/integration.test.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -789,28 +789,31 @@ describe('setAnonymousId', () => {
789789
})
790790

791791
describe('addSourceMiddleware', () => {
792-
it('supports registering source middlewares', async () => {
793-
const [analytics] = await AnalyticsBrowser.load({
794-
writeKey,
795-
})
796-
797-
await analytics
798-
.addSourceMiddleware(({ next, payload }) => {
799-
payload.obj.context = {
800-
hello: 'from the other side',
801-
}
802-
next(payload)
803-
})
804-
.catch((err) => {
805-
throw err
792+
it.each(['track', 'screen'] as const)(
793+
'supports registering source middlewares for %s',
794+
async (type) => {
795+
const [analytics] = await AnalyticsBrowser.load({
796+
writeKey,
806797
})
807798

808-
const ctx = await analytics.track('Hello!')
809-
810-
expect(ctx.event.context).toMatchObject({
811-
hello: 'from the other side',
812-
})
813-
})
799+
await analytics
800+
.addSourceMiddleware(({ next, payload }) => {
801+
payload.obj.context = {
802+
hello: 'from the other side',
803+
}
804+
next(payload)
805+
})
806+
.catch((err) => {
807+
throw err
808+
})
809+
810+
const ctx = await analytics[type]('Hello!')
811+
812+
expect(ctx.event.context).toMatchObject({
813+
hello: 'from the other side',
814+
})
815+
}
816+
)
814817
})
815818

816819
describe('addDestinationMiddleware', () => {

packages/browser/src/plugins/middleware/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export function sourceMiddlewarePlugin(
124124

125125
track: apply,
126126
page: apply,
127+
screen: apply,
127128
identify: apply,
128129
alias: apply,
129130
group: apply,

0 commit comments

Comments
 (0)