Configure the Go SDK event dispatcher
How to configure the event dispatcher for HTTP requests made from every impression or conversion in the Optimizely Feature Experimentation Go 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 we recommend overriding it based on the specifics of your environment.
The Go SDK has an out-of-the-box asynchronous dispatcher. We recommend 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. Customizing the event dispatcher allows you to take advantage of features like batching, which makes it easier to handle large event volumes efficiently or to implement retry logic when a request fails. You can build your dispatcher from scratch or start with the provided dispatcher.
The examples show that to customize the event dispatcher, initialize the Optimizely client (or manager) with an event dispatcher instance.
import "github.com/optimizely/go-sdk/pkg/event" // for v2: "github.com/optimizely/go-sdk/v2/pkg/event"
type CustomEventDispatcher struct {
}
// DispatchEvent dispatches event with callback
func (d *CustomEventDispatcher) DispatchEvent(event event.LogEvent) (bool, error) {
dispatchedEvent := map[string]interface{}{
"url": event.EndPoint,
"http_verb": "POST",
"headers": map[string]string{"Content-Type": "application/json"},
"params": event.Event,
}
return true, nil
}
import (
"github.com/optimizely/go-sdk/pkg/client" // for v2: "github.com/optimizely/go-sdk/v2/pkg/client"
"github.com/optimizely/go-sdk/pkg/event" // for v2: "github.com/optimizely/go-sdk/v2/pkg/event"
)
optimizelyFactory := &client.OptimizelyFactory{
SDKKey: "SDK_KEY_HERE",
}
customEventDispatcher := &CustomEventDispatcher{}
// Create an Optimizely client with the custom event dispatcher
optlyClient, e := optimizelyFactory.Client(client.WithEventDispatcher(customEventDispatcher))
The event dispatcher should implement a DispatchEvent
function, which takes in one argument: event.LogEvent
. In this function, you should send a POST
request to the given event.EndPoint
using the event.EndPoint
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.
By default, our Go SDK uses a basic asynchronous event dispatcher.
Updated 9 months ago