GuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In

Set up notification listener

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.

Add and remove all notification listeners

The example code below shows how to add a listener, remove a listener, remove all listeners of a specific type (such as all decision listeners), and remove all listeners.

using OptimizelySDK;
using OptimizelySDK.Entity;
using OptimizelySDK.Event;
using OptimizelySDK.Notifications;
using NotificationType = OptimizelySDK.Notifications.NotificationCenter.NotificationType;

var optimizelyClient = new Optimizely(datafile);

// Remove notification listener
optimizelyClient.NotificationCenter.RemoveNotification(notificationId);

// Clear all notification listeners of a certain type
optimizelyClient.NotificationCenter.ClearNotifications(NotificationType.Decision);

// Clear all notifications
optimizelyClient.NotificationCenter.ClearAllNotifications();

Set up each type of notification listener

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

using OptimizelySDK;
using OptimizelySDK.Entity;
using OptimizelySDK.Event;
using OptimizelySDK.Notifications;
using NotificationType = OptimizelySDK.Notifications.NotificationCenter.NotificationType;

// import your third-party analytics integration here



/**************************************
* SET UP DECISION NOTIFICATION LISTENER
**************************************/

NotificationCenter.DecisionCallback OnDecision = (type, userId, userAttributes, decisionInfo) =>
{
  // Access type on decisionObject to get type of decision
  if (type == "flag")
  {
    var serializedJsonInfo = Newtonsoft.Json.JsonConvert.SerializeObject(decisionInfo);
    Console.WriteLine($"Feature flag access related information: {serializedJsonInfo}");
    // Send data to analytics provider here
  }
};

// Add notification listener
int notificationId = optimizely.NotificationCenter.AddNotification(NotificationType.Decision, OnDecision);

/**************************************
* SET UP LOG EVENT NOTIFICATION LISTENER
**************************************/

NotificationCenter.LogEventCallback OnLogEvent= (logEvent) => 
{
  // process the logEvent object here (send to analytics provider, audit/inspect data)
};

// Add notification listener
int notificationId = optimizely.NotificationCenter.AddNotification(NotificationType.LogEvent, OnLogEvent);

/**************************************
* SET UP OPTIMIZELY CONFIG NOTIFICATION LISTENER
**************************************/

// listen to OPTIMIZELY_CONFIG_UPDATE to get updated data
NotificationCenter.OptimizelyConfigUpdateCallback OnConfigUpdate = () => 
{
  OptimizelyConfig optimizelyConfig = optimizely.GetOptimizelyConfig();
};

// Add notification listener
int notificationId = optimizely.NotificationCenter.AddNotification(NotificationType.OptimizelyConfigUpdate, OnConfigUpdate);

/**************************************
* SET UP TRACK LISTENER
**************************************/

NotificationCenter.TrackCallback OnTrack = (string eventKey, string userId, UserAttributes userAttributes, EventTags eventTags, LogEvent logEvent) =>
{
  // process the event here (send to analytics provider, audit/inspect data)
};

// Add notification listener
int notificationId = optimizely.NotificationCenter.AddNotification(NotificationType.Track, OnTrack);

Did this page help you?