Configure the Swift SDK event dispatcher
The Optimizely Feature Experimentation SDKs make HTTP requests for every decision event or conversion eventdecision event or conversion event that gets triggered. Each SDK has a built-in event dispatcher for handling these events, but You should overriding it based on the specifics of your environment.
You should customizing the event dispatcher you use in production to ensure that you queue and send events in a manner that scales to the volumes handled by your application. You can build your dispatcher from scratch or start with the provided dispatcher.
Unlike most other SDKs, the provided dispatcher for Swift includes event queueing, event batching, and flushing. Events will be queued if an app is offline and will be forwarded to the server when the app is online again.
On watchOS, the SDK cannot observe app lifecycle changes. Call WatchBackgroundNotifier.applicationDidEnterBackground() and WatchBackgroundNotifier.applicationDidBecomeActive() from your extension delegate. The SDK then flushes queued events when the app moves to the background.
The examples show that to customize the event dispatcher, initialize the Optimizely client (or manager) with an event dispatcher instance.
// create OptimizelyClient with a custom EventDispatcher
let customEventDispatcher = CustomEventDispatcher()
// or you can simply adjust parameters of DefaultEventDispatcher to disable batching (timerInterval=0), etc
let customEventDispatcher = DefaultEventDispatcher(timerInterval: 0)
optimizely = OptimizelyClient(sdkKey: "<Your_SDK_Key>",
eventDispatcher: customEventDispatcher)// create OptimizelyClient with a custom EventDispatcher
CustomEventDispatcher *customEventDispatcher = [[CustomEventDispatcher alloc] init];
self.optimizely = [[OptimizelyClient alloc] initWithSdkKey:@"<Your_SDK_Key>"
logger:nil
eventDispatcher:customEventDispatcher
userProfileService:nil
periodicDownloadInterval:@(5*60)
defaultLogLevel:OptimizelyLogLevelInfo];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 APIOptimizely 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.
Configure the default event dispatcher
You can adjust the provided DefaultEventDispatcher with these initializer parameters:
Parameter | Type | Default | Description |
|---|---|---|---|
`batchSize` | `Int` | `10` | Maximum number of events combined into one batch. Set to `1` to send each event individually. Values of `0` or less use the default. |
`backingStore` | `DataStoreType` | `.file` | Where queued events are stored: `.file`, `.memory`, or `.userDefaults`. |
`dataStoreName` | `String` | `"OPTEventQueue"` | Name of the event queue store. |
`timerInterval` | `TimeInterval` | `60` (seconds) | Interval for flushing batched events. Set to `0` to disable batching and send events immediately. Negative values use the default. |
`maxQueueSize` | `Int` | `10000` | Maximum number of events held in the queue. Values below `100` use the default. If the value is smaller than `batchSize`, the SDK raises it to `batchSize`. |
Implement a custom event dispatcher
The event dispatcher must conform to the OPTEventDispatcher protocol and implement these functions:
- dispatchEvent(event: EventForDispatch, completionHandler: DispatchCompletionHandler?)
- flushEvents()
In Objective-C, conform to the OPTEventDispatcher protocol, whose completion handler is (NSData *, NSError *).
The SDK creates EventForDispatch, which carries the target url and the JSON body of the event. In dispatchEvent, send a POST request to that url. Use body as the request body and set content-type: application/json in the headers.
Important If you are using a custom event dispatcher, do not modify the event payload returned from Optimizely. Modifying this payload will alter your results.
Source files
The included event handler implementation for the Swift SDK is available on GitHub.