Skip to content

Commit f090092

Browse files
authored
[algolia-insights] add support for auth user token and product list viewed (#3104)
* fix product viewed typos * add support for Product List Viewed events * add support for authenticatedUsertoken * update objectIDs description * remove gen types * run yarn types * update productIDs mapping * resolve comments * update product list viewed snapshot
1 parent 73f48f1 commit f090092

File tree

24 files changed

+808
-31
lines changed

24 files changed

+808
-31
lines changed

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

Lines changed: 40 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 [
@@ -155,10 +158,47 @@ Object {
155158
}
156159
`;
157160

161+
exports[`Testing snapshot for actions-algolia-insights destination: productListViewedEvents action - all fields 1`] = `
162+
Object {
163+
"events": Array [
164+
Object {
165+
"authenticatedUserToken": "rV6HQ[S7QuqZWEj%HvC",
166+
"eventName": "rV6HQ[S7QuqZWEj%HvC",
167+
"eventType": "click",
168+
"index": "rV6HQ[S7QuqZWEj%HvC",
169+
"objectIDs": Array [
170+
"rV6HQ[S7QuqZWEj%HvC",
171+
],
172+
"queryID": "rV6HQ[S7QuqZWEj%HvC",
173+
"testType": "rV6HQ[S7QuqZWEj%HvC",
174+
"timestamp": null,
175+
"userToken": "rV6HQ[S7QuqZWEj%HvC",
176+
},
177+
],
178+
}
179+
`;
180+
181+
exports[`Testing snapshot for actions-algolia-insights destination: productListViewedEvents action - required fields 1`] = `
182+
Object {
183+
"events": Array [
184+
Object {
185+
"eventName": "Product List Viewed",
186+
"eventType": "view",
187+
"index": "rV6HQ[S7QuqZWEj%HvC",
188+
"objectIDs": Array [
189+
"rV6HQ[S7QuqZWEj%HvC",
190+
],
191+
"userToken": "rV6HQ[S7QuqZWEj%HvC",
192+
},
193+
],
194+
}
195+
`;
196+
158197
exports[`Testing snapshot for actions-algolia-insights destination: productViewedEvents action - all fields 1`] = `
159198
Object {
160199
"events": Array [
161200
Object {
201+
"authenticatedUserToken": "BLFCPcmz",
162202
"eventName": "BLFCPcmz",
163203
"eventType": "view",
164204
"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/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AlgoliaApiPermissions, algoliaApiPermissionsUrl } from './algolia-insig
1111
import { productAddedEvents } from './productAddedEvents'
1212

1313
import { productListFilteredEvents, productListFilteredPresets } from './productListFilteredEvents'
14+
import { productListViewedEvents, productListViewedPresets } from './productListViewedEvents'
1415

1516
export const ALGOLIA_INSIGHTS_USER_AGENT = 'algolia-segment-action-destination: 0.1'
1617

@@ -67,14 +68,16 @@ const destination: DestinationDefinition<Settings> = {
6768
purchasePreset,
6869
addToCartPreset,
6970
productViewedPresets,
70-
productListFilteredPresets
71+
productListFilteredPresets,
72+
productListViewedPresets
7173
],
7274
actions: {
7375
productClickedEvents,
7476
conversionEvents,
7577
productViewedEvents,
7678
productAddedEvents,
77-
productListFilteredEvents
79+
productListFilteredEvents,
80+
productListViewedEvents
7881
}
7982
}
8083

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.

0 commit comments

Comments
 (0)