Skip to content

[algolia-insights] add support for auth user token and product list viewed #3104

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`Testing snapshot for actions-algolia-insights destination: conversionEv
Object {
"events": Array [
Object {
"authenticatedUserToken": "U[ABpE$k",
"currency": "HTG",
"eventName": "U[ABpE$k",
"eventSubtype": "purchase",
Expand Down Expand Up @@ -86,6 +87,7 @@ exports[`Testing snapshot for actions-algolia-insights destination: productClick
Object {
"events": Array [
Object {
"authenticatedUserToken": "LLjxSD^^GnH",
"eventName": "LLjxSD^^GnH",
"eventType": "conversion",
"index": "LLjxSD^^GnH",
Expand Down Expand Up @@ -124,6 +126,7 @@ exports[`Testing snapshot for actions-algolia-insights destination: productListF
Object {
"events": Array [
Object {
"authenticatedUserToken": "6O0djra",
"eventName": "6O0djra",
"eventType": "view",
"filters": Array [
Expand Down Expand Up @@ -155,10 +158,47 @@ Object {
}
`;

exports[`Testing snapshot for actions-algolia-insights destination: productListViewedEvents action - all fields 1`] = `
Object {
"events": Array [
Object {
"authenticatedUserToken": "rV6HQ[S7QuqZWEj%HvC",
"eventName": "rV6HQ[S7QuqZWEj%HvC",
"eventType": "click",
"index": "rV6HQ[S7QuqZWEj%HvC",
"objectIDs": Array [
"rV6HQ[S7QuqZWEj%HvC",
],
"queryID": "rV6HQ[S7QuqZWEj%HvC",
"testType": "rV6HQ[S7QuqZWEj%HvC",
"timestamp": null,
"userToken": "rV6HQ[S7QuqZWEj%HvC",
},
],
}
`;

exports[`Testing snapshot for actions-algolia-insights destination: productListViewedEvents action - required fields 1`] = `
Object {
"events": Array [
Object {
"eventName": "Product List Viewed",
"eventType": "view",
"index": "rV6HQ[S7QuqZWEj%HvC",
"objectIDs": Array [
"rV6HQ[S7QuqZWEj%HvC",
],
"userToken": "rV6HQ[S7QuqZWEj%HvC",
},
],
}
`;

exports[`Testing snapshot for actions-algolia-insights destination: productViewedEvents action - all fields 1`] = `
Object {
"events": Array [
Object {
"authenticatedUserToken": "BLFCPcmz",
"eventName": "BLFCPcmz",
"eventType": "view",
"index": "BLFCPcmz",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type EventCommon = {
eventName: string
index: string
userToken: string
authenticatedUserToken?: string
timestamp?: number
queryID?: string
eventType: AlgoliaEventType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`Testing snapshot for AlgoliaInsights's conversionEvents destination act
Object {
"events": Array [
Object {
"authenticatedUserToken": ")j)vR5%1AP*epuo8A%R",
"currency": "CUC",
"eventName": ")j)vR5%1AP*epuo8A%R",
"eventSubtype": "addToCart",
Expand Down Expand Up @@ -34,6 +35,7 @@ exports[`Testing snapshot for AlgoliaInsights's conversionEvents destination act
Object {
"events": Array [
Object {
"authenticatedUserToken": "user1234",
"eventName": "Conversion Event",
"eventSubtype": "purchase",
"eventType": "conversion",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ describe('AlgoliaInsights.conversionEvents', () => {
expect(algoliaEvent.eventType).toBe('conversion')
expect(algoliaEvent.eventSubtype).toBe('purchase')
expect(algoliaEvent.index).toBe(event.properties?.search_index)
expect(algoliaEvent.userToken).toBe(event.userId)
expect(algoliaEvent.userToken).toBe(event.anonymousId)
expect(algoliaEvent.authenticatedUserToken).toBe(event.userId)
expect(algoliaEvent.objectIDs).toContain('9876')
expect(algoliaEvent.objectIDs).toContain('5432')
})
Expand Down Expand Up @@ -277,4 +278,73 @@ describe('AlgoliaInsights.conversionEvents', () => {
expect(algoliaEvent.objectData).toBeUndefined()
})
})

it('should pass anonymousId as user token if present', async () => {
const event = createTestEvent({
type: 'track',
event: 'Order Completed',
properties: {
query_id: '1234',
search_index: 'fashion_1',
products: [
{
product_id: '9876'
},
{
product_id: '5432'
}
]
},
anonymousId: 'anon-user-1234'
})
const algoliaEvent = await testAlgoliaDestination(event)
expect(algoliaEvent.userToken).toBe(event.anonymousId)
})

it('should pass userId as user token if anonymousId not present', async () => {
const event = createTestEvent({
type: 'track',
event: 'Order Completed',
properties: {
query_id: '1234',
search_index: 'fashion_1',
products: [
{
product_id: '9876'
},
{
product_id: '5432'
}
]
},
anonymousId: undefined,
userId: 'authed-user-1234'
})
const algoliaEvent = await testAlgoliaDestination(event)
expect(algoliaEvent.userToken).toBe(event.userId)
})

it('should pass userId and anonymousId if present', async () => {
const event = createTestEvent({
type: 'track',
event: 'Order Completed',
properties: {
query_id: '1234',
search_index: 'fashion_1',
products: [
{
product_id: '9876'
},
{
product_id: '5432'
}
]
},
anonymousId: 'anon-user-1234',
userId: 'authed-user-1234'
})
const algoliaEvent = await testAlgoliaDestination(event)
expect(algoliaEvent.userToken).toBe(event.anonymousId)
expect(algoliaEvent.authenticatedUserToken).toBe(event.userId)
})
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,23 @@ const getEventFields = (subtype: AlgoliaEventSubtype = 'purchase'): BaseActionDe
userToken: {
type: 'string',
required: true,
description: 'The ID associated with the user.',
description:
'The ID associated with the user. If a user is authenticated, this should be set to the same value as the Authenticated User Token',
label: 'User Token',
default: {
'@if': {
exists: { '@path': '$.userId' },
then: { '@path': '$.userId' },
else: { '@path': '$.anonymousId' }
exists: { '@path': '$.anonymousId' },
then: { '@path': '$.anonymousId' },
else: { '@path': '$.userId' }
}
}
},
authenticatedUserToken: {
type: 'string',
description: 'The authenticated ID associated with the user.',
label: 'Authenticated User Token',
default: { '@path': '$.userId' }
},
timestamp: {
type: 'string',
required: false,
Expand Down Expand Up @@ -178,6 +185,7 @@ export const conversionEvents: ActionDefinition<Settings, Payload> = {
value: data.payload.value,
currency: data.payload.currency,
userToken: data.payload.userToken,
authenticatedUserToken: data.payload.authenticatedUserToken,
timestamp: data.payload.timestamp ? new Date(data.payload.timestamp).valueOf() : undefined
}
const insightPayload = { events: [insightEvent] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AlgoliaApiPermissions, algoliaApiPermissionsUrl } from './algolia-insig
import { productAddedEvents } from './productAddedEvents'

import { productListFilteredEvents, productListFilteredPresets } from './productListFilteredEvents'
import { productListViewedEvents, productListViewedPresets } from './productListViewedEvents'

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

Expand Down Expand Up @@ -67,14 +68,16 @@ const destination: DestinationDefinition<Settings> = {
purchasePreset,
addToCartPreset,
productViewedPresets,
productListFilteredPresets
productListFilteredPresets,
productListViewedPresets
],
actions: {
productClickedEvents,
conversionEvents,
productViewedEvents,
productAddedEvents,
productListFilteredEvents
productListFilteredEvents,
productListViewedEvents
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`Testing snapshot for AlgoliaInsights's productClickedEvents destination
Object {
"events": Array [
Object {
"authenticatedUserToken": "tTO6#",
"eventName": "tTO6#",
"eventType": "view",
"index": "tTO6#",
Expand All @@ -26,6 +27,7 @@ exports[`Testing snapshot for AlgoliaInsights's productClickedEvents destination
Object {
"events": Array [
Object {
"authenticatedUserToken": "user1234",
"eventName": "Product Clicked",
"eventType": "click",
"index": "tTO6#",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ describe('AlgoliaInsights.productClickedEvents', () => {
expect(algoliaEvent.eventName).toBe('Product Clicked')
expect(algoliaEvent.eventType).toBe('click')
expect(algoliaEvent.index).toBe(event.properties?.search_index)
expect(algoliaEvent.userToken).toBe(event.userId)
expect(algoliaEvent.userToken).toBe(event.anonymousId)
expect(algoliaEvent.authenticatedUserToken).toBe(event.userId)
expect(algoliaEvent.queryID).toBe(event.properties?.query_id)
expect(algoliaEvent.objectIDs).toContain('9876')
expect(algoliaEvent.positions).toContain(5)
Expand Down Expand Up @@ -92,4 +93,55 @@ describe('AlgoliaInsights.productClickedEvents', () => {
const algoliaEvent = await testAlgoliaDestination(event)
expect(algoliaEvent.positions?.[0]).toBe(event.properties?.position)
})

it('should pass anonymousId as user token if present', async () => {
const event = createTestEvent({
type: 'track',
event: 'Product Clicked',
properties: {
query_id: '1234',
search_index: 'fashion_1',
product_id: '9876',
position: 5
},
anonymousId: 'anon-user-1234'
})
const algoliaEvent = await testAlgoliaDestination(event)
expect(algoliaEvent.userToken).toBe(event.anonymousId)
})

it('should pass userId as user token if anonymousId not present', async () => {
const event = createTestEvent({
type: 'track',
event: 'Product Clicked',
properties: {
query_id: '1234',
search_index: 'fashion_1',
product_id: '9876',
position: 5
},
anonymousId: undefined,
userId: 'authed-user-1234'
})
const algoliaEvent = await testAlgoliaDestination(event)
expect(algoliaEvent.userToken).toBe(event.userId)
})

it('should pass userId and anonymousId if present', async () => {
const event = createTestEvent({
type: 'track',
event: 'Product Clicked',
properties: {
query_id: '1234',
search_index: 'fashion_1',
product_id: '9876',
position: 5
},
anonymousId: 'anon-user-1234',
userId: 'authed-user-1234'
})
const algoliaEvent = await testAlgoliaDestination(event)
expect(algoliaEvent.userToken).toBe(event.anonymousId)
expect(algoliaEvent.authenticatedUserToken).toBe(event.userId)
})
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,23 @@ export const productClickedEvents: ActionDefinition<Settings, Payload> = {
userToken: {
type: 'string',
required: true,
description: 'The ID associated with the user.',
description:
'The ID associated with the user. If a user is authenticated, this should be set to the same value as the Authenticated User Token',
label: 'User Token',
default: {
'@if': {
exists: { '@path': '$.userId' },
then: { '@path': '$.userId' },
else: { '@path': '$.anonymousId' }
exists: { '@path': '$.anonymousId' },
then: { '@path': '$.anonymousId' },
else: { '@path': '$.userId' }
}
}
},
authenticatedUserToken: {
type: 'string',
description: 'The authenticated ID associated with the user.',
label: 'Authenticated User Token',
default: { '@path': '$.userId' }
},
timestamp: {
type: 'string',
required: false,
Expand Down Expand Up @@ -108,6 +115,7 @@ export const productClickedEvents: ActionDefinition<Settings, Payload> = {
queryID: data.payload.queryID,
objectIDs: [data.payload.objectID],
userToken: data.payload.userToken,
authenticatedUserToken: data.payload.authenticatedUserToken,
positions: data.payload.position ? [data.payload.position] : undefined,
timestamp: data.payload.timestamp ? new Date(data.payload.timestamp).valueOf() : undefined
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`Testing snapshot for AlgoliaInsights's productListFilteredEvents destin
Object {
"events": Array [
Object {
"authenticatedUserToken": "E625IsTOULbrg8",
"eventName": "E625IsTOULbrg8",
"eventType": "conversion",
"filters": Array [
Expand Down
Loading
Loading