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 React Native SDK v4+

How the Optimizely Feature Experimentation React Native SDK uses the event processor to batch impressions and conversion events into a single payload before sending it to Optimizely.

The React Native 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 Native 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.

📘

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

FactoryUse case
createBatchEventProcessor({ batchSize?, flushInterval? })Batches events before dispatching.
createForwardingEventProcessor()Forwards each event immediately (no batching).

The following table defines the batch event processor options:

OptionTypeDefaultDescription
batchSizenumber10Maximum number of events to hold in the queue before flushing.
flushIntervalnumber1000 msMaximum duration in milliseconds that an event can exist in the queue before being flushed.
❗️

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

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.

Unlike browser environments, React Native does not have a pagehide event, so close() is not called automatically. You should call optimizely.close() in your application lifecycle, for example by listening to the AppState change event and calling close() when the app transitions to the background or is about to be terminated:

import { AppState } from 'react-native';

AppState.addEventListener('change', (nextAppState) => {
  if (nextAppState === 'background' || nextAppState === 'inactive') {
    optimizely.close();
  }
});