Skip to content

Commit 94e4722

Browse files
committed
wip
1 parent b1d61e6 commit 94e4722

File tree

3 files changed

+119
-19
lines changed

3 files changed

+119
-19
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Signal } from '@segment/analytics-signals-runtime'
2+
import { User } from '../../../types'
3+
import { SignalsMiddleware, SignalsMiddlewareContext } from '../../emitter'
4+
5+
export class UserInfoMiddleware implements SignalsMiddleware {
6+
ctx!: SignalsMiddlewareContext
7+
user!: User
8+
9+
async load(ctx: SignalsMiddlewareContext) {
10+
this.user = await ctx.analyticsInstance.user()
11+
}
12+
13+
process(signal: Signal): Signal {
14+
signal.anonymousId = this.user.anonymousId()
15+
return signal
16+
}
17+
}

packages/signals/signals/src/core/signal-generators/dom-gen/__tests__/change-gen.test.ts

Lines changed: 96 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { createInteractionSignal } from '../../../../types/factories'
21
import { SignalEmitter } from '../../../emitter'
32
import { OnChangeGenerator } from '../change-gen'
43

5-
describe('OnChangeGenerator', () => {
4+
describe(OnChangeGenerator, () => {
65
let onChangeGenerator: OnChangeGenerator
76
let emitter: SignalEmitter
87
let unregister: () => void
@@ -28,14 +27,49 @@ describe('OnChangeGenerator', () => {
2827
unregister = onChangeGenerator.register(emitter)
2928
document.dispatchEvent(event)
3029

31-
expect(emitSpy).toHaveBeenCalledWith(
32-
createInteractionSignal({
33-
eventType: 'change',
34-
listener: 'onchange',
35-
target: expect.any(Object),
36-
change: { value: 'new value' },
37-
})
38-
)
30+
expect(emitSpy.mock.calls.length).toBe(1)
31+
expect(emitSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
32+
{
33+
"anonymousId": "",
34+
"data": {
35+
"change": {
36+
"value": "new value",
37+
},
38+
"eventType": "change",
39+
"listener": "onchange",
40+
"page": {
41+
"hash": "",
42+
"hostname": "localhost",
43+
"path": "/",
44+
"referrer": "",
45+
"search": "",
46+
"title": "",
47+
"url": "http://localhost/",
48+
},
49+
"target": {
50+
"attributes": {
51+
"type": "text",
52+
},
53+
"checked": false,
54+
"classList": [],
55+
"describedBy": undefined,
56+
"id": "",
57+
"innerText": undefined,
58+
"label": undefined,
59+
"labels": [],
60+
"name": "",
61+
"nodeName": "INPUT",
62+
"tagName": "INPUT",
63+
"textContent": "",
64+
"title": "",
65+
"type": "text",
66+
"value": "new value",
67+
},
68+
},
69+
"timestamp": <ISO Timestamp>,
70+
"type": "interaction",
71+
}
72+
`)
3973
})
4074

4175
it('should not emit a signal for ignored elements', () => {
@@ -85,15 +119,58 @@ describe('OnChangeGenerator', () => {
85119
unregister = onChangeGenerator.register(emitter)
86120
document.dispatchEvent(event)
87121

88-
expect(emitSpy).toHaveBeenCalledWith(
89-
createInteractionSignal({
90-
eventType: 'change',
91-
listener: 'onchange',
92-
target: expect.any(Object),
93-
change: {
94-
selectedOptions: [{ value: 'value1', label: 'label1' }],
122+
expect(emitSpy.mock.lastCall).toMatchInlineSnapshot(`
123+
[
124+
{
125+
"anonymousId": "",
126+
"data": {
127+
"change": {
128+
"selectedOptions": [
129+
{
130+
"label": "label1",
131+
"value": "value1",
132+
},
133+
],
134+
},
135+
"eventType": "change",
136+
"listener": "onchange",
137+
"page": {
138+
"hash": "",
139+
"hostname": "localhost",
140+
"path": "/",
141+
"referrer": "",
142+
"search": "",
143+
"title": "",
144+
"url": "http://localhost/",
145+
},
146+
"target": {
147+
"attributes": {},
148+
"classList": [],
149+
"describedBy": undefined,
150+
"id": "",
151+
"innerText": undefined,
152+
"label": undefined,
153+
"labels": [],
154+
"name": "",
155+
"nodeName": "SELECT",
156+
"selectedIndex": 0,
157+
"selectedOptions": [
158+
{
159+
"label": "label1",
160+
"value": "value1",
161+
},
162+
],
163+
"tagName": "SELECT",
164+
"textContent": "",
165+
"title": "",
166+
"type": "select-one",
167+
"value": "value1",
168+
},
169+
},
170+
"timestamp": <ISO Timestamp>,
171+
"type": "interaction",
95172
},
96-
})
97-
)
173+
]
174+
`)
98175
})
99176
})

packages/signals/signals/src/types/analytics-api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ export interface DestinationMiddlewareParams {
5050
integration: string
5151
}
5252

53+
export interface User {
54+
id(): string
55+
anonymousId(): string
56+
}
57+
5358
export interface AnyAnalytics {
5459
settings: {
5560
cdnSettings: CDNSettings
@@ -67,6 +72,7 @@ export interface AnyAnalytics {
6772
alias(...args: any[]): void
6873
screen(...args: any[]): void
6974
reset(): void
75+
user(): User | Promise<User>
7076
on(name: 'reset', fn: (...args: any[]) => void): void
7177
}
7278

0 commit comments

Comments
 (0)