createAlgoliaInsightsPlugin
The Algolia Insights plugin automatically sends click and conversion events to the Algolia Insights API whenever a user interacts with the autocomplete.
Starting from version 1.9.2 of Autocomplete, you no longer need to use this plugin directly. Instead, use the insights
option of the autocomplete
function.
Using the insights
option
This option is available starting from v1.9.2.
Instead of using this plugin, you can set up events using the insights
option of the autocomplete
function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { autocomplete } from '@algolia/autocomplete-js';
autocomplete({
container: '#autocomplete',
insights: true,
});
// or with options
autocomplete({
container: '#autocomplete',
insights: {
// ...
},
});
If you don’t pass an insightsClient
, it will be automatically detected from the window
object, or downloaded from the jsDelivr CDN.
If you are using the insights
option, you don’t need to follow the rest of this guide.
Installation
First, you need to install the plugin.
1
2
3
yarn add @algolia/autocomplete-plugin-algolia-insights
# or
npm install @algolia/autocomplete-plugin-algolia-insights
Then import it in your project:
1
import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights';
If you don’t use a package manager, you can use the HTML script
element:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<script src="https://cdn.jsdelivr.net/npm/@algolia/autocomplete-plugin-algolia-insights"></script>
<script>
const { createAlgoliaInsightsPlugin } = window[
'@algolia/autocomplete-plugin-algolia-insights'
];
</script>
If you don't pass an `insightsClient`, it will be automatically detected from the `window` object, or downloaded from the [jsDelivr CDN](https://www.jsdelivr.com/).
{:.alert.alert-info}
## Examples
This example uses the plugin within [`autocomplete-js`](/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete/), along with the [`algoliasearch`](https://www.npmjs.com/package/algoliasearch) API client and [Search Insights](https://www.npmjs.com/package/search-insights) library.
If you don't pass an `insightsClient`, it will be automatically detected from the `window` object, or downloaded from the [jsDelivr CDN](https://www.jsdelivr.com/).
```js
import algoliasearch from 'algoliasearch/lite';
import { autocomplete } from '@algolia/autocomplete-js';
import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights';
import insightsClient from 'search-insights';
const appId = 'latency';
const apiKey = '6be0576ff61c053d5f9a3225e2a90f76';
const searchClient = algoliasearch(appId, apiKey);
insightsClient('init', { appId, apiKey });
const algoliaInsightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });
autocomplete({
container: '#autocomplete',
plugins: [algoliaInsightsPlugin],
});
To identify users consistently, it’s best if you set your own userToken
with setUserToken
—for example, for authenticated users.
The search-insights
library can create anonymous user tokens and store them in cookie.
To use cookie-based anonymous tokens, initialize the library with useCookie
set to true
:
1
2
3
4
5
6
insightsClient('init', { appId, apiKey });
insightsClient('setUserToken', yourUserToken);
// or
insightsClient('init', { appId, apiKey, useCookie: true });
In older versions of the search-insights
library (earlier than version 2.0),
cookie-based anonymous tokens are on by default.
The plugin exposes hooks to let you inject custom logic in the lifecycle: onItemsChange
, onSelect
, and onActive
. You can use them to either customize the events sent to Algolia, or plug additional behavior.
For example, if you have several search experiences on your site, you can customize the event name to identify where the events came from:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const algoliaInsightsPlugin = createAlgoliaInsightsPlugin({
insightsClient,
onItemsChange({ insights, insightsEvents }) {
const events = insightsEvents.map((insightsEvent) => ({
...insightsEvent,
eventName: 'Product Viewed from Autocomplete',
}));
insights.viewedObjectIDs(...events);
},
onSelect({ insights, insightsEvents }) {
const events = insightsEvents.map((insightsEvent) => ({
...insightsEvent,
eventName: 'Product Selected from Autocomplete',
}));
insights.clickedObjectIDsAfterSearch(...events);
},
});
If you’re using another analytics provider along with Algolia Insights, you can leverage these hooks to send them events as well. For example, you can send Segment events:
1
2
3
4
5
6
7
8
9
10
const algoliaInsightsPlugin = createAlgoliaInsightsPlugin({
insightsClient,
onActive({ insights, insightsEvents }) {
insightsEvents.forEach((insightsEvent) => {
// Assuming you've initialized the Segment script
// and identified the current user already
analytics.track('Product Browsed from Autocomplete', insightsEvent);
});
},
});
If you send events to other analytics providers, it might make sense to create a dedicated plugin.
Parameters
Parameter | Description | ||
---|---|---|---|
insightsClient
|
type: InsightsClient
Optional
The Search Insights client. If not provided, the plugin will use the global |
||
insightsInitParams
|
since: v1.14.0
type: object
Optional
Insights parameters to forward to the Insights client’s With With |
||
onItemsChange
|
type: (params: OnItemsChangeParams) => void
Hook to send an Insights event whenever the items change. By default, it sends a In as-you-type experiences, items change as users type. This hook is debounced every 400 ms to reflect actual items that users notice and avoid generating too many events for items matching “in progress” queries.
Copy
|
||
onSelect
|
type: (params: OnSelectParams) => void
Hook to send an Insights event whenever an item is selected. By default, it sends a
Copy
|
||
onActive
|
type: (params: OnActiveParams) => void
Hook to send an Insights event whenever an item is active. By default, it doesn’t send any events.
Copy
|