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 GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

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.

// Add a notification listener
let notificationId = optimizely.notificationCenter.addDecisionNotificationListener(decisionListener: { (type, userId, attributes, decisionInfo) in    
    // Send data to analytics provider here
})  

// Remove a specific listener
optimizely.notificationCenter.removeNotificationListener(notificationId: notificationId!)

// Remove all Notification Listeners
optimizely.notificationCenter.clearAllNotificationListeners()  

// Remove all Notification Listeners of a certain type
optimizely.notificationCenter.clearNotificationListeners(type: .decision)
// Add a notification listener
NSNumber *notificationId = [self.optimizely.notificationCenter addDecisionNotificationListenerWithDecisionListener:^(NSString *type, NSString *userId, NSDictionary<NSString *,id> *attributes, NSDictionary<NSString *,id> *decisionInfo) {
    // Send data to analytics provider here
}];

// Remove a specific listener
[optimizely.notificationCenter removeNotificationListenerWithNotificationId:notificationId];

// Remove all Notification Listeners
[optimizely.notificationCenter clearAllNotificationListeners];

// Remove all Notification Listeners of a certain type
[optimizely.notificationCenter clearNotificationListenersWithType:NotificationTypeDecision];

Set up each type of notification listener

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

// SET UP ACTIVATE NOTIFICATION LISTENER
        
let onActivate : ActivateListener = {[weak self] (experiment, userId, attributes, variation, event) in
}
self.optimizelyClient?.notificationCenter?.addActivateNotificationListener(activateListener: onActivate)
        
// SET UP DECISION NOTIFICATION LISTENER
        
let onDecision : DecisionListener = {[weak self](decisionType, userId, attributes, decisionInfo) in
    // Add a DECISION Notification Listener for type FEATURE
    if decisionType == "feature" {
       // Access information about feature, for example, key and enabled status
       print(decisionInfo["featureKey"])
       print(decisionInfo["featureEnabled"])
       print(decisionInfo["source"])
     }
}
self.optimizelyClient?.notificationCenter?.addDecisionNotificationListener(decisionListener: onDecision)
        
// SET UP LOG EVENT NOTIFICATION LISTENER
        
let onLogEvent : LogEventListener = {[weak self] (url, logEvent) in
    // process the logEvent object here (send to analytics provider, audit/inspect data)
}
self.optimizelyClient?.notificationCenter?.addLogEventNotificationListener(logEventListener: onLogEvent)
        
// SET UP DATAFILE CHANGE NOTIFICATION LISTENER
        
let onDatafileUpdate : DatafileChangeListener = {[weak self] (datafile) in
}
self.optimizelyClient?.notificationCenter?.addDatafileChangeNotificationListener(datafileListener: onDatafileUpdate)
        
// SET UP TRACK LISTENER
        
let onTrack : TrackListener = { [weak self](eventKey, userId, attributes, eventTags, event) in
    // process the event here (send to analytics provider, audit/inspect data)
}
self.optimizelyClient?.notificationCenter?.addTrackNotificationListener(trackListener: onTrack)
// SET UP ACTIVATE NOTIFICATION LISTENER
    
(void)[self.optimizelyClient.notificationCenter addActivateNotificationListenerWithActivateListener:^(NSDictionary<NSString *,id> * _Nonnull experiment, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nonnull variation, NSDictionary<NSString *,id> * _Nonnull event) {
}];

// SET UP DECISION NOTIFICATION LISTENER
    
(void)[self.optimizelyClient.notificationCenter addDecisionNotificationListenerWithDecisionListener: ^(NSString * _Nonnull decisionType, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nonnull decisionInfo) {
    // Add a DECISION Notification Listener for type FEATURE
    if ([decisionType isEqualToString:@"feature"]) {
        // Access information about feature, for example, key and enabled status
        NSLog(decisionInfo[@"featureKey"]);
        NSLog(decisionInfo[@"featureEnabled"]);
        NSLog(decisionInfo[@"source"]);
    }
}];

// SET UP LOG EVENT NOTIFICATION LISTENER
    
(void)[self.optimizelyClient.notificationCenter addLogEventNotificationListenerWithLogEventListener:^(NSString * _Nonnull url, NSDictionary<NSString *,id> * _Nonnull logEvent) {
    // process the logEvent object here (send to analytics provider, audit/inspect data)
}];

// SET UP DATAFILE CHANGE NOTIFICATION LISTENER
    
(void)[self.optimizelyClient.notificationCenter addDatafileChangeNotificationListenerWithDatafileListener:^(NSData * _Nonnull datafile) {
}];
     
// SET UP TRACK LISTENER
    
(void)[self.optimizelyClient.notificationCenter addTrackNotificationListenerWithTrackListener:^(NSString * _Nonnull eventKey, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nullable eventTags, NSDictionary<NSString *,id> * _Nonnull event) {
    // process the event here (send to analytics provider, audit/inspect data)
}];