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 GuideGitHubNuGetDev CommunitySumbit a ticketLog In
GitHubNuGetDev CommunitySumbit a ticket

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.

# 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.notification_center.clear_notification_listeners(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION])

Set up each type of notification listener

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

# import your third-party analytics integration here

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

def on_activate(experiment, user_id, attributes, variation, _event)
end

callback_reference = lambda do |*args|
    on_activate(*args)
end

optimizely_client.notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE], callback_reference)


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

def on_decision(type, user_id, attributes, decision_info)
end

callback_reference = lambda do |*args|
    on_decision(*args)
end

optimizely_client.notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], callback_reference)


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

def on_log_event(log_event)
end

callback_reference = lambda do |*args|
    on_log_event(*args)
end

optimizely_client.notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT], callback_reference)

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

# listen to OPTIMIZELY_CONFIG_UPDATE to get updated data
def on_config_update
end

callback_reference = lambda do |*args|
    on_config_update(*args)
end

optimizely_client.notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:OPTIMIZELY_CONFIG_UPDATE], callback_reference)

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

def on_track(event_key, user_id, attributes, event_tags, _event)
end

callback_reference = lambda do |*args|
    on_track(*args)
end

optimizely_client.notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK], callback_reference)