Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

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 GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Set up notification listener

This topic describes how to create your own error handler logic.

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.

To track feature usage:

  1. Sign up for an analytics provider of your choice (for example: Segment)
  2. Set up a notification listener of type 'DECISION'
  3. Follow your analytics provider's documentation and send events from within the decision listener callback

Steps 1 and 3 are not covered in this documentation. However, setting up a 'DECISION' notification listener is covered below.

Set up a DECISION notification listener

The DECISION notification listener enables you to be notified whenever the SDK determines what decision value to return for a feature. The callback is triggered with the decision type, associated decision information, user ID, and attributes.

DECISION listeners are triggered when your application renders OptimizelyFeature or OptimizelyExperiment or when you call the Is Feature Enabled or Get Enabled Features APIs. DECISION listeners are also triggered in some other cases. Please see the tables at the end of this section for complete detail.

To set up a DECISION listener:

  1. Define a callback to be called when the DECISION event is triggered
  2. Add the callback to the notification center on the Optimizely instance

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.

import {
  createInstance,
  enums,
} from '@optimizely/react-sdk';

const optimizely = createInstance({
  sdkKey: '<Your_SDK_Key>',
})

function onDecision(decisionObject) {
  if (decisionObject.type === 'feature') {
    // Access type on decisionObject to get type of decision
    console.log(decisionObject.type);
    // Access decisionInfo on decisionObject which
    // will have form as per type of decision.
    console.log(decisionObject.decisionInfo);

    // Send data to analytics provider here
    // Example:
    //  analytics.track({
    //    userId: decisionObject.userId,
    //    event: decisionObject.type,
    //    properties: decisionObject.decisionInfo,
    //  });
  }
}

// Add a notificaiton listener for the decision notification
const listenerId = optimizely.notificationCenter.addNotificationListener(
  enums.NOTIFICATION_TYPES.DECISION,
  onDecision,
);

// Remove a specific listener
optimizely.notificationCenter.removeNotificationListener(listenerId);

// Remove listeners for a specific notification type
optimizely.notificationCenter.clearNotificationListeners(
  enums.NOTIFICATION_TYPES.DECISION,
);

// Remove all listeners
optimizely.notificationCenter.clearAllNotificationListeners();

The tables below show the information provided to the listener when it is triggered.

FieldTypeDescription
typestring- feature: Returned when you use the Is Feature Enabled to determine if user has access to one specific feature, or Get Enabled Features method to determine if user has access to multiple features.

- ab-test: Returned when you use activate or get_variation to determine the variation for a user, and the given experiment is not associated to any feature.

- feature-test: Returned when you use activate or get_variation to determine the variation for a user, and the given experiment is associated to some feature.

- feature-variable: Returned when you use one of the get_feature_variable methods to determine value of some feature variable. Such as get_feature_variable_boolean.
decision infomapKey-value map that consists of data corresponding to the decision and based on the type.
See the table below for valid fields and values for each type.
user IDstringThe user ID.
attributesmapA map of custom key-value string pairs specifying attributes for the user that are used for audience targeting. Non-string values are only supported in the 3.0 SDK and above.
TypeDecision Info Values
feature- featureKey: String id of the feature. - featureEnabled: True or false based on whether the feature is enabled for the user. - source: String denoting how user gained access to the feature. Value is: - feature-test if the feature became enabled or disabled for the user because of some experiment associated with the feature. - rollout if the feature became enabled or disabled for the user because of the rollout configuration associated with the feature. - sourceInfo: Empty if the source is rollout. Holds experimentKey and variationKey if the source is feature-test.
ab-test- experimentKey: String key of the experiment - variationKey: String key of the variation to which the user got bucketed.
feature-test- experimentKey: String key of the experiment - variationKey: String key of the variation to which the user got bucketed.
feature-variable- featureKey: String id of the feature. - featureEnabled: True or false based on whether the feature is enabled for the user. - source: String denoting how user gained access to the feature. Value is: - feature-test if the feature became enabled or disabled for the user because of some experiment associated with the feature. - rollout if the feature became enabled or disabled for the user because of the rollout configuration associated with the feature. - variableKey: String key of the feature variable. - variableValue: Mixed value of the feature variable for this user. - variableType: String type of the feature variable. Can be one of boolean, double, integer, string. - sourceInfo: Map denoting source of decision. Empty if the source is rollout. Holds experimentKey and variationKey if the source is feature-test.