HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

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.

from optimizely.helpers import enums

# Remove Notification Listener
optimizely_client.notification_center.remove_notification_listener(notification_id)

# Remove all Notification Listeners
optimizely_client.notification_center.clear_all_notification_listeners()

# Remove all Notification Listeners of a certain type
optimizely_client.clear_notification_listeners(enums.NotificationTypes.DECISION);

Set up each type of notification listener

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

from optimizely.helpers import enums
#import your third-party analytics integration here

#######################################
# SET UP ACTIVATE NOTIFICATION LISTENER
#######################################

def on_activate(experiment, user_id, attributes, variation, event):
    #Send data to analytics provider here

 
optimizely_client.notification_center.add_notification_listener(enums.NotificationTypes.ACTIVATE, on_activate)


#######################################
# SET UP DECISION NOTIFICATION LISTENER
#######################################

def on_decision(decision_type, user_id, attributes, decision_info):
    # Add a DECISION Notification Listener for type FEATURE
    if decision_type == 'feature':
        # Access information about feature, for example, key and enabled status
        print decision_info.get('feature_key')
        print decision_info.get('feature_enabled')
        print decision_info.get('source')      
        #Send data to analytics provider here
  
notification_id = optimizely_client.notification_center.add_notification_listener(
enums.NotificationTypes.DECISION, on_decision)


#######################################
# SET UP LOG EVENT NOTIFICATION LISTENER
#######################################

def on_log_event(logEvent): 
    #process the logEvent object here (send to analytics provider, audit/inspect data)

optimizely_client.notification_center.add_notification_listener(enums.NotificationTypes.LOG_EVENT, on_log_event)

#######################################
# SET UP OPTIMIZELY CONFIG NOTIFICATION LISTENER
#######################################

# listen to OPTIMIZELY_CONFIG_UPDATE to get updated data
def on_config_update_listener(*args):
    config = optimizely_client.get_optimizely_config()

optimizely_client.notification_center.add_notification_listener(
    enums.NotificationTypes.OPTIMIZELY_CONFIG_UPDATE, on_config_update_listener)

#######################################
# SET UP TRACK LISTENER
#######################################

def on_track(event):    
    #process the event here (send to analytics provider, audit/inspect data)
  
optimizely_client.notification_center.add_notification_listener(
    enums.NotificationTypes.TRACK, on_track)

Did this page help you?