Skip to content

Commit 65e3d52

Browse files
committed
add support for authenticatedUsertoken
1 parent eff5ddd commit 65e3d52

File tree

22 files changed

+378
-33
lines changed

22 files changed

+378
-33
lines changed

packages/destination-actions/src/destinations/algolia-insights/__tests__/__snapshots__/snapshot.test.ts.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`Testing snapshot for actions-algolia-insights destination: conversionEv
44
Object {
55
"events": Array [
66
Object {
7+
"authenticatedUserToken": "U[ABpE$k",
78
"currency": "HTG",
89
"eventName": "U[ABpE$k",
910
"eventSubtype": "purchase",
@@ -86,6 +87,7 @@ exports[`Testing snapshot for actions-algolia-insights destination: productClick
8687
Object {
8788
"events": Array [
8889
Object {
90+
"authenticatedUserToken": "LLjxSD^^GnH",
8991
"eventName": "LLjxSD^^GnH",
9092
"eventType": "conversion",
9193
"index": "LLjxSD^^GnH",
@@ -124,6 +126,7 @@ exports[`Testing snapshot for actions-algolia-insights destination: productListF
124126
Object {
125127
"events": Array [
126128
Object {
129+
"authenticatedUserToken": "6O0djra",
127130
"eventName": "6O0djra",
128131
"eventType": "view",
129132
"filters": Array [
@@ -159,6 +162,7 @@ exports[`Testing snapshot for actions-algolia-insights destination: productListV
159162
Object {
160163
"events": Array [
161164
Object {
165+
"authenticatedUserToken": "rV6HQ[S7QuqZWEj%HvC",
162166
"eventName": "rV6HQ[S7QuqZWEj%HvC",
163167
"eventType": "click",
164168
"index": "rV6HQ[S7QuqZWEj%HvC",
@@ -194,6 +198,7 @@ exports[`Testing snapshot for actions-algolia-insights destination: productViewe
194198
Object {
195199
"events": Array [
196200
Object {
201+
"authenticatedUserToken": "BLFCPcmz",
197202
"eventName": "BLFCPcmz",
198203
"eventType": "view",
199204
"index": "BLFCPcmz",

packages/destination-actions/src/destinations/algolia-insights/algolia-insight-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type EventCommon = {
1212
eventName: string
1313
index: string
1414
userToken: string
15+
authenticatedUserToken?: string
1516
timestamp?: number
1617
queryID?: string
1718
eventType: AlgoliaEventType

packages/destination-actions/src/destinations/algolia-insights/conversionEvents/__tests__/__snapshots__/snapshot.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`Testing snapshot for AlgoliaInsights's conversionEvents destination act
44
Object {
55
"events": Array [
66
Object {
7+
"authenticatedUserToken": ")j)vR5%1AP*epuo8A%R",
78
"currency": "CUC",
89
"eventName": ")j)vR5%1AP*epuo8A%R",
910
"eventSubtype": "addToCart",
@@ -34,6 +35,7 @@ exports[`Testing snapshot for AlgoliaInsights's conversionEvents destination act
3435
Object {
3536
"events": Array [
3637
Object {
38+
"authenticatedUserToken": "user1234",
3739
"eventName": "Conversion Event",
3840
"eventSubtype": "purchase",
3941
"eventType": "conversion",

packages/destination-actions/src/destinations/algolia-insights/conversionEvents/__tests__/index.test.ts

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ describe('AlgoliaInsights.conversionEvents', () => {
5555
expect(algoliaEvent.eventType).toBe('conversion')
5656
expect(algoliaEvent.eventSubtype).toBe('purchase')
5757
expect(algoliaEvent.index).toBe(event.properties?.search_index)
58-
expect(algoliaEvent.userToken).toBe(event.userId)
58+
expect(algoliaEvent.userToken).toBe(event.anonymousId)
59+
expect(algoliaEvent.authenticatedUserToken).toBe(event.userId)
5960
expect(algoliaEvent.objectIDs).toContain('9876')
6061
expect(algoliaEvent.objectIDs).toContain('5432')
6162
})
@@ -277,4 +278,73 @@ describe('AlgoliaInsights.conversionEvents', () => {
277278
expect(algoliaEvent.objectData).toBeUndefined()
278279
})
279280
})
281+
282+
it('should pass anonymousId as user token if present', async () => {
283+
const event = createTestEvent({
284+
type: 'track',
285+
event: 'Order Completed',
286+
properties: {
287+
query_id: '1234',
288+
search_index: 'fashion_1',
289+
products: [
290+
{
291+
product_id: '9876'
292+
},
293+
{
294+
product_id: '5432'
295+
}
296+
]
297+
},
298+
anonymousId: 'anon-user-1234'
299+
})
300+
const algoliaEvent = await testAlgoliaDestination(event)
301+
expect(algoliaEvent.userToken).toBe(event.anonymousId)
302+
})
303+
304+
it('should pass userId as user token if anonymousId not present', async () => {
305+
const event = createTestEvent({
306+
type: 'track',
307+
event: 'Order Completed',
308+
properties: {
309+
query_id: '1234',
310+
search_index: 'fashion_1',
311+
products: [
312+
{
313+
product_id: '9876'
314+
},
315+
{
316+
product_id: '5432'
317+
}
318+
]
319+
},
320+
anonymousId: undefined,
321+
userId: 'authed-user-1234'
322+
})
323+
const algoliaEvent = await testAlgoliaDestination(event)
324+
expect(algoliaEvent.userToken).toBe(event.userId)
325+
})
326+
327+
it('should pass userId and anonymousId if present', async () => {
328+
const event = createTestEvent({
329+
type: 'track',
330+
event: 'Order Completed',
331+
properties: {
332+
query_id: '1234',
333+
search_index: 'fashion_1',
334+
products: [
335+
{
336+
product_id: '9876'
337+
},
338+
{
339+
product_id: '5432'
340+
}
341+
]
342+
},
343+
anonymousId: 'anon-user-1234',
344+
userId: 'authed-user-1234'
345+
})
346+
const algoliaEvent = await testAlgoliaDestination(event)
347+
expect(algoliaEvent.userToken).toBe(event.anonymousId)
348+
expect(algoliaEvent.authenticatedUserToken).toBe(event.userId)
349+
})
280350
})

packages/destination-actions/src/destinations/algolia-insights/conversionEvents/generated-types.ts

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/destination-actions/src/destinations/algolia-insights/conversionEvents/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,23 @@ const getEventFields = (subtype: AlgoliaEventSubtype = 'purchase'): BaseActionDe
8686
userToken: {
8787
type: 'string',
8888
required: true,
89-
description: 'The ID associated with the user.',
89+
description:
90+
'The ID associated with the user. If a user is authenticated, this should be set to the same value as the Authenticated User Token',
9091
label: 'User Token',
9192
default: {
9293
'@if': {
93-
exists: { '@path': '$.userId' },
94-
then: { '@path': '$.userId' },
95-
else: { '@path': '$.anonymousId' }
94+
exists: { '@path': '$.anonymousId' },
95+
then: { '@path': '$.anonymousId' },
96+
else: { '@path': '$.userId' }
9697
}
9798
}
9899
},
100+
authenticatedUserToken: {
101+
type: 'string',
102+
description: 'The authenticated ID associated with the user.',
103+
label: 'Authenticated User Token',
104+
default: { '@path': '$.userId' }
105+
},
99106
timestamp: {
100107
type: 'string',
101108
required: false,
@@ -178,6 +185,7 @@ export const conversionEvents: ActionDefinition<Settings, Payload> = {
178185
value: data.payload.value,
179186
currency: data.payload.currency,
180187
userToken: data.payload.userToken,
188+
authenticatedUserToken: data.payload.authenticatedUserToken,
181189
timestamp: data.payload.timestamp ? new Date(data.payload.timestamp).valueOf() : undefined
182190
}
183191
const insightPayload = { events: [insightEvent] }

packages/destination-actions/src/destinations/algolia-insights/productClickedEvents/__tests__/__snapshots__/snapshot.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`Testing snapshot for AlgoliaInsights's productClickedEvents destination
44
Object {
55
"events": Array [
66
Object {
7+
"authenticatedUserToken": "tTO6#",
78
"eventName": "tTO6#",
89
"eventType": "view",
910
"index": "tTO6#",
@@ -26,6 +27,7 @@ exports[`Testing snapshot for AlgoliaInsights's productClickedEvents destination
2627
Object {
2728
"events": Array [
2829
Object {
30+
"authenticatedUserToken": "user1234",
2931
"eventName": "Product Clicked",
3032
"eventType": "click",
3133
"index": "tTO6#",

packages/destination-actions/src/destinations/algolia-insights/productClickedEvents/__tests__/index.test.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ describe('AlgoliaInsights.productClickedEvents', () => {
4545
expect(algoliaEvent.eventName).toBe('Product Clicked')
4646
expect(algoliaEvent.eventType).toBe('click')
4747
expect(algoliaEvent.index).toBe(event.properties?.search_index)
48-
expect(algoliaEvent.userToken).toBe(event.userId)
48+
expect(algoliaEvent.userToken).toBe(event.anonymousId)
49+
expect(algoliaEvent.authenticatedUserToken).toBe(event.userId)
4950
expect(algoliaEvent.queryID).toBe(event.properties?.query_id)
5051
expect(algoliaEvent.objectIDs).toContain('9876')
5152
expect(algoliaEvent.positions).toContain(5)
@@ -92,4 +93,55 @@ describe('AlgoliaInsights.productClickedEvents', () => {
9293
const algoliaEvent = await testAlgoliaDestination(event)
9394
expect(algoliaEvent.positions?.[0]).toBe(event.properties?.position)
9495
})
96+
97+
it('should pass anonymousId as user token if present', async () => {
98+
const event = createTestEvent({
99+
type: 'track',
100+
event: 'Product Clicked',
101+
properties: {
102+
query_id: '1234',
103+
search_index: 'fashion_1',
104+
product_id: '9876',
105+
position: 5
106+
},
107+
anonymousId: 'anon-user-1234'
108+
})
109+
const algoliaEvent = await testAlgoliaDestination(event)
110+
expect(algoliaEvent.userToken).toBe(event.anonymousId)
111+
})
112+
113+
it('should pass userId as user token if anonymousId not present', async () => {
114+
const event = createTestEvent({
115+
type: 'track',
116+
event: 'Product Clicked',
117+
properties: {
118+
query_id: '1234',
119+
search_index: 'fashion_1',
120+
product_id: '9876',
121+
position: 5
122+
},
123+
anonymousId: undefined,
124+
userId: 'authed-user-1234'
125+
})
126+
const algoliaEvent = await testAlgoliaDestination(event)
127+
expect(algoliaEvent.userToken).toBe(event.userId)
128+
})
129+
130+
it('should pass userId and anonymousId if present', async () => {
131+
const event = createTestEvent({
132+
type: 'track',
133+
event: 'Product Clicked',
134+
properties: {
135+
query_id: '1234',
136+
search_index: 'fashion_1',
137+
product_id: '9876',
138+
position: 5
139+
},
140+
anonymousId: 'anon-user-1234',
141+
userId: 'authed-user-1234'
142+
})
143+
const algoliaEvent = await testAlgoliaDestination(event)
144+
expect(algoliaEvent.userToken).toBe(event.anonymousId)
145+
expect(algoliaEvent.authenticatedUserToken).toBe(event.userId)
146+
})
95147
})

packages/destination-actions/src/destinations/algolia-insights/productClickedEvents/generated-types.ts

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/destination-actions/src/destinations/algolia-insights/productClickedEvents/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,23 @@ export const productClickedEvents: ActionDefinition<Settings, Payload> = {
5151
userToken: {
5252
type: 'string',
5353
required: true,
54-
description: 'The ID associated with the user.',
54+
description:
55+
'The ID associated with the user. If a user is authenticated, this should be set to the same value as the Authenticated User Token',
5556
label: 'User Token',
5657
default: {
5758
'@if': {
58-
exists: { '@path': '$.userId' },
59-
then: { '@path': '$.userId' },
60-
else: { '@path': '$.anonymousId' }
59+
exists: { '@path': '$.anonymousId' },
60+
then: { '@path': '$.anonymousId' },
61+
else: { '@path': '$.userId' }
6162
}
6263
}
6364
},
65+
authenticatedUserToken: {
66+
type: 'string',
67+
description: 'The authenticated ID associated with the user.',
68+
label: 'Authenticated User Token',
69+
default: { '@path': '$.userId' }
70+
},
6471
timestamp: {
6572
type: 'string',
6673
required: false,
@@ -108,6 +115,7 @@ export const productClickedEvents: ActionDefinition<Settings, Payload> = {
108115
queryID: data.payload.queryID,
109116
objectIDs: [data.payload.objectID],
110117
userToken: data.payload.userToken,
118+
authenticatedUserToken: data.payload.authenticatedUserToken,
111119
positions: data.payload.position ? [data.payload.position] : undefined,
112120
timestamp: data.payload.timestamp ? new Date(data.payload.timestamp).valueOf() : undefined
113121
}

0 commit comments

Comments
 (0)