---
title: Event batching for the JavaScript SDK v6+
slug: feature-experimentation/docs/event-batching-for-the-javascript-sdk
docTags: 
createdAt: 2026-08-05T20:39:59.000Z
---

The Optimizely Feature Experimentation 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.

## Minimum SDK version

v6.0.0+

For versions 5.3.5 and below, see [JavaScript (Browser) SDK](docId\:zoGMl_rkjugXFMimT6fFu) or [JavaScript (Node) SDK](docId\:Tq8vbUUzK26SSvlL34DWu). See the [SDK compatibility matrix](docId:2shbZfPCkPAZ0mlZpL1TU) documentation for a list of current SDK releases and the features they support.

:::hint{type="info"}
**Note**
To enable event batching, you must use batch event processor in the JavaScript SDK version 6.0.0 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 in a single network request.
- A new datafile revision is received. This occurs only when live datafile updates are enabled.

```javascript
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 for browser and React Native, and 30 seconds for NodeJS.

:::hint{type="danger"}
**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](docId:822ZQOB6Zk2-kWJKavBvA), which the SDK 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, because some events could be lost if a user immediately bounces, while gaining network efficiency in cases where many decisions calls happen back-to-back.

| Name              | Description                                                                                                                                         | Recommended Value                    |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ |
| **batchSize**     | Maximum size of batch of events to send to Optimizely. Default: 10                                                                                  | 10                                   |
| **flushInterval** | The maximum duration in milliseconds that an event can exist in the queue before being flushed.\ Default: 1000 (Browser/React Native), 30000 (Node) | 1000 (For Browser)\ 30000 (For Node) |

For information, see [Initialize the JavaScript SDK](docId:2bQ4KAFcVOAesqDOcmtCZ).

## Side effects

The following table describes other functionality related to event batching.

| Functionality              | Description                                                                                                                                                                                                                                                                                      |
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **LogEvent**               | Whenever 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 configured event dispatcher and it is sent to the notification subscribers. |
| **Notification Listeners** | LOG\\\_EVENT \[notification listeners]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/set-up-a-notification-listener-using-the-javascript-sdk-v6) 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:

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

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

### `LogEvent`

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

| Parameter    | Description                                          |
| ------------ | ---------------------------------------------------- |
| **url**      | URL to dispatch log event to.                        |
| **httpVerb** | The HTTP verb to use when dispatching the log event. |
| **params**   | Contains the event batch.                            |

## Close Optimizely 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 when possible to avoid any data loss.

:::hint{type="info"}
**Warning**
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.
:::

| Method      | Description                                                                                                                                   |
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| **close()** | Stops all timers and flushes the event queue. This method also stops any pending timers or in-flight requests related to datafile management. |

On the browser side, `optimizely.close()` is automatically connected to the [pagehide event](https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event), so there is no need to do any manual instrumentation.
