Skip to content

Commit 26691a5

Browse files
feat(api): allow insights options to be forwarded to Autocomplete (#1904)
* Allow insights options to be forwarded to Autocomplete * Add autocomplete params to hits for insights * Fixes TS errors after Autocomplete update * Bump bundlesize after Autocomplete update * Cleanup checking if insights is active * Add documentation for sending events with DocSearch * Move insights boolean casting to inside of getSources * Update packages/docsearch-react/src/DocSearchModal.tsx Co-authored-by: François Chalifour <[email protected]> --------- Co-authored-by: François Chalifour <[email protected]>
1 parent 066539c commit 26691a5

File tree

9 files changed

+113
-28
lines changed

9 files changed

+113
-28
lines changed

bundlesize.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
},
77
{
88
"path": "packages/docsearch-react/dist/umd/index.js",
9-
"maxSize": "20.30 kB"
9+
"maxSize": "22.47 kB"
1010
},
1111
{
1212
"path": "packages/docsearch-js/dist/umd/index.js",
13-
"maxSize": "28.20 kB"
13+
"maxSize": "30.33 kB"
1414
}
1515
]
1616
}

examples/demo/src/App.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function App() {
1212
indexName="docsearch"
1313
appId="R2IYF7ETH7"
1414
apiKey="599cec31baffa4868cae4e79f180729b"
15+
insights
1516
/>
1617
</div>
1718
);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"cy:verify": "cypress verify",
2626
"lint:css": "stylelint **/src/**/*.css",
2727
"lint": "eslint --ext .js,.ts,.tsx .",
28+
"playground:build": "yarn workspace @docsearch/react-example build",
2829
"playground:start": "yarn workspace @docsearch/react-example start",
2930
"playground-js:start": "yarn workspace @docsearch/js-example start",
3031
"release": "shipjs prepare",

packages/docsearch-react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
"watch": "watch \"yarn on:change\" --ignoreDirectoryPattern \"/dist/\""
3434
},
3535
"dependencies": {
36-
"@algolia/autocomplete-core": "1.8.2",
37-
"@algolia/autocomplete-preset-algolia": "1.8.2",
36+
"@algolia/autocomplete-core": "1.9.2",
37+
"@algolia/autocomplete-preset-algolia": "1.9.2",
3838
"@docsearch/css": "3.4.0",
3939
"algoliasearch": "^4.0.0"
4040
},

packages/docsearch-react/src/DocSearch.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface DocSearchProps {
4444
navigator?: AutocompleteOptions<InternalDocSearchHit>['navigator'];
4545
translations?: DocSearchTranslations;
4646
getMissingResultsUrl?: ({ query }: { query: string }) => string;
47+
insights?: AutocompleteOptions<InternalDocSearchHit>['insights'];
4748
}
4849

4950
export function DocSearch(props: DocSearchProps) {

packages/docsearch-react/src/DocSearchModal.tsx

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function DocSearchModal({
5858
initialQuery: initialQueryFromProp = '',
5959
translations = {},
6060
getMissingResultsUrl,
61+
insights = false,
6162
}: DocSearchModalProps) {
6263
const {
6364
footer: footerTranslations,
@@ -147,6 +148,7 @@ export function DocSearchModal({
147148
searchSuggestions: [],
148149
},
149150
},
151+
insights,
150152
navigator,
151153
onStateChange(props) {
152154
setState(props.state);
@@ -171,7 +173,7 @@ export function DocSearchModal({
171173
return item.url;
172174
},
173175
getItems() {
174-
return recentSearches.getAll();
176+
return recentSearches.getAll() as InternalDocSearchHit[];
175177
},
176178
},
177179
{
@@ -187,12 +189,14 @@ export function DocSearchModal({
187189
return item.url;
188190
},
189191
getItems() {
190-
return favoriteSearches.getAll();
192+
return favoriteSearches.getAll() as InternalDocSearchHit[];
191193
},
192194
},
193195
];
194196
}
195197

198+
const insightsActive = Boolean(insights);
199+
196200
return searchClient
197201
.search<DocSearchHit>([
198202
{
@@ -224,6 +228,7 @@ export function DocSearchModal({
224228
highlightPreTag: '<mark>',
225229
highlightPostTag: '</mark>',
226230
hitsPerPage: 20,
231+
clickAnalytics: insightsActive,
227232
...searchParameters,
228233
},
229234
},
@@ -260,6 +265,19 @@ export function DocSearchModal({
260265

261266
setContext({ nbHits });
262267

268+
let insightsParams = {};
269+
270+
if (insightsActive) {
271+
insightsParams = {
272+
__autocomplete_indexName: indexName,
273+
__autocomplete_queryID: results[0].queryID,
274+
__autocomplete_algoliaCredentials: {
275+
appId,
276+
apiKey,
277+
},
278+
};
279+
}
280+
263281
return Object.values<DocSearchHit[]>(sources).map(
264282
(items, index) => {
265283
return {
@@ -285,16 +303,23 @@ export function DocSearchModal({
285303
.map(transformItems)
286304
.map((groupedHits) =>
287305
groupedHits.map((item) => {
306+
let parent: InternalDocSearchHit | null = null;
307+
308+
const potentialParent = groupedHits.find(
309+
(siblingItem) =>
310+
siblingItem.type === 'lvl1' &&
311+
siblingItem.hierarchy.lvl1 ===
312+
item.hierarchy.lvl1
313+
) as InternalDocSearchHit | undefined;
314+
315+
if (item.type !== 'lvl1' && potentialParent) {
316+
parent = potentialParent;
317+
}
318+
288319
return {
289320
...item,
290-
__docsearch_parent:
291-
item.type !== 'lvl1' &&
292-
groupedHits.find(
293-
(siblingItem) =>
294-
siblingItem.type === 'lvl1' &&
295-
siblingItem.hierarchy.lvl1 ===
296-
item.hierarchy.lvl1
297-
),
321+
__docsearch_parent: parent,
322+
...insightsParams,
298323
};
299324
})
300325
)
@@ -320,6 +345,9 @@ export function DocSearchModal({
320345
navigator,
321346
transformItems,
322347
disableUserPersonalization,
348+
insights,
349+
appId,
350+
apiKey,
323351
]
324352
);
325353

packages/docsearch-react/src/types/DocSearchHit.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,10 @@ export declare type DocSearchHit = {
7878
};
7979
};
8080
_distinctSeqID?: number;
81+
__autocomplete_indexName?: string;
82+
__autocomplete_queryID?: string;
83+
__autocomplete_algoliaCredentials?: {
84+
appId: string;
85+
apiKey: string;
86+
};
8187
};

packages/website/docs/DocSearch-v3.mdx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,46 @@ docsearch({
229229

230230
</Tabs>
231231

232+
### Sending events
233+
234+
You can send search events to your DocSearch index by passing in the `insights` parameter when creating your DocSearch instance.
235+
236+
<Tabs
237+
groupId="language"
238+
defaultValue="js"
239+
values={[
240+
{ label: 'JavaScript', value: 'js', },
241+
{ label: 'React', value: 'react', }
242+
]
243+
}>
244+
<TabItem value="js">
245+
246+
```js
247+
docsearch({
248+
searchParameters: {
249+
facetFilters: ['language:en', 'version:1.0.0'],
250+
},
251+
insights,
252+
});
253+
```
254+
255+
</TabItem>
256+
257+
<TabItem value="react">
258+
259+
```jsx
260+
<DocSearch
261+
searchParameters={{
262+
facetFilters: ['language:en', 'version:1.0.0'],
263+
}}
264+
insights
265+
/>
266+
```
267+
268+
</TabItem>
269+
270+
</Tabs>
271+
232272
## Performance optimization
233273

234274
### Preconnect

yarn.lock

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@
77
resolved "https://registry.yarnpkg.com/@adobe/css-tools/-/css-tools-4.0.1.tgz#b38b444ad3aa5fedbb15f2f746dcd934226a12dd"
88
integrity sha512-+u76oB43nOHrF4DDWRLWDCtci7f3QJoEBigemIdIeTi1ODqjx6Tad9NCVnPRwewWlKkVab5PlK8DCtPTyX7S8g==
99

10-
"@algolia/autocomplete-core@1.8.2":
11-
version "1.8.2"
12-
resolved "https://registry.yarnpkg.com/@algolia/autocomplete-core/-/autocomplete-core-1.8.2.tgz#8d758c8652742e2761450d2b615a841fca24e10e"
13-
integrity sha512-mTeshsyFhAqw/ebqNsQpMtbnjr+qVOSKXArEj4K0d7sqc8It1XD0gkASwecm9mF/jlOQ4Z9RNg1HbdA8JPdRwQ==
10+
"@algolia/autocomplete-core@1.9.2":
11+
version "1.9.2"
12+
resolved "https://registry.yarnpkg.com/@algolia/autocomplete-core/-/autocomplete-core-1.9.2.tgz#1c9ffcfac7fc4733fe97356247b25d9d7a83538c"
13+
integrity sha512-hkG80c9kx9ClVAEcUJbTd2ziVC713x9Bji9Ty4XJfKXlxlsx3iXsoNhAwfeR4ulzIUg7OE5gez0UU1zVDdG7kg==
1414
dependencies:
15-
"@algolia/autocomplete-shared" "1.8.2"
15+
"@algolia/autocomplete-plugin-algolia-insights" "1.9.2"
16+
"@algolia/autocomplete-shared" "1.9.2"
1617

17-
"@algolia/autocomplete-preset-algolia@1.8.2":
18-
version "1.8.2"
19-
resolved "https://registry.yarnpkg.com/@algolia/autocomplete-preset-algolia/-/autocomplete-preset-algolia-1.8.2.tgz#706e87f94c5f198c0e90502b97af09adeeddcc79"
20-
integrity sha512-J0oTx4me6ZM9kIKPuL3lyU3aB8DEvpVvR6xWmHVROx5rOYJGQcZsdG4ozxwcOyiiu3qxMkIbzntnV1S1VWD8yA==
18+
"@algolia/autocomplete-plugin-algolia-insights@1.9.2":
19+
version "1.9.2"
20+
resolved "https://registry.yarnpkg.com/@algolia/autocomplete-plugin-algolia-insights/-/autocomplete-plugin-algolia-insights-1.9.2.tgz#b4672d5662acc2d0a0547d14dfbdcc70c17625de"
21+
integrity sha512-2LVsf4W66hVHQ3Ua/8k15oPlxjELCztbAkQm/hP42Sw+GLkHAdY1vaVRYziaWq64+Oljfg6FKkZHCdgXH+CGIA==
2122
dependencies:
22-
"@algolia/autocomplete-shared" "1.8.2"
23+
"@algolia/autocomplete-shared" "1.9.2"
2324

24-
"@algolia/[email protected]":
25-
version "1.8.2"
26-
resolved "https://registry.yarnpkg.com/@algolia/autocomplete-shared/-/autocomplete-shared-1.8.2.tgz#e6972df5c6935a241f16e4909aa82902338e029d"
27-
integrity sha512-b6Z/X4MczChMcfhk6kfRmBzPgjoPzuS9KGR4AFsiLulLNRAAqhP+xZTKtMnZGhLuc61I20d5WqlId02AZvcO6g==
25+
"@algolia/[email protected]":
26+
version "1.9.2"
27+
resolved "https://registry.yarnpkg.com/@algolia/autocomplete-preset-algolia/-/autocomplete-preset-algolia-1.9.2.tgz#a31fc9a88800ee7312cd177c738e9e4c0e0f78e8"
28+
integrity sha512-pqgIm2GNqtCT59Y1ICctIPrYTi34+wNPiNWEclD/yDzp5uDUUsyGe5XrUjCNyQRTKonAlmYxoaEHOn8FWgmBHA==
29+
dependencies:
30+
"@algolia/autocomplete-shared" "1.9.2"
31+
32+
"@algolia/[email protected]":
33+
version "1.9.2"
34+
resolved "https://registry.yarnpkg.com/@algolia/autocomplete-shared/-/autocomplete-shared-1.9.2.tgz#b5b909377439c45774cfb91947ad8e6ebd4652c1"
35+
integrity sha512-XxX6YDn+7LG+SmdpXEOnj7fc3TjiVpQ0CbGhjLwrd2tYr6LVY2D4Iiu/iuYJ4shvVDWWnpwArSk0uIWC/8OPUA==
2836

2937
"@algolia/[email protected]":
3038
version "4.14.1"

0 commit comments

Comments
 (0)