Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(insights): forward insights usertoken to algolia api calls #1179

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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