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

Set up a notification listener using the Go SDK

How to set up and remove notification listeners for the Optimizely Feature Experimentation Go SDK.

Notification listeners trigger a callback function that you define when certain actions are triggered in the SDK.

The most common use case is to send a stream of all feature flag decisions to an analytics provider or to an internal data warehouse to join it with other data that you have about your users.

Notification listener types

For more information about notification listener types and use cases, see Notification listeners.

Set up each type of notification listener

The example code below shows how to set up and remove each type of notification listener.

// Create default Client
optimizelyFactory := &client.OptimizelyFactory{
  SDKKey: "[SDK_KEY_HERE]",
}
optimizelyClient, err := optimizelyFactory.Client()
if err != nil {
	// handle error
}


// SET UP DECISION NOTIFICATION LISTENER

onDecision := func(notification notification.DecisionNotification) {
  // Add a DECISION Notification Listener for type FLAG
  if string(notification.Type) == "flag" {
    // Access information about feature, for example, key and enabled status
    fmt.Print(notification.DecisionInfo["flagKey"])
    fmt.Print(notification.DecisionInfo["enabled"])
    fmt.Print(notification.DecisionInfo["decisionEventDispatched"])
  }
}
notificationID, err := optimizelyClient.DecisionService.OnDecision(onDecision)
if err != nil {
	// handle error
}

// REMOVE DECISION NOTIFICATION LISTENER

optimizelyClient.DecisionService.RemoveOnDecision(notificationID)

// SET UP LOG EVENT NOTIFICATION LISTENER

onLogEvent := func(eventNotification event.LogEvent) {
  // process the logEvent object here (send to analytics provider, audit/inspect data)
}
notificationID, err = optimizelyClient.EventProcessor.OnEventDispatch(onLogEvent)
if err != nil {
	// handle error
}

// REMOVE LOG EVENT NOTIFICATION LISTENER

optimizelyClient.EventProcessor.RemoveOnEventDispatch(notificationID)

// SET UP OPTIMIZELY CONFIG NOTIFICATION LISTENER

// listen to OPTIMIZELY_CONFIG_UPDATE to get updated data
// You will get notifications whenever the datafile is updated except for SDK initialization
// you will get notifications whenever the datafile is updated, except for the SDK initialization
onConfigUpdate := func(notification notification.ProjectConfigUpdateNotification) {
}
notificationID, err = optimizelyClient.ConfigManager.OnProjectConfigUpdate(onConfigUpdate)
if err != nil {
	// handle error
}

// REMOVE OPTIMIZELY CONFIG NOTIFICATION LISTENER

optimizelyClient.ConfigManager.RemoveOnProjectConfigUpdate(notificationID)

// SET UP TRACK LISTENER

onTrack := func(eventKey string, userContext entities.UserContext, eventTags map[string]interface{}, conversionEvent event.ConversionEvent) {
  // process the event here (send to analytics provider, audit/inspect data)
}

notificationID, err = optimizelyClient.OnTrack(onTrack)
if err != nil {
	// handle error
}

// REMOVE TRACK LISTENER

optimizelyClient.RemoveOnTrack(notificationID)