Concepts / Getting insights and analytics / Send Click and Conversion Analytics with Angular InstantSearch
Jun. 24, 2019

Send Click and Conversion Analytics with Angular InstantSearch

You are reading the documentation for Angular InstantSearch v3, which is in beta. You can find the v2 documentation here.

This guide describes how to use Algolia Insights with Angular InstantSearch.

Install the Search-Insights Library

To make use of analytics, the search-insights library has to be added to your application.

The Insights library can either be loaded via jsDelivr CDN or from your own static fileserver. If you choose the latter, you have to update the ALGOLIA_INSIGHTS_SRC variable to point to the URL of the file on your own fileserver. We recommend loading the library by adding this snippet in the <head> of your HTML file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script>
  var ALGOLIA_INSIGHTS_SRC = "https://cdn.jsdelivr.net/npm/search-insights@1.1.1";

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


  // Initialize library
  aa('init', {
    appId: 'APPLICATION_ID',
    apiKey: 'SEARCH_API_KEY'
  });
</script>

jsDelivr is a third-party CDN. We are not able to provide support regarding third party services.

Connect InstantSearch with the Insights client for JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import * as algoliasearch from 'algoliasearch/lite';
import { Component } from "@angular/core";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  config = {
    searchClient: algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey'),
    insightsClient: (window as any).aa,
    indexName: "demo_ecommerce"
  };
}

When sending an event to Algolia, you need to include the queryID of the related search. However, by default, the search response does not contain a queryID. Therefore, in order to retrieve it, the custom search parameter clickAnalytics must be set to true and needs to be added when instantiating the search.

To enable the queryID response from the search API, Angular InstantSearch enables setting custom query parameters with the ais-configure widget:

1
<ais-configure [searchParameters]="{ clickAnalytics: true }"></ais-configure>

Hook as user action to an Insights event

You will often send events when the user interacts with search results. You could send these events from the ais-hits or the ais-infinite-hits widget.

In this example, we set up the ais-hits widget.

1
2
3
4
5
6
7
8
9
10
<ais-hits>
  <ng-template let-hits="hits" let-results="results" let-insights="insights">
    <div *ngFor="let hit of hits">
      <ais-highlight attribute="name" [hit]="hit"></ais-highlight>
      <button (click)="insights('clickedObjectIDsAfterSearch', { eventName: 'Add to favorite', objectIDs: [hit.objectID] })">
        Favorite
      </button>
    </div>
  </ng-template>
</ais-hits>

Or your conversion event:

1
2
3
<button (click)="insights('convertedObjectIDsAfterSearch', { eventName: 'Add to cart', objectIDs: [hit.objectID] })">
  Add to cart
</button>

Insights events (click, conversion, view) used for analytics and/or personalization do not take immediate effect. The delay can range from 10 to 60 minutes depending on how long after the search they are sent. For precise times, see our page on when Insights events take effect.

When sending a conversion event, you’re not always on the search page (for example, you could be on a product detail page). Therefore, you need to pass the queryID to the detail page.

Did you find this page helpful?