Set up a notification listener using the Python SDK
How to set up and remove notification listeners for the Optimizely Feature Experimentation Python 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.
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.
Note
The follow code examples assumes you have already initialized the Python SDK and have already created an
optimizely_client
instance.
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 DECISION NOTIFICATION LISTENER
#######################################
def on_decision(decision_type, user_id, attributes, decision_info):
# Add a DECISION Notification Listener for type FLAG
if decision_type == 'flag':
# Access information about feature flag, for example, key and enabled status
print(decision_info.get('flag_key'))
print(decision_info.get('enabled'))
print(decision_info.get('decision_event_dispatched'))
#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
# You will get notifications whenever the datafile is updated except for SDK initialization
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)
Updated about 1 year ago