pushEvents method

Future<void> pushEvents(
  1. List<AlgoliaEvent> events
)

Pushes an array of events to the Insights API.

An event is

  • an action: eventName
  • performed in a context: eventType
  • at some point in time provided: timestamp
  • by an end user: userToken
  • on something: index

Notes:

  • The number of events that can be sent at the same time is limited to 1000.
  • To be accepted, all events sent must be valid.
  • When an event is tied to an Algolia search, it must also provide a queryId. If that event is a click, their absolute positions should also be passed.
  • We consider that an index provides access to 2 resources: objects and filters. An event can only interact with a single resource type, but not necessarily on a single item. As such an event will accept an array of objectIds or filters.

Source: Learn more.

Implementation

Future<void> pushEvents(List<AlgoliaEvent> events) async {
  if (events.isEmpty) return;
  final url = '${_insightsHost}events';
  final eventList = events.map((e) => e.toMap()).toList();
  final response = await http.post(
    Uri.parse(url),
    headers: _headers,
    body: utf8.encode(json.encode({'events': eventList})),
    encoding: Encoding.getByName('utf-8'),
  );
  Map<String, dynamic> body = json.decode(response.body);

  if (!(response.statusCode >= 200 && response.statusCode < 300)) {
    throw AlgoliaError._(body, response.statusCode);
  }
}