Skip to content

Commit d053024

Browse files
authored
fix(insights): position gets computed per source (#1159)
* 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
1 parent 25de61e commit d053024

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

packages/autocomplete-plugin-algolia-insights/src/__tests__/createAlgoliaInsightsPlugin.test.ts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,64 @@ describe('createAlgoliaInsightsPlugin', () => {
726726
eventName: 'Item Selected',
727727
index: 'index1',
728728
objectIDs: ['1'],
729-
positions: [0],
729+
positions: [1],
730+
queryID: 'queryID1',
731+
algoliaSource: ['autocomplete', 'autocomplete-internal'],
732+
}
733+
);
734+
});
735+
736+
test('sends a `clickedObjectIDsAfterSearch` event on non-first source by default', async () => {
737+
const insightsClient = jest.fn();
738+
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });
739+
740+
const { inputElement } = createPlayground(createAutocomplete, {
741+
plugins: [insightsPlugin],
742+
defaultActiveItemId: 0,
743+
openOnFocus: true,
744+
getSources() {
745+
return [
746+
createSource({
747+
sourceId: 'not clicked',
748+
getItems: () => [
749+
{
750+
label: '1',
751+
objectID: '1',
752+
__autocomplete_indexName: 'index0',
753+
__autocomplete_queryID: 'queryID1',
754+
},
755+
],
756+
}),
757+
createSource({
758+
getItems: () => [
759+
{
760+
label: '1',
761+
objectID: '1',
762+
__autocomplete_indexName: 'index1',
763+
__autocomplete_queryID: 'queryID1',
764+
},
765+
],
766+
}),
767+
];
768+
},
769+
});
770+
771+
inputElement.focus();
772+
773+
await runAllMicroTasks();
774+
775+
// select second item
776+
userEvent.type(inputElement, '{arrowdown}{enter}');
777+
778+
await runAllMicroTasks();
779+
780+
expect(insightsClient).toHaveBeenCalledWith(
781+
'clickedObjectIDsAfterSearch',
782+
{
783+
eventName: 'Item Selected',
784+
index: 'index1',
785+
objectIDs: ['1'],
786+
positions: [1],
730787
queryID: 'queryID1',
731788
algoliaSource: ['autocomplete', 'autocomplete-internal'],
732789
}
@@ -833,7 +890,7 @@ describe('createAlgoliaInsightsPlugin', () => {
833890
eventName: 'Product Selected from Autocomplete',
834891
index: 'index1',
835892
objectIDs: ['1'],
836-
positions: [0],
893+
positions: [1],
837894
queryID: 'queryID1',
838895
algoliaSource: ['autocomplete'],
839896
}
@@ -998,7 +1055,7 @@ describe('createAlgoliaInsightsPlugin', () => {
9981055
__autocomplete_queryID: 'queryID1',
9991056
}),
10001057
],
1001-
positions: [0],
1058+
positions: [1],
10021059
queryID: 'queryID1',
10031060
algoliaSource: ['autocomplete'],
10041061
});

packages/autocomplete-plugin-algolia-insights/src/createAlgoliaInsightsPlugin.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
noop,
88
safelyRunOnBrowser,
99
} from '@algolia/autocomplete-shared';
10+
import { AutocompleteReshapeSource } from '@algolia/autocomplete-shared/dist/esm/core';
1011

1112
import { createClickedEvent } from './createClickedEvent';
1213
import { createSearchInsightsApi } from './createSearchInsightsApi';
@@ -166,7 +167,7 @@ export function createAlgoliaInsightsPlugin(
166167
},
167168
});
168169

169-
onSelect(({ item, state, event }) => {
170+
onSelect(({ item, state, event, source }) => {
170171
if (!isAlgoliaInsightsHit(item)) {
171172
return;
172173
}
@@ -179,13 +180,18 @@ export function createAlgoliaInsightsPlugin(
179180
insightsEvents: [
180181
{
181182
eventName: 'Item Selected',
182-
...createClickedEvent({ item, items: previousItems.current }),
183+
...createClickedEvent({
184+
item,
185+
items: (source as AutocompleteReshapeSource<any>)
186+
.getItems()
187+
.filter(isAlgoliaInsightsHit),
188+
}),
183189
},
184190
],
185191
});
186192
});
187193

188-
onActive(({ item, state, event }) => {
194+
onActive(({ item, source, state, event }) => {
189195
if (!isAlgoliaInsightsHit(item)) {
190196
return;
191197
}
@@ -198,7 +204,12 @@ export function createAlgoliaInsightsPlugin(
198204
insightsEvents: [
199205
{
200206
eventName: 'Item Active',
201-
...createClickedEvent({ item, items: previousItems.current }),
207+
...createClickedEvent({
208+
item,
209+
items: (source as AutocompleteReshapeSource<any>)
210+
.getItems()
211+
.filter(isAlgoliaInsightsHit),
212+
}),
202213
},
203214
],
204215
});

packages/autocomplete-plugin-algolia-insights/src/createClickedEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type CreateClickedEventParams = {
1111

1212
export function createClickedEvent({
1313
item,
14-
items,
14+
items = [],
1515
}: CreateClickedEventParams): Omit<
1616
InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>,
1717
'eventName'

0 commit comments

Comments
 (0)