Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In

Event batching

This topic describes how the Optimizely Feature Experimentation C# SDK uses the event processor to batch decisions and conversion events into a single payload before sending it.

The Optimizely Feature Experimentation C# SDK batches decision and conversion events into a single payload before sending it to Optimizely. This is achieved through a new SDK component called the event processor.

Event batching can reduce the number of outbound requests to Optimizely Feature Experimentation depending on how you define, configure, and use the event processor. It means less network traffic for the same number of Impression and conversion events tracked.

In the C# SDK, BatchEventProcessor provides the implementation of the EventProcessor interface and batches events. You can control batching based on two parameters:

  • Batch size: Defines the number of events that are batched together before sending to Optimizely Feature Experimentation.
  • Flush interval: Defines the amount of time after which any batched events should be sent to Optimizely Feature Experimentation.

An event consisting of the batched payload is sent as soon as the batch size reaches the specified limit or the flush interval reaches the specified time limit. BatchEventProcessor options are described in more detail below.

📘

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

Basic example

using OptimizelySDK;

class App
{
    static void Main(string[] args)
    {
        string sdkKey = args[0];
        // Returns Optimizely Client
        OptimizelyFactory.NewDefaultInstance(sdkKey);
    }
}

By default, batch size is 10 and flush interval is 30 seconds.

Advanced Example

using OptimizelySDK;

class App
{
    static void Main(string[] args)
    {
        string sdkKey = args[0];
        ProjectConfigManager projectConfigManager = HttpProjectConfigManager.builder()
        .WithSdkKey(sdkKey)
        .Build();

        BatchEventProcessor batchEventProcessor = new BatchEventProcessor.Builder()
            .WithMaxBatchSize(10)
            .WithFlushInterval(TimeSpan.FromSeconds(30))
            .Build();

        Optimizely optimizely = new Optimizely(
             projectConfigManager,
                ..  // Other Params
             ..batchEventProcessor
           );
    }
}

BatchEventProcessor

BatchEventProcessor is an implementation of EventProcessor where events are batched. The class maintains a single consumer thread that pulls events off of the BlockingCollection and buffers them for either a configured batch size or a maximum duration before the resulting LogEvent is sent to the EventDispatcher and NotificationCenter.

The following properties can be used to customize the BatchEventProcessor configuration using the Builder class

PropertyDefault valueDescription
EventDispatcherDefautEventDispatcherUsed to dispatch event payload to Optimizely Feature Experimentation.
BatchSize10The maximum number of events to batch before dispatching. Once this number is reached, all queued events are flushed and sent to Optimizely Feature Experimentation.
FlushInterval30000 (30 Seconds)Milliseconds to wait before batching and dispatching events.
EventQueue1000BlockingCollection that queues individual events to be batched and dispatched by the executor.
NotificationCenternullNotification center instance to be used to trigger any notifications.

For more information, see Initialize SDK.

Side effects

The table lists other Optimizely Feature Experimentation functionality that may be triggered by using this class.

FunctionalityDescription
LogEvent (see following section)Whenever the event processor produces a batch of events, a LogEvent object will be created using the EventFactory.
It contains 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.
Notification ListenersFlush invokes the LOGEVENT notification listener if this listener is subscribed to.

Registering LogEvent listener

To register a LogEvent listener:

NotificationCenter.AddNotification(
                                        NotificationType.LogEvent, 
                      new LogEventCallback((logevent) => {
                            // Your code here
                                })
                  );

LogEvent

LogEvent object gets created using EventFactory.It represents the batch of decision and conversion events we send to the Optimizely Feature Experimentation backend.

ObjectTypeDescription
Url
Required
string URL to dispatch log event to.
Params
Required
Dictionary<string, object>Parameters to be set in the log event. It contains EventBatch of all UserEvents inside Visitors.
HttpVerb
Required
stringThe HTTP verb to use when dispatching the log event. It can be GET or POST.
Headers Dictionary<string, string> HeadersHeaders to be set when sending the request.

Dispose Optimizely Feature Experimentation on application exit

If you enable event batching, you must call the Close method (optimizely.Dispose()) before exiting. This ensures that queued events are flushed as soon as possible to avoid data loss.

Warning

Because the Optimizely Feature Experimentation client maintains a buffer of queued events, you must call Dispose() on the instance before shutting down your application or whenever dereferencing the instance.

MethodDescription
Dispose()Stops all timers and flushes the event queue. This method will also stop any timers that are happening for the data-file manager.