Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Event batching for the JavaScript SDK v6

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

❗️

Warning

This content covers the Feature Experimentation JavaScript (Browser) SDK v6 features currently in pre-production testing and is subject to change before release

For the latest released version, see JavaScript (Browser) SDK.

The Optimizely Feature Experimentation Javascript (Browser) 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.

Minimum SDK version

v6.0.0+

📘

Note

In order to enable "Event Batching" it is required to use "Batch Event Processor" in Javascript SDK version 6 or newer.

Event batching works with the default and custom event dispatchers.

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

Basic example

Optimizely provides two options to configure event batching: batchSize and flushInterval. You can pass in both options during eventProcessor creation. Events are held in a queue until the following:

  • The number of events reaches the defined batchSize.

  • The oldest event has been in the queue for longer than the defined flushInterval, 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.

import { createInstance } from '@optimizely/optimizely-sdk';

const SDK_KEY="YOUR_SDK_KEY";

const pollingConfigManager = createPollingProjectConfigManager({
  sdkKey: SDK_KEY,
});

const batchEventProcessor = createBatchEventProcessor({
  batchSize: 100,
  flushInterval: 5000
});

const optimizelyClient = createInstance({
  projectConfigManager: pollingConfigManager,
  eventProcessor: batchEventProcessor 
});

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

❗️

Warning

The maximum payload size is 3.5 MB. Optimizely rejects requests with a 400 response code, Bad Request Error, if the batch payload exceeds this limit.

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 exceed the size limit, try configuring a smaller batch size.

The following table defines these options and lists general recommendations. On the browser you should use 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
batchSizeMaximum size of batch of events to send to Optimizely Feature Experimentation. Default: 1010
flushIntervalThe maximum duration in milliseconds that an event can exist in the queue before being flushed.
Default: 1000
1000

For more information, see Initialize the JavaScript (Browser) SDK.

Side effects

The following table 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 is dispatched using the provided event dispatcher and it is sent to the notification subscribers.
Notification ListenersLOG_EVENT notification listeners are triggered when a LogEvent is passed to the event dispatcher

Register a LogEvent listener

To register a LogEvent notification listener see the following code example:

// Using notificationCenter to register logEvent listener
import { NOTIFICATION_TYPES } from '@optimizely/optimizely-sdk'

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

LogEvent

LogEvent represents the batch of decision and conversion events sent to the Optimizely Feature Experimentation backend.

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

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