Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Set up a notification listener using the Swift SDK

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

πŸ“˜

Note

The follow code examples assume you have already initialized the Swift SDK and have created an optimizelyClient instance.

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 DECISION NOTIFICATION LISTENER
let onDecision : DecisionListener = {[weak self](decisionType, userId, attributes, decisionInfo) in
    // Add a DECISION Notification Listener for type FLAG
    if decisionType == "flag" {
       // Access information about feature, for example, key and enabled status
       print(decisionInfo["flagKey"])
       print(decisionInfo["enabled"])
       print(decisionInfo["decisionEventDispatched"])
     }
}
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
// You will get notifications whenever the datafile is updated except for SDK initialization
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)
}];