Skip to content

Commit

Permalink
feat(insights): forward insights usertoken to algolia api calls (#1179)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhayab authored Aug 11, 2023
1 parent f341055 commit 7a71eb4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"files": [
{
"path": "packages/autocomplete-core/dist/umd/index.production.js",
"maxSize": "8.5 kB"
"maxSize": "8.75 kB"
},
{
"path": "packages/autocomplete-js/dist/umd/index.production.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('createAutocomplete', () => {
insights: { insightsClient },
});

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledTimes(3);
expect(insightsClient).toHaveBeenCalledWith(
'addAlgoliaAgent',
'insights-plugin'
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('createAutocomplete', () => {
});

expect(defaultInsightsClient).toHaveBeenCalledTimes(0);
expect(userInsightsClient).toHaveBeenCalledTimes(1);
expect(userInsightsClient).toHaveBeenCalledTimes(3);
expect(userInsightsClient).toHaveBeenCalledWith(
'addAlgoliaAgent',
'insights-plugin'
Expand Down
6 changes: 3 additions & 3 deletions packages/autocomplete-js/src/__tests__/autocomplete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,16 +751,16 @@ See: https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocom
insights: { insightsClient: defaultInsightsClient },
});

expect(defaultInsightsClient).toHaveBeenCalledTimes(1);
expect(defaultInsightsClient).toHaveBeenCalledTimes(3);
expect(userInsightsClient).toHaveBeenCalledTimes(0);

const insightsPlugin = createAlgoliaInsightsPlugin({
insightsClient: userInsightsClient,
});
update({ plugins: [insightsPlugin] });

expect(defaultInsightsClient).toHaveBeenCalledTimes(1);
expect(userInsightsClient).toHaveBeenCalledTimes(1);
expect(defaultInsightsClient).toHaveBeenCalledTimes(3);
expect(userInsightsClient).toHaveBeenCalledTimes(3);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('createAlgoliaInsightsPlugin', () => {

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

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledTimes(3);
expect(insightsClient).toHaveBeenCalledWith(
'addAlgoliaAgent',
'insights-plugin'
Expand Down Expand Up @@ -174,6 +174,54 @@ describe('createAlgoliaInsightsPlugin', () => {
]);
});

test('forwards `userToken` from Search Insights to Algolia API requests', async () => {
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });

const searchClient = createSearchClient({
search: jest.fn(() =>
Promise.resolve(
createMultiSearchResponse({
hits: [{ objectID: '1' }],
})
)
),
});

insightsClient('setUserToken', 'customUserToken');

const playground = createPlayground(createAutocomplete, {
plugins: [insightsPlugin],
getSources({ query }) {
return [
{
sourceId: 'hits',
getItems() {
return getAlgoliaResults({
searchClient,
queries: [{ indexName: 'indexName', query }],
});
},
templates: {
item({ item }) {
return item.objectID;
},
},
},
];
},
});

userEvent.type(playground.inputElement, 'a');
await runAllMicroTasks();

expect(searchClient.search).toHaveBeenCalledTimes(1);
expect(searchClient.search).toHaveBeenCalledWith([
expect.objectContaining({
params: expect.objectContaining({ userToken: 'customUserToken' }),
}),
]);
});

describe('automatic pulling', () => {
const consoleError = jest
.spyOn(console, 'error')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,24 @@ export function createAlgoliaInsightsPlugin(
return {
name: 'aa.algoliaInsightsPlugin',
subscribe({ setContext, onSelect, onActive }) {
function setInsightsContext(userToken?: string) {
setContext({
algoliaInsightsPlugin: {
__algoliaSearchParameters: {
clickAnalytics: true,
...(userToken ? { userToken } : {}),
},
insights,
},
});
}

insightsClient('addAlgoliaAgent', 'insights-plugin');

setContext({
algoliaInsightsPlugin: {
__algoliaSearchParameters: {
clickAnalytics: true,
},
insights,
},
setInsightsContext();
insightsClient('onUserTokenChange', setInsightsContext);
insightsClient('getUserToken', null, (_error, userToken) => {
setInsightsContext(userToken);
});

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

0 comments on commit 7a71eb4

Please sign in to comment.