Skip to content

Commit

Permalink
fix(insights): position gets computed per source (#1159)
Browse files Browse the repository at this point in the history
* fix(insights): position gets computed per source

The insights "position" parameter refers to the absolute position of the item, within its set of hits. Using the entire set of autocomplete items is thus wrong.

* rename a bit

* Apply suggestions from code review

* simplify

* HUH why was the position 0 before? that's wrong, no?

IRL i don't see any difference, it always was 1-based
  • Loading branch information
Haroenv authored Jun 20, 2023
1 parent 25de61e commit d053024
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,64 @@ describe('createAlgoliaInsightsPlugin', () => {
eventName: 'Item Selected',
index: 'index1',
objectIDs: ['1'],
positions: [0],
positions: [1],
queryID: 'queryID1',
algoliaSource: ['autocomplete', 'autocomplete-internal'],
}
);
});

test('sends a `clickedObjectIDsAfterSearch` event on non-first source by default', async () => {
const insightsClient = jest.fn();
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });

const { inputElement } = createPlayground(createAutocomplete, {
plugins: [insightsPlugin],
defaultActiveItemId: 0,
openOnFocus: true,
getSources() {
return [
createSource({
sourceId: 'not clicked',
getItems: () => [
{
label: '1',
objectID: '1',
__autocomplete_indexName: 'index0',
__autocomplete_queryID: 'queryID1',
},
],
}),
createSource({
getItems: () => [
{
label: '1',
objectID: '1',
__autocomplete_indexName: 'index1',
__autocomplete_queryID: 'queryID1',
},
],
}),
];
},
});

inputElement.focus();

await runAllMicroTasks();

// select second item
userEvent.type(inputElement, '{arrowdown}{enter}');

await runAllMicroTasks();

expect(insightsClient).toHaveBeenCalledWith(
'clickedObjectIDsAfterSearch',
{
eventName: 'Item Selected',
index: 'index1',
objectIDs: ['1'],
positions: [1],
queryID: 'queryID1',
algoliaSource: ['autocomplete', 'autocomplete-internal'],
}
Expand Down Expand Up @@ -833,7 +890,7 @@ describe('createAlgoliaInsightsPlugin', () => {
eventName: 'Product Selected from Autocomplete',
index: 'index1',
objectIDs: ['1'],
positions: [0],
positions: [1],
queryID: 'queryID1',
algoliaSource: ['autocomplete'],
}
Expand Down Expand Up @@ -998,7 +1055,7 @@ describe('createAlgoliaInsightsPlugin', () => {
__autocomplete_queryID: 'queryID1',
}),
],
positions: [0],
positions: [1],
queryID: 'queryID1',
algoliaSource: ['autocomplete'],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
noop,
safelyRunOnBrowser,
} from '@algolia/autocomplete-shared';
import { AutocompleteReshapeSource } from '@algolia/autocomplete-shared/dist/esm/core';

import { createClickedEvent } from './createClickedEvent';
import { createSearchInsightsApi } from './createSearchInsightsApi';
Expand Down Expand Up @@ -166,7 +167,7 @@ export function createAlgoliaInsightsPlugin(
},
});

onSelect(({ item, state, event }) => {
onSelect(({ item, state, event, source }) => {
if (!isAlgoliaInsightsHit(item)) {
return;
}
Expand All @@ -179,13 +180,18 @@ export function createAlgoliaInsightsPlugin(
insightsEvents: [
{
eventName: 'Item Selected',
...createClickedEvent({ item, items: previousItems.current }),
...createClickedEvent({
item,
items: (source as AutocompleteReshapeSource<any>)
.getItems()
.filter(isAlgoliaInsightsHit),
}),
},
],
});
});

onActive(({ item, state, event }) => {
onActive(({ item, source, state, event }) => {
if (!isAlgoliaInsightsHit(item)) {
return;
}
Expand All @@ -198,7 +204,12 @@ export function createAlgoliaInsightsPlugin(
insightsEvents: [
{
eventName: 'Item Active',
...createClickedEvent({ item, items: previousItems.current }),
...createClickedEvent({
item,
items: (source as AutocompleteReshapeSource<any>)
.getItems()
.filter(isAlgoliaInsightsHit),
}),
},
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type CreateClickedEventParams = {

export function createClickedEvent({
item,
items,
items = [],
}: CreateClickedEventParams): Omit<
InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>,
'eventName'
Expand Down

0 comments on commit d053024

Please sign in to comment.