Skip to content

Commit 60f8ae4

Browse files
authored
fix(insights): add Algolia agent on subscribe (#1058)
1 parent d7d2842 commit 60f8ae4

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

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

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ describe('createAlgoliaInsightsPlugin', () => {
6464
);
6565
});
6666

67+
test('sets a user agent on the Insights client on subscribe', () => {
68+
const insightsClient = jest.fn();
69+
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });
70+
71+
expect(insightsClient).not.toHaveBeenCalled();
72+
73+
createPlayground(createAutocomplete, { plugins: [insightsPlugin] });
74+
75+
expect(insightsClient).toHaveBeenCalledTimes(1);
76+
expect(insightsClient).toHaveBeenCalledWith(
77+
'addAlgoliaAgent',
78+
'insights-plugin'
79+
);
80+
});
81+
6782
describe('onItemsChange', () => {
6883
test('sends a `viewedObjectIDs` event by default', async () => {
6984
const insightsClient = jest.fn();
@@ -180,7 +195,10 @@ describe('createAlgoliaInsightsPlugin', () => {
180195
await runAllMicroTasks();
181196
jest.runAllTimers();
182197

183-
expect(insightsClient).not.toHaveBeenCalled();
198+
expect(insightsClient).not.toHaveBeenCalledWith(
199+
'viewedObjectIDs',
200+
expect.any(Object)
201+
);
184202
});
185203

186204
test('debounces calls', async () => {
@@ -271,7 +289,10 @@ describe('createAlgoliaInsightsPlugin', () => {
271289
await runAllMicroTasks();
272290
jest.runAllTimers();
273291

274-
expect(insightsClient).not.toHaveBeenCalled();
292+
expect(insightsClient).not.toHaveBeenCalledWith(
293+
'viewedObjectIDs',
294+
expect.any(Object)
295+
);
275296
});
276297

277298
test('does not send an event when there are no results', async () => {
@@ -296,7 +317,10 @@ describe('createAlgoliaInsightsPlugin', () => {
296317
await runAllMicroTasks();
297318
jest.runAllTimers();
298319

299-
expect(insightsClient).not.toHaveBeenCalled();
320+
expect(insightsClient).not.toHaveBeenCalledWith(
321+
'viewedObjectIDs',
322+
expect.any(Object)
323+
);
300324
});
301325
});
302326

@@ -436,7 +460,10 @@ describe('createAlgoliaInsightsPlugin', () => {
436460

437461
await runAllMicroTasks();
438462

439-
expect(insightsClient).not.toHaveBeenCalled();
463+
expect(insightsClient).not.toHaveBeenCalledWith(
464+
'clickedObjectIDsAfterSearch',
465+
expect.any(Object)
466+
);
440467
});
441468

442469
test('does not send an event with non-Algolia Insights hits', async () => {
@@ -464,7 +491,10 @@ describe('createAlgoliaInsightsPlugin', () => {
464491

465492
await runAllMicroTasks();
466493

467-
expect(insightsClient).not.toHaveBeenCalled();
494+
expect(insightsClient).not.toHaveBeenCalledWith(
495+
'viewedObjectIDs',
496+
expect.any(Object)
497+
);
468498
});
469499
});
470500

@@ -493,6 +523,9 @@ describe('createAlgoliaInsightsPlugin', () => {
493523
},
494524
});
495525

526+
// The client is always called once with `addAlgoliaAgent` on `subscribe`
527+
insightsClient.mockClear();
528+
496529
inputElement.focus();
497530

498531
await runAllMicroTasks();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ export function createAlgoliaInsightsPlugin(
8989
onSelect: onSelectEvent,
9090
onActive: onActiveEvent,
9191
} = getOptions(options);
92-
9392
const insights = createSearchInsightsApi(insightsClient);
9493
const previousItems = createRef<AlgoliaInsightsHit[]>([]);
9594

@@ -123,6 +122,8 @@ export function createAlgoliaInsightsPlugin(
123122
return {
124123
name: 'aa.algoliaInsightsPlugin',
125124
subscribe({ setContext, onSelect, onActive }) {
125+
insightsClient('addAlgoliaAgent', 'insights-plugin');
126+
126127
setContext({ algoliaInsightsPlugin: { insights } });
127128

128129
onSelect(({ item, state, event }) => {

0 commit comments

Comments
 (0)