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.
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 ACTIVATE NOTIFICATION LISTENER
**************************************/
NotificationCenter.ActivateCallback OnActivate = (Experiment experiment, string userId, UserAttributes userAttributes, Variation variation, LogEvent logEvent) =>
{
// Send data to analytics provider here
};
// Add notification listener
int notificationId = optimizely.NotificationCenter.AddNotification(NotificationType.Activate, OnActivate);
/**************************************
* SET UP DECISION NOTIFICATION LISTENER
**************************************/
NotificationCenter.DecisionCallback OnDecision = (type, userId, userAttributes, decisionInfo) =>
{
// Access type on decisionObject to get type of decision
if (type == "feature")
{
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
// You will get notification whenever the datafile is update except for SDK initialization
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);
Updated over 1 year ago