Event batching for the React SDK v4+
How the Optimizely Feature Experimentation 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 impression and conversion events tracked.
Minimum SDK version
v4.0.0+
For versions 3.x and earlier, see React SDK prior to v4.
In v4, event processing is opt-in. If no eventProcessor is passed to createInstance, no events are dispatched. You must explicitly create an event processor using a factory function.
NoteEvent 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
Use createBatchEventProcessor to create a batch event processor and pass it to createInstance. Events are held in a queue until either:
- 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. - A new datafile revision is received.
import {
createInstance,
createPollingProjectConfigManager,
createBatchEventProcessor,
} from '@optimizely/react-sdk';
const optimizely = createInstance({
projectConfigManager: createPollingProjectConfigManager({
sdkKey: 'YOUR_SDK_KEY',
}),
eventProcessor: createBatchEventProcessor({
batchSize: 100,
flushInterval: 3000,
}),
});Event processor factories
| Factory | Use case |
|---|---|
createBatchEventProcessor({ batchSize?, flushInterval? }) | Batches events before dispatching. |
createForwardingEventProcessor() | Forwards each event immediately (no batching). |
The following table defines the batch event processor options:
| Option | Type | Default (browser) | Description |
|---|---|---|---|
batchSize | number | 10 | Maximum number of events to hold in the queue before flushing. |
flushInterval | number | 1000 ms | Maximum duration in milliseconds that an event can exist in the queue before being flushed. |
WarningThe 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 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.
Disable event dispatching
To disable sending all events to Optimizely's results backend, simply omit the eventProcessor parameter when calling createInstance. In v4, this is the default behavior — no events are dispatched unless you explicitly opt in.
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 data loss.
For SSR per-request instances, you can set disposable: true in createInstance so the instance can be garbage collected without explicitly calling close().
WarningBecause 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.
On the browser side, optimizely.close() is automatically connected to the pagehide event, so there is no need to do any manual instrumentation.
Updated 25 days ago
