Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Configure the Swift SDK event dispatcher

How to configure the event dispatcher for HTTP requests made from every impression or conversion in the Optimizely Feature Experimentation Swift SDK.

The Optimizely Feature Experimentation SDKs make HTTP requests for every decision event or conversion event that gets triggered. Each SDK has a built-in event dispatcher for handling these events, but Optimizely recommends overriding it based on the specifics of your environment.

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

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

The event dispatcher should implement a dispatchEvent function, which takes in three arguments:

  • httpVerb
  • url
  • params

These arguments are created by the internal EventBuilder class. In this function, you should send a POST request to the given url using the params as the body of the request (be sure to stringify it to JSON) and {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 Feature Experimentation. Modifying this payload will alter your results.

Source files

The included event handler implementation for the Swift SDK is available on GitHub.