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 com.optimizely.ab.notification.*;
// Remove Notification Listener
optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId);
// Remove all Notification Listeners
optimizelyClient.getNotificationCenter().clearAllNotificationListeners();
// Remove all Notification Listeners of a certain type
optimizelyClient.getNotificationCenter().clearNotificationListeners(DecisionNotification.class);
Set up each type of notification listener
The example code below shows how to set up each type of notification listener.
import com.optimizely.ab.notification.*;
// import your third-party analytics integration here
/**************************************
* SET UP ACTIVATE NOTIFICATION LISTENER
**************************************/
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(ActivateNotification.class, activateNotification -> {
logger.debug(activateNotification.getExperiment().getKey());
logger.debug(activateNotification.getUserId());
logger.debug(activateNotification.getAttributes());
logger.debug(activateNotification.getVariation().getKey());
// Send data to analytics provider here
});
/**************************************
* SET UP DECISION NOTIFICATION LISTENER
**************************************/
int notificationId = optimizelyClient.addDecisionNotificationHandler((DecisionNotification decisionNotification) -> {
// Access type on decisionObject to get type of decision
String decisionType = decisionNotification.getType();
if (decisionType == "feature") {
Map<String, ?> featureDecisionInfo = decisionNotification.getDecisionInfo();
logger.debug("Feature access related information: {}", featureDecisionInfo.toString());
// Send data to analytics provider here
}
});
/**************************************
* SET UP LOG EVENT NOTIFICATION LISTENER
**************************************/
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(LogEvent.class, LogEvent -> {
// process the logEvent object here (send to analytics provider, audit/inspect data)
});
/**************************************
* SET UP OPTIMIZELY CONFIG NOTIFICATION LISTENER
**************************************/
// listen to OPTIMIZELY_CONFIG_UPDATE to get updated data
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(UpdateConfigNotification.class, config -> {
OptimizelyConfig optimizelyConfig = optimizely.getOptimizelyConfig();
});
/**************************************
* SET UP TRACK LISTENER
**************************************/
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(TrackNotification.class, trackNotification -> {
// process the event here (send to analytics provider, audit/inspect data)
});
Updated almost 3 years ago