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

Set up a notification listener using the Ruby SDK

How to set up and remove notification listeners for the Optimizely Feature Experimentation Ruby 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 Ruby SDK and have created an optimizely_client 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.

# 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.

# frozen_string_literal: true

# 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
  return unless decision_type == 'flag'

  # Access information about feature flag, for example, key and enabled status
  puts decision_info[:flag_key]
  puts decision_info[:enabled]
  puts decision_info[:decision_event_dispatched]
  # Send data to analytics provider here
end

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

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

def on_log_event(log_event) end

optimizely_client.notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT], method(: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
  config = optimizely_client.get_optimizely_config
end

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

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

def on_track(event_key, user_id, attributes, event_tags, _event)
  # process the event here (send to analytics provider, audit/inspect data)
end

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