HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

Event batching

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

The Optimizely Full Stack Javascript 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 Impression and conversion events tracked.

By default, event batching is enabled in JavaScript SDK version 3.3.0 or newer.

πŸ“˜

Note

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

The event batching process doesn't remove any personally identifiable information (PII) from events. You must still ensure that you aren't sending any unnecessary PII to Optimizely.

Basic example

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.

let optimizelySdk = require('@optimizely/optimizely-sdk');

optimizelySdk.createInstance({
  // other options
  eventBatchSize: 100,
  eventFlushInterval: 3000,
})

By default, batch size is 10 and flush interval is 1 second.

The table below defines these options and lists general recommendations. 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.

NameDescriptionRecommend Value
eventBatchSizeMaximum size of batch of events to send to Optimizely. Default: 1010
eventFlushIntervalThe maximum duration in milliseconds that an event can exist in the queue before being flushed.
Default: 1000
1000

For more information, see Initialize SDK.

Side effects

The table below describes other functionality related to event batching.

FunctionalityDescription
LogEventWhenever the event processor produces a batch of events to be dispatched, a LogEvent notification object is created, which contains
the batch of conversion and decision events.
This object will be dispatched using the provided event dispatcher and also it will be sent to the notification subscribers (see below).
Notification ListenersLOG_EVENT notification listeners are triggered when a LogEvent is passed to the event dispatcher

Registering LogEvent listener

To register a LogEvent notification listener

// Using notificationCenter to register logEvent listener

optlimizely.notificationCenter
  .addNotificationListener(optimizelyEnums.NOTIFICATION_TYPES.LOG_EVENT, (logEvent) => {});

LogEvent

It represents the batch of decision and conversion events we send to the Optimizely backend.

ParameterDescription
urlURL to dispatch log event to
httpVerbThe HTTP verb to use when dispatching the log event
paramsContains the event batch

Close Optimizely on application exit

If you enable event batching, it's 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 pending timers or in-flight requests related to datafile management.

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