Skip to content

Commit 7a71eb4

Browse files
authored
feat(insights): forward insights usertoken to algolia api calls (#1179)
1 parent f341055 commit 7a71eb4

File tree

5 files changed

+71
-14
lines changed

5 files changed

+71
-14
lines changed

bundlesize.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"files": [
33
{
44
"path": "packages/autocomplete-core/dist/umd/index.production.js",
5-
"maxSize": "8.5 kB"
5+
"maxSize": "8.75 kB"
66
},
77
{
88
"path": "packages/autocomplete-js/dist/umd/index.production.js",

packages/autocomplete-core/src/__tests__/createAutocomplete.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('createAutocomplete', () => {
129129
insights: { insightsClient },
130130
});
131131

132-
expect(insightsClient).toHaveBeenCalledTimes(1);
132+
expect(insightsClient).toHaveBeenCalledTimes(3);
133133
expect(insightsClient).toHaveBeenCalledWith(
134134
'addAlgoliaAgent',
135135
'insights-plugin'
@@ -160,7 +160,7 @@ describe('createAutocomplete', () => {
160160
});
161161

162162
expect(defaultInsightsClient).toHaveBeenCalledTimes(0);
163-
expect(userInsightsClient).toHaveBeenCalledTimes(1);
163+
expect(userInsightsClient).toHaveBeenCalledTimes(3);
164164
expect(userInsightsClient).toHaveBeenCalledWith(
165165
'addAlgoliaAgent',
166166
'insights-plugin'

packages/autocomplete-js/src/__tests__/autocomplete.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,16 +751,16 @@ See: https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocom
751751
insights: { insightsClient: defaultInsightsClient },
752752
});
753753

754-
expect(defaultInsightsClient).toHaveBeenCalledTimes(1);
754+
expect(defaultInsightsClient).toHaveBeenCalledTimes(3);
755755
expect(userInsightsClient).toHaveBeenCalledTimes(0);
756756

757757
const insightsPlugin = createAlgoliaInsightsPlugin({
758758
insightsClient: userInsightsClient,
759759
});
760760
update({ plugins: [insightsPlugin] });
761761

762-
expect(defaultInsightsClient).toHaveBeenCalledTimes(1);
763-
expect(userInsightsClient).toHaveBeenCalledTimes(1);
762+
expect(defaultInsightsClient).toHaveBeenCalledTimes(3);
763+
expect(userInsightsClient).toHaveBeenCalledTimes(3);
764764
});
765765
});
766766
});

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('createAlgoliaInsightsPlugin', () => {
8585

8686
createPlayground(createAutocomplete, { plugins: [insightsPlugin] });
8787

88-
expect(insightsClient).toHaveBeenCalledTimes(1);
88+
expect(insightsClient).toHaveBeenCalledTimes(3);
8989
expect(insightsClient).toHaveBeenCalledWith(
9090
'addAlgoliaAgent',
9191
'insights-plugin'
@@ -174,6 +174,54 @@ describe('createAlgoliaInsightsPlugin', () => {
174174
]);
175175
});
176176

177+
test('forwards `userToken` from Search Insights to Algolia API requests', async () => {
178+
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });
179+
180+
const searchClient = createSearchClient({
181+
search: jest.fn(() =>
182+
Promise.resolve(
183+
createMultiSearchResponse({
184+
hits: [{ objectID: '1' }],
185+
})
186+
)
187+
),
188+
});
189+
190+
insightsClient('setUserToken', 'customUserToken');
191+
192+
const playground = createPlayground(createAutocomplete, {
193+
plugins: [insightsPlugin],
194+
getSources({ query }) {
195+
return [
196+
{
197+
sourceId: 'hits',
198+
getItems() {
199+
return getAlgoliaResults({
200+
searchClient,
201+
queries: [{ indexName: 'indexName', query }],
202+
});
203+
},
204+
templates: {
205+
item({ item }) {
206+
return item.objectID;
207+
},
208+
},
209+
},
210+
];
211+
},
212+
});
213+
214+
userEvent.type(playground.inputElement, 'a');
215+
await runAllMicroTasks();
216+
217+
expect(searchClient.search).toHaveBeenCalledTimes(1);
218+
expect(searchClient.search).toHaveBeenCalledWith([
219+
expect.objectContaining({
220+
params: expect.objectContaining({ userToken: 'customUserToken' }),
221+
}),
222+
]);
223+
});
224+
177225
describe('automatic pulling', () => {
178226
const consoleError = jest
179227
.spyOn(console, 'error')

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,24 @@ export function createAlgoliaInsightsPlugin(
156156
return {
157157
name: 'aa.algoliaInsightsPlugin',
158158
subscribe({ setContext, onSelect, onActive }) {
159+
function setInsightsContext(userToken?: string) {
160+
setContext({
161+
algoliaInsightsPlugin: {
162+
__algoliaSearchParameters: {
163+
clickAnalytics: true,
164+
...(userToken ? { userToken } : {}),
165+
},
166+
insights,
167+
},
168+
});
169+
}
170+
159171
insightsClient('addAlgoliaAgent', 'insights-plugin');
160172

161-
setContext({
162-
algoliaInsightsPlugin: {
163-
__algoliaSearchParameters: {
164-
clickAnalytics: true,
165-
},
166-
insights,
167-
},
173+
setInsightsContext();
174+
insightsClient('onUserTokenChange', setInsightsContext);
175+
insightsClient('getUserToken', null, (_error, userToken) => {
176+
setInsightsContext(userToken);
168177
});
169178

170179
onSelect(({ item, state, event, source }) => {

0 commit comments

Comments
 (0)