API Reference / API Methods / Insights
List of methods

It’s recommended to use the Kotlin API client, which is better suited for Android development.

The Insights API lets you capture click, conversion, and view events to help you understand how your users interact with your digital experience.

We released a new version of the JavaScript API client in public beta. Read the beta documentation for more information.

Install the Insights client

The Insights client is distributed as a separate npm package. You can find the source code on GitHub

To send Insights events from your JavaScript application, you can include a code snippet directly in your HTML or install it as a dependency in your project.

Include the Insights client in your HTML

Paste the following code snippet into the <head> tag of every page where you want to track Insights events.

1
2
3
4
5
6
7
8
<script>
var ALGOLIA_INSIGHTS_SRC = "https://cdn.jsdelivr.net/npm/search-insights@2.2.1";

!function(e,a,t,n,s,i,c){e.AlgoliaAnalyticsObject=s,e[s]=e[s]||function(){
(e[s].queue=e[s].queue||[]).push(arguments)},i=a.createElement(t),c=a.getElementsByTagName(t)[0],
i.async=1,i.src=n,c.parentNode.insertBefore(i,c)
}(window,document,"script",ALGOLIA_INSIGHTS_SRC,"aa");
</script>

Install the Insights client with npm

Install the search-insights package in your project:

1
npm install search-insights

Initialize the Insights client

To initialize the Insights client, you need your Algolia application ID and your API key with search permissions. You can find both in your Algolia dashboard.

1
2
3
4
5
6
7
8
9
10
<?php
# Installation instructions:
# https://www.algolia.com/doc/api-client/getting-started/install/
require_once __DIR__."/vendor/autoload.php";
use Algolia\AlgoliaSearch\InsightsClient;

$insightsClient = InsightsClient::create(
  "YourApplicationID",
  "YourSearchOnlyAPIKey"
)

Identify users

You need to provide anonymous or pseudonymous user identifiers as userToken when tracking Insights events. To identify users consistently, you should provide your own user IDs:

1
2
// See previous code snippet for how to initialize the Insights client
aa('set-user-token', 'user-token-1')

The search-insights library can also generate anonymous user IDs that it stores in a cookie.

1
2
3
4
5
6
7
const aa = require('search-insights')

aa('init', {
  appId: 'YourApplicationID',
  apiKey: 'YourSearchOnlyAPIKey',
+ useCookie: true,  
})

Make sure to obtain user consent before storing cookies on a user’s device.

Did you find this page helpful?