API client / Methods
List of methods

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

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

The Algolia Insights API lets you collect events related to your search and discovery experience. Events are actions that users take on your app or website. They unlock powerful features, such as recommendations, personalization, smarter search results, and analytics that help you optimize your user experience.

Install the Insights client

The Insights client is a separate npm package. Find the source code on GitHub.

To send Insights events from your JavaScript app, either 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.15.0/dist/search-insights.min.js";

  !function(e,a,t,n,s,i,c){e.AlgoliaAnalyticsObject=s,e[s]=e[s]||function(){
  (e[s].queue=e[s].queue||[]).push(arguments)},e[s].version=(n.match(/@([^\/]+)\/?/) || [])[1],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>

If you prefer hosting your own version, copy the script in your own project.

1
2
3
4
5
<script>
  var ALGOLIA_INSIGHTS_SRC = 'path/to/search-insights.min.js';

  // …
</script>

Install the Insights client with npm

Install the search-insights package in your project:

1
2
3
npm install search-insights
# or
yarn add 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
<?php
require_once __DIR__."/vendor/autoload.php";
use Algolia\AlgoliaSearch\InsightsClient;

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

Find all the parameters you can use in the init method documentation.

Starting from v2.4.0, you can also pass credentials using headers for each call. If you’re using the client with InstantSearch or Autocomplete, credentials are inferred from the passed search client.

Identify users

It’s important to identify users so that you can give them a consistent experience, especially when using Personalization. Do this by providing an anonymous or pseudonymous user identifier (a userToken or authenticatedUserToken) when tracking Insights events.

Did you find this page helpful?