Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Event batching for the JavaScript (Node) SDK

How the Optimizely Feature Experimentation JavaScript (Node) SDK uses the event processor to batch decision and conversion events into a single payload before sending it to Optimizely.

The JavaScript (Node) SDK lets you batch events and includes options to set an event batch size, event time interval and event flush interval. The benefit of event batching is less network traffic for the same number of impression and conversion events tracked.

πŸ“˜

Note

By default, event batching is enabled in JavaScript (Node) SDK versions 3.3.0 and newer.

Event batching works with both out-of-the-box and custom event dispatchers.

The event batching process does not remove any personally identifiable information (PII) from events. You must still ensure that you are not sending any unnecessary PII to Optimizely.

Configure event batching

We provide two options to configure event batching: eventBatchSize and eventFlushInterval. You can pass in both options during client creation. Events are held in a queue until either:

  • The number of events reaches the defined eventBatchSize.
  • The oldest event has been in the queue for longer than the defined eventFlushInterval, which is specified in milliseconds. The queue is then flushed and all queued events are sent to Optimizely Feature Experimentation in a single network request.
  • A new datafile revision is received. This occurs only when live datafile updates are enabled.
const { createInstance } = require('@optimizely/optimizely-sdk')

const optimizely = createInstance({
  // other options
  eventBatchSize: 100,
  eventFlushInterval: 3000,
});

The table below defines these options and lists general recommendations for browser and server default settings. On the browser we recommend using a small eventBatchSize (10) and a short eventFlushInterval (1000). This ensures that events are sent in a relatively fast manner, since some events could be lost if a user immediately bounces, while gaining network efficiency in cases where many decisions calls happen back-to-back.

For the server, these numbers are more flexible and should be set based on your organization's traffic and needs. Contact Optimizely support for recommendations for your specific implementation.

NameDefault valueDescription
eventBatchSize 10The maximum number of events to hold in the queue. Once this number is reached, all queued events are flushed and sent to Optimizely Feature Experimentation.

Note: By setting this value to 1, events are not batched and event requests behave exactly as in pre-3.3.0 JavaScript (Node)SDKs.
eventFlushInterval 3000 msThe maximum duration in milliseconds that an event can exist in the queue before being flushed.

❗️

Note

The maximum payload size is 3.5 MB. If the resulting batch payload exceeds this limit, requests will be rejected with a 400 response code, Bad Request Error.

The size limitation is because of the Optimizely Events API, which Feature Experimentation uses to send data to Optimizely.

The most common cause of a large payload size is a high batch size. If your payloads are exceeding the size limit, try configuring a smaller batch size.

Close Optimizely Feature Experimentation on application exit

If you enable event batching, you must call the Close method (optimizely.close()) before exiting. This ensures that queued events are flushed as soon as possible to avoid data loss.

❗

Warning

Because the Optimizely client maintains a buffer of queued events, you must call close() on the Optimizely Feature Experimentation instance before shutting down your application or whenever dereferencing the instance.

MethodDescription
close() Stops all timers and flushes the event queue. This method will also stop any timers happening for the datafile manager.

Note: We recommend connecting this method to a kill signal for the running process.

You can hook into the SIGTERM event to ensure that close() is invoked, guaranteeing that all events in the queue are sent.

// respond to SIGTERM and close optimizely
process.on('SIGTERM', async () => {
  console.log('SIGTERM signal received.');
  await optimizely.close()
  process.exit(0)
});