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.
import { enums } from '@optimizely/optimizely-sdk';
// Remove Notification Listener
optimizelyClient.notificationCenter.removeNotificationListener(notificationId);
// Remove All Notification Listeners
optimizelyClient.notificationCenter.clearAllNotificationListeners();
// Remove all Notification Listeners of a certain type
optimizelyClient.notificationCenter.clearNotificationListeners(enums.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 { enums } from '@optimizely/optimizely-sdk';
// import your third-party analytics integration here
///////////////////////////////////////////
// SET UP ACTIVATE NOTIFICATION LISTENER //
///////////////////////////////////////////
const onActivate = (experiment, userId, attributes, variation, event) => {
// Send data to analytics provider here
}
optimizelyClient.notificationCenter.addNotificationListener(enums.NOTIFICATION_TYPES.ACTIVATE, onActivate);
///////////////////////////////////////////
// SET UP DECISION NOTIFICATION LISTENER //
///////////////////////////////////////////
const onDecision = (decisionType, userId, attributes, decisionInfo) => {
// Add a DECISION Notification Listener for type FEATURE
if(decisionType == 'feature') {
// Access information about feature, for example, key and enabled status
console.log(decisionInfo['feature_key']);
console.log(decisionInfo['feature_enabled']);
console.log(decisionInfo['source']);
// Send data to analytics provider here
}
}
const notificationId = optimizelyClient.notificationCenter.addNotificationListener(enums.NOTIFICATION_TYPES.DECISION, onDecision);
////////////////////////////////////////////
// SET UP LOG EVENT NOTIFICATION LISTENER //
////////////////////////////////////////////
const onLogEvent = (logEvent) => {
// process the logEvent object here (send to analytics provider, audit/inspect data)
}
optimizelyClient.notificationCenter.addNotificationListener(enums.NOTIFICATION_TYPES.LOG_EVENT, onLogEvent);
////////////////////////////////////////////////////
// SET UP OPTIMIZELY CONFIG NOTIFICATION LISTENER //
////////////////////////////////////////////////////
// listen to OPTIMIZELY_CONFIG_UPDATE to get updated data
const onConfigUpdateListener = () => {
const config = optimizelyClient.getOptimizelyConfig()
}
optimizelyClient.notificationCenter.addNotificationListener(enums.NOTIFICATION_TYPES.OPTIMIZELY_CONFIG_UPDATE, onConfigUpdateListener);
////////////////////////////////////////
// SET UP TRACK NOTIFICATION LISTENER //
////////////////////////////////////////
const onTrack = (event) => {
// process the event here (send to analytics provider, audit/inspect data)
}
optimizelyClient.notificationCenter.addNotificationListener(enums.NOTIFICATION_TYPES.TRACK, onTrack);
Updated over 2 years ago