Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Set up a notification listener using the C# SDK

How to set up and remove notification listeners for the Optimizely Feature Experimentation C# 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.

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 NotificationType = OptimizelySDK.Notifications.NotificationCenter.NotificationType;

var datafile = "YOUR_JSON_DATAFILE";
var optimizely = new Optimizely(datafile);

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

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

// Clear all notifications
optimizely.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

var datafile = "YOUR_JSON_DATAFILE";
var optimizely = new Optimizely(datafile);

/*
 * Set up decision notification listener to get notified of any decision event
 */
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/register the notification listener using the notificationId to remove the listener later
var notificationId =
    optimizely.NotificationCenter.AddNotification(NotificationType.Decision, OnDecision);

/*
 * Set up log event notification listener to get notified of any log events
 */
NotificationCenter.LogEventCallback OnLogEvent = (logEvent) =>
{
    // process the logEvent object here (send to analytics provider, audit/inspect data)
};

// Add/register the notification listener using the notificationId to remove the listener later
var notificationId =
    optimizely.NotificationCenter.AddNotification(NotificationType.LogEvent, OnLogEvent);

/*
 * Set up config update notification listener to get notified of any datafile updates
 * except during SDK initialization
 */
NotificationCenter.OptimizelyConfigUpdateCallback OnConfigUpdate = () =>
{
    // Use the optimizely instance to get the updated config
    var optimizelyConfig = optimizely.GetOptimizelyConfig();
};

// Add/register the notification listener using the notificationId to remove the listener later
var notificationId =
    optimizely.NotificationCenter.AddNotification(NotificationType.OptimizelyConfigUpdate,
        OnConfigUpdate);

/*
 * Set up track notification listener to get notified of any track event calls
 */
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/register the notification listener using the notificationId to remove the listener later
var notificationId = optimizely.NotificationCenter.AddNotification(NotificationType.Track, OnTrack);