Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In

Event batching

This topic describes how the Optimizely React SDK uses the event processor to batch impressions and conversion events into a single payload before sending it to Optimizely.

The React SDK lets you batch events and includes options to set a maximum batch size and flush interval timeout. The benefit of event batching is less network traffic for the same number of Impressions and conversion events tracked.

The event batching functionality works by processing impression events from OptimizelyExperiment or OptimizelyFeature, and conversion events from calling track on the client instance, placing them into a queue. The queue is drained when it either reaches its maximum size limit or when the flush interval is triggered.

By default, event batching is enabled in React SDK.

📘

Note

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 aren't 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 in a single network request.
  • A new datafile revision is received. This occurs only when live datafile updates are enabled.
import { createInstance } from '@optimizely/react-sdk';

const optimizely = createInstance({
  datafile: window.datafile // assuming you have a datafile at window.datafile
  // other options
  eventBatchSize: 100,
  eventFlushInterval: 3000,
});
const optimizelySdk = require('@optimizely/optimizely-sdk')

optimizelySdk.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 events are generated back-to-back.

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

NameDescriptionBrowserServer
eventBatchSize The maximum number of events to hold in the queue. Once this number is reached, all queued events are flushed and sent to Optimizely.
Default: 10

Note: By setting this value to 1, events aren't batched and event requests behave exactly as in pre-3.3.0 JavaScript and Node SDKs.
10Based on your organization's requirements.
eventFlushInterval The maximum duration in milliseconds that an event can exist in the queue before being flushed.
Default: 1000 ms in the browser, 30000 ms in Node.js
1000Based on your organization's requirements.

📘

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.

Close Optimizely on application exit

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

Important

Because the Optimizely client maintains a buffer of queued events, you must call close() on the Optimizely 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 that are happening for the datafile manager.

Note: We recommend that you connect this method to a kill signal for the running process.

On the browser side, optimizely.close() is automatically connected to the pagehide event, so there is no need to do any manual instrumentation.