Before you begin
To follow this guide, you need to build both search results and category pages with one of these UI libraries:- InstantSearch.js version 4.59 or later (Upgrade guide)
- React InstantSearch version 7.2.1 or later (Upgrade guide)
- Vue InstantSearch version 4.12.0 or later (Upgrade guide)
Manage the Insights library
InstantSearch loads thesearch-insights
library for you from jsDelivr. You don’t need to install it or set it up yourself.
If you’re using a Content Security Policy to protect your site and you want to let InstantSearch load search-insights
for you, make sure to add https://cdn.jsdelivr.net
in your list of trusted sources for JavaScript.
search-insights
, you can add it to your project:
InstantSearch doesn’t load search-insights
when it detects it on the page.
Enable events collection
You can enable the automatic collection of events from your InstantSearch apps in the Algolia dashboard (without coding), or when setting up your InstantSearch app (with coding). Enabling automatic events collection takes care of the following:- Add the
search-insights
library to your project and make it globally available aswindow.aa
- Set an anonymous
userToken
for sending events to the Insights API and for search requests - Include the
queryID
parameter in the search response - Send default click and view events for your InstantSearch widgets
No code
- Go to the Algolia dashboard and select your Algolia application.
- On the left sidebar, select Data sources > Events > Settings.
- Click Enable automatic events collection.
Code
Enable theinsights
option:
Understand which events to track
You may want to send events that are not automatically captured by InstantSearch widgets. To understand which events you should send, think about the different steps a user takes through your website to perform desired actions. For example, a user might: Be presented with a list of real estate properties after performing a search from your homepage. Then click on a property to view more details. Finally, convert by contacting the realtor. In this example, you should track the following events:Property Clicked
by using theclickedObjectIDAfterSearch
method.Property Contacted
by using theconvertedObjectIDAfterSearch
method.
A conversion is any user action that’s valuable to your business; a user purchasing from your store or subscribing to your newsletter are examples of common conversions.
— GA4 About conversions
Keep track of query IDs
Conversion events are often triggered on pages that InstantSearch doesn’t cover, such as your item details pages. To relate the conversion events back to the search request made on your search results or category pages, you need to keep track of the query IDs across your pages.Track conversion events
Using InstantSearch widgets
You can use thesendEvent
function to send conversion events from your InstantSearch app.
Unlike
click
events, setting custom conversion
events doesn’t prevent the custom click
event from being sent.On pages without InstantSearch widgets
Conversion events represent user actions that are important to the success of your business. For example:JavaScript
window.aa
object is the API client for the Insights API and is globally available
if you enabled automatic events collection.
The queryID
parameter is used by Algolia to relate the event to a prior search.
If the event is not directly related to a search, for example, it’s triggered on the homepage,
use the convertedObjectIDs
method instead.
JavaScript
Track click events
Override default click events
Thehits
and infiniteHits
widgets expose a sendEvent
function. Use it to send click
events when users interact with your search results.
- A
click
event with theeventName
“Item More Info Clicked”. - A
click
event with theeventName
“Item Clicked” (through event propagation).
Event.stopPropagation
in your event handler.
click
event that you defined, it doesn’t send the default click
event.
In the following example, when users click the View more info button,
only the “Item More Info Clicked” event is sent.
Track additional click events
For Algolia Recommend and Personalization, capture additional click events when users select individual items:- On your home page
- From recommendations
JavaScript
Optional: set up personalization
For effective personalization, you need to identify users across sessions. It’s best to use an identifier from your authentication system after users sign in. For more information, see User token. After getting the identifier from your system, set it as theauthenticatedUserToken
parameter.
JavaScript
JavaScript
JavaScript
Examples
- InstantSearch.js:
- React InstantSearch:
- Vue InstantSearch: