Guides / Sending events / Implementing events / Connectors

The validation process for the Segment connector has been improved, so invalid events / errors that weren’t displayed before are now appearing. Your existing valid events are still here.

If you already have integrated Segment on your website and are sending events to Segment, you can configure your Segment dashboard to redirect the events to Algolia.

There are two steps to integrate Segment with Algolia:

  1. Enable the “Algolia” destination on your Segment dashboard.
  2. Add extra Algolia-related data to events sent to Segment.

Enable “Algolia” destination on your Segment dashboard

On your source, click the “Add Destination” button. 1 sources

From the destination catalog, search for and add “Algolia”. 2 destinations

Copy your Application ID and Search-Only API Key from your Algolia dashboard and paste there. 3 configuration

Enable clickAnalytics

First, you need to enable clickAnalytics to retrieve queryID in your search response.

1
2
3
4
5
6
index.search('query', {
  userToken: 'user-1',
  clickAnalytics: true
}).then(({ hits, queryID }) => {
  console.log(hits, queryID);
})

Required properties

With Segment integrated, you probably have code that looks something like the following to send events to Segment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
analytics.track('Product Clicked', {
  product_id: '507f1f77bcf86cd799439011',
  sku: 'G-32',
  category: 'Games',
  name: 'Monopoly: 3rd Edition',
  brand: 'Hasbro',
  variant: '200 pieces',
  price: 18.99,
  quantity: 1,
  coupon: 'MAYDEALS',
  position: 3,
  url: 'https://www.example.com/product/path',
  image_url: 'https://www.example.com/product/path.jpg',
})

Algolia requires the following fields that aren’t a part of the regular specifications from Segment:

  • index: string (required)
  • eventType: 'view' | 'click' | 'conversion' (required)
  • queryID: string
  • objectIDs: string[]
    • or objectID: string
  • positions: number[]
    • or position: number
  • filters: Array<{ type: string; value: string; }> or string[](${type}:${value}, for example, brand:apple)

This is an example of the final payload:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
analytics.track('Product Clicked', {
  product_id: '507f1f77bcf86cd799439011',
  sku: 'G-32',
  category: 'Games',
  name: 'Monopoly: 3rd Edition',
  brand: 'Hasbro',
  variant: '200 pieces',
  price: 18.99,
  quantity: 1,
  coupon: 'MAYDEALS',
  position: 3,
  url: 'https://www.example.com/product/path',
  image_url: 'https://www.example.com/product/path.jpg',
  
  index: 'my-algolia-index',
  eventType: 'click',
  queryID: 'fd0bbaadc287937s7671d00f1d053b88',
  objectID: '131280270',
  position: 1
});

You can get all those properties like the following:

1
2
3
4
5
6
7
8
index.search('query', {
  userToken: 'user-1',
}).then(({ hits, queryID }) => {
  hits.map((hit, index) => {
    const position = index + 1; // position starts from 1
    const objectID = hit.objectID;
  });
});

Debugging events

First, you can validate your events on the Segment dashboard.

Debugging 1

The green box showing “200 OK” means that the event is successfully delivered to Algolia.

Debugging 2

Now, you can check the events on the Algolia dashboard. Go to the Events Debugger to see more information about your events.

Debugging 3

Using Algolia Personalization with Segment

If you pass a userId to Segment (by using analytics.identify()), you may want to pass the same identifier to Algolia to allow personalized search results. All you need to do is pass the userToken parameter with your search.

1
2
3
4
5
6
index.search('query', {
  userToken: 'user-1',
  enablePersonalization: true
}).then(({ hits }) => {
  console.log(hits);
})
Did you find this page helpful?