API client / Methods / Insights
Required API Key: any key with the search ACL
Method signature
$insights->sendEvents(array events)

// Send a single event
$insights->sendEvent(array event)

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 JavaScript 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.

You’re currently reading the JavaScript API client v4 documentation. Check the migration guide to learn how to upgrade from v3 to v4. You can still access the v3 documentation.

You’re currently reading the Ruby API client v2 documentation. Check the migration guide to learn how to upgrade from v1 to v2. You can still access the v1 documentation.

About this method

Send a list of events in a single request.

If you want to send a list of different events in a single batch, such as importing historical data, or if you’re sending events from your backend, you can use the sendEvents method to send multiple events in a single request.

When you’re sending historical events, make sure to set correct timestamps for all events, or else every event will have the same timestamp.

For sending real-time events, consider using the dedicated methods, which makes sure that every event has the required parameters:

Examples

Read the Algolia CLI documentation for more information.

The following example sends three events for the same user: one click event, one view event, and one conversion event.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$insights = Algolia\AlgoliaSearch\InsightsClient::create(
    'YourApplicationID',
    'YourSearchOnlyAPIKey'
);

$response = $insights->sendEvents(array(
    // `clickedObjectIDsAfterSearch`
    array(
        'eventType' => 'click',
        'eventName' => 'Clicked Example',
        'index' => 'your_index_name',
        'userToken' => 'user-123456',
        'objectIDs' => array('objectID-1', 'objectID-2'),
        'timestamp' => 1675343386205,
        'queryID' => 'queryID'
    ),
    // `viewedObjectIDs`
    array(
        'eventType' => 'view',
        'eventName' => 'Viewed Example',
        'index' => 'your_index_name',
        'userToken' => 'user-123456',
        'objectIDs' => array('objectID-1', 'objectID-2'),
        'timestamp' => 1675343386205
    ),
    // `convertedObjectIDsAfterSearch`
    array(
        'eventType' => 'conversion',
        'eventName' => 'Converted Example',
        'index' => 'your_index_name',
         'userToken' => 'user-123456',
        'objectIDs' => array('objectID-1', 'objectID-2'),
        'timestamp' => 1675343386205,
        'queryID' => 'queryID'
    ),
  )
);

Parameters

events
type: array
items: event
Required

An array of event objects

events ➔ event

eventType
type: click | conversion | view
Required

The type of the event, either click, conversion, or view.

Related: API client methods

eventName
type: string
pattern: [\x20-\x7E]{1,64}
Required

The name of the specific event.

Format: 1-64 ASCII characters, except control characters.

To maximize the impact of your events, you should use consistent event names and consistent formatting—for example, “Product Added To Cart” (always in title case).

For example, you can adopt Segment’s object-action framework.

index
type: string
Required

The name of the Algolia index.

Format: same as the index name used by the search engine.

userToken
type: string
pattern: [A-Za-z0-9_-=]{1,128}
Required

A pseudonymous or anonymous user identifier.

Never include personal identifiable information in user tokens.

Related: user token

timestamp
type: int64
default: now()
Optional

The time of the event in milliseconds in Unix epoch time. By default, the Insights API uses the time it receives an event as its timestamp.

If you’re not sending events in real time—for example, if you’re doing backend search, it’s important to set correct timestamps. If you’re sending events in batches and you’re not including a timestamp, all events will have the same timestamp.

The Algolia Insights API accepts events from up to 4 days in the past. Timestamps for click and conversion events must be within 1 hour of the corresponding search or browse request.

queryID
type: string
pattern: [a-f0-9]{32}
Optional

A unique identifier for a search query.

A queryID is required for events related to search or browse requests.

If you add clickAnalytics: true as a search request parameter, the queryID is included in the API response.

Related: Keep track of query IDs

objectIDs
type: array
items: string
Optional

An array of object identifiers for items of an Algolia index.

You can include up to 20 objectID.

A single event must not include both objectIDs and filters.

filters
type: array
items: string
pattern: ${attr}:${value}
Optional

An array of facet filters.

You can include up to 10 filters.

Format: ${attribute}:${value}—for example, brand:apple. Each facet filter string must be URL-encoded, such as, "discount:10%25".

A single event must not include both objectIDs and filters.

positions
type: array
items: uint
Optional

The position of the clicked objects in the search results. This property is required for click events with queryID.

You must provide one position for each objectID.

Algolia uses this parameter to calculate the average click position.

The position is absolute, and not relative, to any results page. For example, if there are 10 results per page, the position of the third record of the second results page is 13. Since page starts at 0, you can use the following formula: $positionOnPage + $page * hitsPerPage.

If you’re using InstantSearch, you can get the position through the hit.__position attribute.

Response

This method doesn't return a response.

Did you find this page helpful?