Skip to content
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

feat(insights): annotate events with algoliaSource #1119

Merged
merged 1 commit into from
Apr 12, 2023
Merged
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 @@ -330,6 +330,7 @@ describe('createAlgoliaInsightsPlugin', () => {
eventName: 'Items Viewed',
index: 'index1',
objectIDs: ['1'],
algoliaSource: ['autocomplete', 'autocomplete-internal'],
});
});

Expand Down Expand Up @@ -381,11 +382,13 @@ describe('createAlgoliaInsightsPlugin', () => {
eventName: 'Items Viewed',
index: 'index1',
objectIDs: ['1'],
algoliaSource: ['autocomplete', 'autocomplete-internal'],
});
expect(insightsClient).toHaveBeenNthCalledWith(2, 'viewedObjectIDs', {
eventName: 'Items Viewed',
index: 'index2',
objectIDs: ['2'],
algoliaSource: ['autocomplete', 'autocomplete-internal'],
});
});

Expand Down Expand Up @@ -431,6 +434,7 @@ describe('createAlgoliaInsightsPlugin', () => {
eventName: 'Product Viewed from Autocomplete',
index: 'index1',
objectIDs: ['1'],
algoliaSource: ['autocomplete'],
});
});

Expand Down Expand Up @@ -529,6 +533,7 @@ describe('createAlgoliaInsightsPlugin', () => {
eventName: 'Items Viewed',
index: 'index1',
objectIDs: ['1', '3'],
algoliaSource: ['autocomplete', 'autocomplete-internal'],
});

// The call triggered with "help" occurred after the timeout, so the item
Expand All @@ -537,6 +542,7 @@ describe('createAlgoliaInsightsPlugin', () => {
eventName: 'Items Viewed',
index: 'index1',
objectIDs: ['3'],
algoliaSource: ['autocomplete', 'autocomplete-internal'],
});
});

Expand Down Expand Up @@ -638,6 +644,7 @@ describe('createAlgoliaInsightsPlugin', () => {
objectIDs: ['1'],
positions: [0],
queryID: 'queryID1',
algoliaSource: ['autocomplete', 'autocomplete-internal'],
}
);
});
Expand Down Expand Up @@ -692,6 +699,7 @@ describe('createAlgoliaInsightsPlugin', () => {
objectIDs: ['1'],
positions: [0],
queryID: 'queryID1',
algoliaSource: ['autocomplete'],
}
);
});
Expand Down Expand Up @@ -849,6 +857,7 @@ describe('createAlgoliaInsightsPlugin', () => {
objectIDs: ['1'],
positions: [0],
queryID: 'queryID1',
algoliaSource: ['autocomplete'],
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,26 @@ export function createAlgoliaInsightsPlugin(
function getOptions(options: CreateAlgoliaInsightsPluginParams) {
return {
onItemsChange({ insights, insightsEvents }: OnItemsChangeParams) {
insights.viewedObjectIDs(...insightsEvents);
insights.viewedObjectIDs(
...insightsEvents.map((event) => ({
...event,
algoliaSource: [
...(event.algoliaSource || []),
'autocomplete-internal',
],
}))
);
},
onSelect({ insights, insightsEvents }: OnSelectParams) {
insights.clickedObjectIDsAfterSearch(...insightsEvents);
insights.clickedObjectIDsAfterSearch(
...insightsEvents.map((event) => ({
...event,
algoliaSource: [
...(event.algoliaSource || []),
'autocomplete-internal',
],
}))
);
},
onActive: noop,
...options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ export function createClickedEvent({
}: CreateClickedEventParams): Omit<
ClickedObjectIDsAfterSearchParams,
'eventName'
> {
> & { algoliaSource?: string[] } {
return {
index: item.__autocomplete_indexName,
objectIDs: [item.objectID],
positions: [1 + items.findIndex((x) => x.objectID === item.objectID)],
queryID: item.__autocomplete_queryID,
algoliaSource: ['autocomplete'],
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function createViewedEvents({
return {
index: indexName,
objectIDs,
algoliaSource: ['autocomplete'],
};
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { AlgoliaInsightsHit, AutocompleteInsightsApi } from '.';

export type OnSelectParams = {
insights: AutocompleteInsightsApi;
insightsEvents: ClickedObjectIDsAfterSearchParams[];
insightsEvents: Array<
ClickedObjectIDsAfterSearchParams & { algoliaSource?: string[] }
>;
item: AlgoliaInsightsHit;
state: AutocompleteState<any>;
event: any;
Expand All @@ -19,6 +21,6 @@ export type OnActiveParams = OnSelectParams;

export type OnItemsChangeParams = {
insights: AutocompleteInsightsApi;
insightsEvents: ViewedObjectIDsParams[];
insightsEvents: Array<ViewedObjectIDsParams & { algoliaSource?: string[] }>;
state: AutocompleteState<any>;
};