- Add the
insightsclient. - Add click events when users click search results.
- Track conversions that start in your InstantSearch app.
Add the InstantSearch Insights library
Add the library as a dependency to yourbuild.gradle file:
gradle
Add the Insights client
To add the Insights client, you’ll need your Algolia application ID, (search) API key, and the index name. You can find your application ID and API key in the Algolia dashboard.Kotlin
generateTimestamps parameter to false:
Kotlin
Set the userToken
All events must have a userToken field to specify the user it relates to. You can set the userToken field in three ways:
- Globally, for all events
- Per app, for every event tracked by the app
- Individually, for each event
Kotlin
User opt-out
You can exclude users who opted out from tracking with the following code:Kotlin
Enable automatic view events tracking
InstantSearch can automatically send view events when hits are returned from Algolia. To enable this feature, setisAutoSendingHitsViewEvents to true when you initialize HitsSearcher:
Kotlin
Create a HitsTracker to track events from search results
Kotlin
Create a FilterTracker to track events from filters
Kotlin
Send events
Use thetrackView, trackClick, and trackConversion methods to send events.
When you add events, check them in the Events Debugger in the Algolia dashboard.
For more information, see Validate your events.
Send view events
Kotlin
Send click events
Kotlin
Send conversion events
Kotlin
Event batching
By default, events are sent in batches of 10. To customize this, adjust theminBatchSize parameter:
Kotlin
Enable logging
You can also check if you’ve sent an event by enabling logging:Kotlin
For the
positions parameter of the clickedObjectIdsAfterSearch method, the first object in the list of search results has a value of 1 (not 0), the second has a value of 2.