createAlgoliaInsightsPlugin
The Algolia Insights plugin automatically sends click and conversion events to the Algolia Insights API whenever a user interacts with the autocomplete.
#
InstallationFirst, you need to install the plugin.
Then import it in your project:
If you don't use a package manager, you can use a standalone endpoint:
#
ExamplesThis example uses the plugin within autocomplete-js
, along with the algoliasearch
API client and Search Insights library.
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:
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:
note
If you send events to other analytics providers, it might make sense to create a dedicated plugin.
#
ParametersinsightsClient
#
InsightsClient
| required
The initialized Search Insights client.
onItemsChange
#
(params: OnItemsChangeParams) => void
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 the user types. This hook is debounced every 400ms to reflect actual items that users notice and avoid generating too many events for items matching "in progress" queries.
onSelect
#
(params: OnSelectParams) => void
Hook to send an Insights event whenever an item is selected.
By default, it sends a clickedObjectIDsAfterSearch
event.
onActive
#
(params: OnActiveParams) => void
Hook to send an Insights event whenever an item is active.
By default, it doesn't send any events.
#
ResourcesFor a more comprehensive guide on how to best leverage Algolia Insights with Autocomplete, check the Sending Algolia Insights events guide.
You can also learn more about Click and Conversion Analytics on the Algolia documentation.