Set up notification listener
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:
- Sign up for an analytics provider of your choice (for example: Segment)
- Set up a notification listener of type 'DECISION'
- Follow your analytics provider's documentation and send events from within the decision listener callback
Steps 1 and 3 aren't 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:
- Define a callback to be called when the
DECISION
event is triggered - 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.
Field | Type | Description |
---|---|---|
type | string |
|
decision info | map | Key-value map that consists of data corresponding to the decision and based on the |
user ID | string | The user ID. |
attributes | map | A 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. |
Type | Decision Info Values |
---|---|
feature |
|
ab-test |
|
feature-test |
|
feature-variable |
|
Updated almost 2 years ago