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.Use the insights option
This option is available starting from v1.9.2.
insights
option of the autocomplete function.
JavaScript
If you don’t pass an
insightsClient,
it will be automatically detected from the window object,
or downloaded from the jsDelivr CDN.Installation
First, you need to install the plugin.JavaScript
script element:
HTML
If you don’t pass an
insightsClient,
it will be automatically detected from the window object,
or downloaded from the jsDelivr CDN.Examples
This example uses the plugin withinautocomplete-js, along with the algoliasearch API client and Search Insights library.
JavaScript
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:
JavaScript
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:
JavaScript
JavaScript
If you send events to other analytics providers,
it might make sense to create a dedicated plugin.
Parameters
The Search Insights client.
If not provided, the plugin will use the global
window.aa object,
or fetch it from the jsDelivr CDN.Insights parameters to forward to the Insights client’s
init method.With search-insights >= v1.7.0 and < 2.0.0, the Insights client accepts useCookie and userToken parameters in the init method.
You can pass useCookie: false to prevent the usage of cookies to store an anonymous user token.
You can also pass a custom userToken while creating insights middleware, if you have one.With search-insights >= 2.0.0, the default value of useCookie is false.Hook to send an Insights event whenever the items change.By default, it sends a
viewedObjectIDs event.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.TypeScript
Hook to send an Insights event whenever an item is selected.By default, it sends a
clickedObjectIDsAfterSearch event.TypeScript
Hook to send an Insights event whenever an item is active.By default, it doesn’t send any events.
TypeScript