Skip to content

Commit efb27ce

Browse files
dhayabHaroenv
authored andcommitted
feat(insights): set algolia credentials per event when supported (#1120)
1 parent 43c5312 commit efb27ce

11 files changed

+583
-61
lines changed

bundlesize.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
{
1616
"path": "packages/autocomplete-plugin-algolia-insights/dist/umd/index.production.js",
17-
"maxSize": "2.5 kB"
17+
"maxSize": "3.25 kB"
1818
},
1919
{
2020
"path": "packages/autocomplete-plugin-redirect-url/dist/umd/index.production.js",

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

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,90 @@ describe('createAlgoliaInsightsPlugin', () => {
334334
});
335335
});
336336

337+
test('sends `viewedObjectIDs` events with additional parameters if client supports it', async () => {
338+
const insightsClient = jest.fn();
339+
// @ts-ignore
340+
insightsClient.version = '2.4.0';
341+
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });
342+
343+
const { inputElement } = createPlayground(createAutocomplete, {
344+
plugins: [insightsPlugin],
345+
defaultActiveItemId: 0,
346+
openOnFocus: true,
347+
getSources() {
348+
return [
349+
createSource({
350+
sourceId: 'testSource1',
351+
getItems: () => [
352+
{
353+
label: '1',
354+
objectID: '1',
355+
__autocomplete_algoliaCredentials: {
356+
appId: 'algoliaAppId1',
357+
apiKey: 'algoliaApiKey1',
358+
},
359+
__autocomplete_indexName: 'index1',
360+
__autocomplete_queryID: 'queryID1',
361+
},
362+
],
363+
}),
364+
createSource({
365+
sourceId: 'testSource2',
366+
getItems: () => [
367+
{
368+
label: '2',
369+
objectID: '2',
370+
__autocomplete_algoliaCredentials: {
371+
appId: 'algoliaAppId2',
372+
apiKey: 'algoliaApiKey2',
373+
},
374+
__autocomplete_indexName: 'index2',
375+
__autocomplete_queryID: 'queryID2',
376+
},
377+
],
378+
}),
379+
];
380+
},
381+
});
382+
383+
insightsClient.mockClear();
384+
385+
inputElement.focus();
386+
387+
await runAllMicroTasks();
388+
jest.runAllTimers();
389+
390+
expect(insightsClient).toHaveBeenCalledTimes(2);
391+
expect(insightsClient).toHaveBeenNthCalledWith(
392+
1,
393+
'viewedObjectIDs',
394+
expect.objectContaining({
395+
index: 'index1',
396+
objectIDs: ['1'],
397+
}),
398+
{
399+
headers: {
400+
'X-Algolia-Application-Id': 'algoliaAppId1',
401+
'X-Algolia-API-Key': 'algoliaApiKey1',
402+
},
403+
}
404+
);
405+
expect(insightsClient).toHaveBeenNthCalledWith(
406+
2,
407+
'viewedObjectIDs',
408+
expect.objectContaining({
409+
index: 'index2',
410+
objectIDs: ['2'],
411+
}),
412+
{
413+
headers: {
414+
'X-Algolia-Application-Id': 'algoliaAppId2',
415+
'X-Algolia-API-Key': 'algoliaApiKey2',
416+
},
417+
}
418+
);
419+
});
420+
337421
test('sends as many `viewedObjectIDs` events as there are compatible sources', async () => {
338422
const insightsClient = jest.fn();
339423
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });
@@ -649,6 +733,58 @@ describe('createAlgoliaInsightsPlugin', () => {
649733
);
650734
});
651735

736+
test('sends a `clickedObjectIDsAfterSearch` event with additional parameters if client supports it', async () => {
737+
const insightsClient = jest.fn();
738+
// @ts-ignore
739+
insightsClient.version = '2.4.0';
740+
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });
741+
742+
const { inputElement } = createPlayground(createAutocomplete, {
743+
plugins: [insightsPlugin],
744+
defaultActiveItemId: 0,
745+
openOnFocus: true,
746+
getSources() {
747+
return [
748+
createSource({
749+
getItems: () => [
750+
{
751+
label: '1',
752+
objectID: '1',
753+
__autocomplete_algoliaCredentials: {
754+
appId: 'algoliaAppId',
755+
apiKey: 'algoliaApiKey',
756+
},
757+
__autocomplete_indexName: 'index1',
758+
__autocomplete_queryID: 'queryID1',
759+
},
760+
],
761+
}),
762+
];
763+
},
764+
});
765+
766+
inputElement.focus();
767+
768+
await runAllMicroTasks();
769+
770+
userEvent.type(inputElement, '{enter}');
771+
772+
await runAllMicroTasks();
773+
774+
expect(insightsClient).toHaveBeenCalledWith(
775+
'clickedObjectIDsAfterSearch',
776+
expect.objectContaining({
777+
objectIDs: ['1'],
778+
}),
779+
{
780+
headers: {
781+
'X-Algolia-Application-Id': 'algoliaAppId',
782+
'X-Algolia-API-Key': 'algoliaApiKey',
783+
},
784+
}
785+
);
786+
});
787+
652788
test('sends a custom event', async () => {
653789
const insightsClient = jest.fn();
654790
const insightsPlugin = createAlgoliaInsightsPlugin({
@@ -854,7 +990,14 @@ describe('createAlgoliaInsightsPlugin', () => {
854990
expect(track).toHaveBeenCalledWith('Product Browsed from Autocomplete', {
855991
eventName: 'Item Active',
856992
index: 'index1',
857-
objectIDs: ['1'],
993+
items: [
994+
expect.objectContaining({
995+
label: '1',
996+
objectID: '1',
997+
__autocomplete_indexName: 'index1',
998+
__autocomplete_queryID: 'queryID1',
999+
}),
1000+
],
8581001
positions: [0],
8591002
queryID: 'queryID1',
8601003
algoliaSource: ['autocomplete'],

0 commit comments

Comments
 (0)