Optimizely will be sunsetting Full Stack Experimentation on July 29, 2024. See the recommended Feature Experimentation migration timeline and documentation.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySumbit a ticketLog In
GitHubNuGetDev CommunitySumbit a ticket

Set up notification listener

This topic describes how to set up and remove notification listeners.

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.

For code samples, see the following sections.

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 := &OptimizelyFactory{
  SDKKey: "[SDK_KEY_HERE]",
}
optimizelyClient, _ := optimizelyFactory.Client()

// SET UP DECISION NOTIFICATION LISTENER

onDecision := func(notification notification.DecisionNotification) {
  // Add a DECISION Notification Listener for type FEATURE
  if string(notification.Type) == "feature" {
    // Access information about feature, for example, key and enabled status
    fmt.Print(notification.DecisionInfo["featureKey"])
    fmt.Print(notification.DecisionInfo["featureEnabled"])
    fmt.Print(notification.DecisionInfo["source"])
    // Send data to analytics provider here
  }
}
notificationID, _ := optimizelyClient.DecisionService.OnDecision(onDecision)

// 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, _ = optimizelyClient.EventProcessor.OnEventDispatch(onLogEvent)

// REMOVE LOG EVENT NOTIFICATION LISTENER

optimizelyClient.EventProcessor.RemoveOnEventDispatch(notificationID)

// SET UP OPTIMIZELY CONFIG NOTIFICATION LISTENER

// listen to OPTIMIZELY_CONFIG_UPDATE to get updated data
onConfigUpdate := func(notification notification.ProjectConfigUpdateNotification) {
}
notificationID, _ = optimizelyClient.ConfigManager.OnProjectConfigUpdate(onConfigUpdate)

// 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, _ = optimizelyClient.OnTrack(onTrack)

// REMOVE TRACK LISTENER

optimizelyClient.RemoveOnTrack(notificationID)