Skip to content

Commit 8048442

Browse files
authored
fix(insights): pass clickAnalytics automatically (#1080)
* fix(insights): pass clickAnalytics automatically * docs(examples): remove manual clickAnalytics * make conditional * test * lint * wording changes
1 parent 50bdbd1 commit 8048442

File tree

12 files changed

+225
-14
lines changed

12 files changed

+225
-14
lines changed

cypress/test-apps/js/app.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
4545
indexName: 'instant_search_demo_query_suggestions',
4646
getSearchParams({ state }) {
4747
return recentSearchesPlugin.data.getAlgoliaSearchParams({
48-
clickAnalytics: true,
4948
hitsPerPage: state.query ? 5 : 10,
5049
});
5150
},
@@ -86,7 +85,6 @@ autocomplete({
8685
indexName: 'instant_search',
8786
query,
8887
params: {
89-
clickAnalytics: true,
9088
attributesToSnippet: ['name:10', 'description:35'],
9189
snippetEllipsisText: '…',
9290
},

examples/playground/app.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
3838
indexName: 'instant_search_demo_query_suggestions',
3939
getSearchParams({ state }) {
4040
return recentSearchesPlugin.data.getAlgoliaSearchParams({
41-
clickAnalytics: true,
4241
hitsPerPage: state.query ? 5 : 10,
4342
});
4443
},
@@ -80,7 +79,6 @@ autocomplete<ProductHit>({
8079
indexName: 'instant_search',
8180
query,
8281
params: {
83-
clickAnalytics: true,
8482
attributesToSnippet: ['name:10', 'description:35'],
8583
snippetEllipsisText: '…',
8684
},

examples/query-suggestions-with-hits/app.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ autocomplete<ProductHit>({
5858
indexName: 'instant_search',
5959
query,
6060
params: {
61-
clickAnalytics: true,
6261
attributesToSnippet: ['name:10'],
6362
snippetEllipsisText: '…',
6463
},

examples/recently-viewed-items/app.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ autocomplete<ProductHit>({
4343
indexName: 'instant_search',
4444
query,
4545
params: {
46-
clickAnalytics: true,
4746
attributesToSnippet: ['name:10', 'description:35'],
4847
snippetEllipsisText: '…',
4948
},

examples/reshape/productsPlugin.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export const productsPlugin = {
2626
indexName: 'instant_search',
2727
query,
2828
params: {
29-
clickAnalytics: true,
3029
attributesToSnippet: ['name:10', 'description:35'],
3130
snippetEllipsisText: '…',
3231
hitsPerPage: 15,

examples/tags-in-searchbox/app.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ autocomplete<ProductHit | Tag<TagExtraData>>({
202202
indexName: 'instant_search',
203203
query,
204204
params: {
205-
clickAnalytics: true,
206205
attributesToSnippet: ['name:10'],
207206
snippetEllipsisText: '…',
208207
filters: mapToAlgoliaFilters(tagsByFacet),

examples/tags-with-hits/app.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ autocomplete<ProductHit | Tag<TagExtraData>>({
157157
indexName: 'instant_search',
158158
query,
159159
params: {
160-
clickAnalytics: true,
161160
attributesToSnippet: ['name:10'],
162161
snippetEllipsisText: '…',
163162
filters: mapToAlgoliaFilters(tagsByFacet),

packages/autocomplete-core/src/onInput.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ export function onInput<TItem extends BaseItem>({
113113
...setters,
114114
})
115115
).then((itemsOrDescription) =>
116-
preResolve<TItem>(itemsOrDescription, source.sourceId)
116+
preResolve<TItem>(
117+
itemsOrDescription,
118+
source.sourceId,
119+
store.getState()
120+
)
117121
);
118122
})
119123
)

packages/autocomplete-core/src/resolve.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import {
1212
} from '@algolia/client-search';
1313
import type { SearchClient } from 'algoliasearch/lite';
1414

15-
import { BaseItem, InternalAutocompleteSource } from './types';
15+
import {
16+
AutocompleteState,
17+
BaseItem,
18+
InternalAutocompleteSource,
19+
} from './types';
1620
import { mapToAlgoliaResponse } from './utils';
1721

1822
function isDescription<TItem extends BaseItem>(
@@ -56,15 +60,36 @@ type RequestDescriptionPreResolvedCustom<TItem extends BaseItem> = {
5660

5761
export function preResolve<TItem extends BaseItem>(
5862
itemsOrDescription: TItem[] | TItem[][] | RequesterDescription<TItem>,
59-
sourceId: string
63+
sourceId: string,
64+
state: AutocompleteState<TItem>
6065
):
6166
| RequestDescriptionPreResolved<TItem>
6267
| RequestDescriptionPreResolvedCustom<TItem> {
6368
if (isRequesterDescription<TItem>(itemsOrDescription)) {
69+
const contextParameters =
70+
itemsOrDescription.requesterId === 'algolia'
71+
? Object.assign(
72+
{},
73+
...Object.keys(state.context).map((key) => {
74+
return (state.context[key] as Record<string, unknown>)
75+
?.__algoliaSearchParameters;
76+
})
77+
)
78+
: {};
79+
6480
return {
6581
...itemsOrDescription,
6682
requests: itemsOrDescription.queries.map((query) => ({
67-
query,
83+
query:
84+
itemsOrDescription.requesterId === 'algolia'
85+
? {
86+
...query,
87+
params: {
88+
...contextParameters,
89+
...query.params,
90+
},
91+
}
92+
: query,
6893
sourceId,
6994
transformResponse: itemsOrDescription.transformResponse,
7095
})),

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,4 +692,100 @@ describe('requester', () => {
692692
`);
693693
});
694694
});
695+
696+
test('allows plugins to pass extra parameters to the search client', async () => {
697+
const container = document.createElement('div');
698+
const panelContainer = document.createElement('div');
699+
700+
document.body.appendChild(panelContainer);
701+
702+
const searchClient = createSearchClient({
703+
search: jest.fn(() =>
704+
Promise.resolve(
705+
createMultiSearchResponse<{ label: string }>(
706+
{
707+
hits: [{ objectID: '1', label: 'Hit 1' }],
708+
},
709+
{ facetHits: [{ count: 2, value: 'Hit 2' }] }
710+
)
711+
)
712+
),
713+
});
714+
715+
autocomplete({
716+
container,
717+
panelContainer,
718+
getSources({ query }) {
719+
return [
720+
{
721+
sourceId: 'hits',
722+
getItems() {
723+
return getAlgoliaResults({
724+
searchClient,
725+
queries: [
726+
{
727+
indexName: 'indexName',
728+
query,
729+
},
730+
],
731+
});
732+
},
733+
templates: {
734+
item({ item }) {
735+
return JSON.stringify(item);
736+
},
737+
},
738+
},
739+
{
740+
sourceId: 'facets',
741+
getItems() {
742+
return getAlgoliaFacets({
743+
searchClient,
744+
queries: [
745+
{
746+
indexName: 'indexName',
747+
facet: 'categories',
748+
params: {
749+
facetQuery: query,
750+
},
751+
},
752+
],
753+
});
754+
},
755+
templates: {
756+
item({ item }) {
757+
return JSON.stringify(item);
758+
},
759+
},
760+
},
761+
];
762+
},
763+
}).setContext({
764+
myPlugin: {
765+
__algoliaSearchParameters: {
766+
extraParam: true,
767+
},
768+
},
769+
});
770+
771+
const input = container.querySelector<HTMLInputElement>('.aa-Input');
772+
773+
fireEvent.input(input, { target: { value: 'a' } });
774+
775+
await waitFor(() => {
776+
expect(
777+
panelContainer.querySelector<HTMLElement>('.aa-Panel')
778+
).toBeInTheDocument();
779+
});
780+
781+
expect(searchClient.search).toHaveBeenCalledTimes(1);
782+
expect(searchClient.search).toHaveBeenCalledWith([
783+
expect.objectContaining({
784+
params: expect.objectContaining({ extraParam: true }),
785+
}),
786+
expect.objectContaining({
787+
params: expect.objectContaining({ extraParam: true }),
788+
}),
789+
]);
790+
});
695791
});

0 commit comments

Comments
 (0)