The Full Stack Developer Guide Developer Hub

Welcome to the Full Stack Developer Guide developer hub. You'll find comprehensive guides and documentation to help you start working with the Full Stack Developer Guide as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    

Register notification listeners

Notification listeners trigger a callback function of your choice when certain actions are triggered in the SDK. The most common use case is to build custom analytics integrations that forward impressions and conversion events on to other systems. For example, you can send a stream of all all experiment and feature flag decisions to an internal data warehouse and join it with other data you have about your users.

Currently, we support these listeners:

Important

The ACTIVATE notification listener is deprecated in Full Stack SDKs 3.1.x and later. You must use the DECISION listener.

You add a listener by accessing the notification listener instance variable off of your Optimizely instance. Define your closure, function, lambda, or class (depending on the language) and add it to the notification center. The listeners are called on every subsequent event. Listeners are added per event type, so there is a listener for activate and a listener for track.

The example code shows how to add a listener, remove a listener, remove all listeners of a specific type (such as all activate listeners), and remove all listeners.

DECISION notification listener

The DECISION notification listener works with SDKs 3.1.x and later.

The DECISION notification listener enables you to be notified whenever the SDK determines what decision value to return for an experiment, a feature, or a feature variable. Use this notification listener to focus on the experiment information that you need, such as:

  • When the system activates a user in an experiment.
  • When the system determines a user's access to a feature or a variable value for a user.

Specific Full Stack methods support the DECISION notification listener functionality. See the API reference topic for:

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

Field
Type
Description

type

string

Indicates the type of decision and is one of these values:

  • ab-test: Returned when you use the Activate or Get Variation method to determine the experiment variation for a user.

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

  • Is Feature Enabled returns a boolean value.
  • Get Enabled Features returns an array of strings consisting of all features in the project enabled for the user.

  • feature-variable: Returned when you use any of the Get Variable methods (string, integer, boolean, or double) to determine variable value for a user.

decision info

map

Key-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 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 and results segmentation. Non-string values are only supported in the 3.0 SDK and above.

Type
Decision Info Values

ab-test

  • experiment_key: String id of the experiment.
  • variation_key: Key denoting variation that the user received. This is null if no variation was assigned to the user.

feature

  • feature_key: String id of the feature.
  • feature_enabled: 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 either:
    • feature-test if the feature became enabled or disabled for the user because they were part of feature test associated with the feature.
    • rollout if the feature became enabled or disabled for the user because of the rollout configuration associated with the feature.
  • source_info: Key-value map that contains more information about the source of how the feature became enabled or disabled. If the source is feature-test, the map contains experiment_key and variation_key values corresponding to the experiment associated with the feature. If the source is rollout, it is an empty key-value map.

feature-variable

  • feature_key: String id of the feature variable’s value.
  • feature_enabled: True or false based on whether feature is enabled for the user.
  • variable_key: Variable whose value is being determined.
  • variable_type: String denoting type of variable: string, double, boolean, or integer.
  • variable_value: Value of variable that user receives; corresponds to the type described previously in variable_type.
  • source: String denoting how user gained access the variable’s value. Value is either feature-test if the feature is enabled or disabled for the user because they are part of a feature test associated with the feature, or rollout(if the feature is enabled or disabled for user because of the rollout configuration associated with the feature.
  • source_info: Key-value map that contains more information about the source of how the feature became enabled or disabled. If the source is feature-test, the map contains experiment_key and variation_key values corresponding to the experiment associated with the feature. If the source is rollout, it is an empty key-value map.

The example below shows how to use the DECISION notification listener.

import com.optimizely.ab.notification.NotificationCenter;

OptimizelyClient optimizelyClient = optimizelyManager.getOptimizely();
        int notificationId = optimizelyClient.addDecisionNotificationHandler((DecisionNotification decisionNotification) -> {
            // Access type on decisionObject to get type of decision
            Map<String, ?> decisionInfo = decisionNotification.getDecisionInfo();
            String decisionType = decisionNotification.getType();
            if (decisionType == "ab-test") {
                logger.debug("Experiment access related information: {}", decisionInfo.toString());
            }
            if (decisionType == "feature") {
                logger.debug("Feature access related information: {}", decisionInfo.toString());
            }
            if (decisionType == "feature-variable") {
                logger.debug("Feature variable related information: {}", decisionInfo.toString());
            }
        });

// Remove a notification listener
optimizely.getNotificationCenter().removeNotificationListener(notificationId);

// Remove all notification listeners
optimizely.getNotificationCenter().clearAllNotificationListeners();

using OptimizelySDK;
using OptimizelySDK.Entity;
using OptimizelySDK.Event;
using OptimizelySDK.Notifications;
using NotificationType = OptimizelySDK.Notifications.NotificationCenter.NotificationType;

var optimizelyClient = new Optimizely(datafile);


NotificationCenter.DecisionCallback OnDecision = (type, userId, userAttributes, decisionInfo) =>
{
  if (type == "ab-test")
  {
    Console.WriteLine(string.Format("Experiment access related information: {0}", decisionInfo.ToString()));
  }
  if (type == "feature")
  {
    Console.WriteLine(string.Format("Feature access related information: {0}", decisionInfo.ToString()));
  }
  if (type == "feature-variable")
  {
    Console.WriteLine(string.Format("Feature variable access related information: {0}", decisionInfo.ToString()));
  }
};

// Add a Decision notification listener
int decisionListenerId = optimizely.NotificationCenter.AddNotification(NotificationType.Decision, OnDecision);

// Remove notification listener
optimizelyClient.NotificationCenter.RemoveNotification(decisionListenerId);

// Clear all notification listeners of a certain type
optimizelyClient.NotificationCenter.ClearNotifications(NotificationType.Decision);

// Clear all notifications
optimizelyClient.NotificationCenter.ClearAllNotifications();

import com.optimizely.ab.notification.*;

optimizely.addDecisionNotificationHandler((DecisionNotification decisionNotification) -> {
  // Access type on decisionObject to get type of decision
  String decisionType = decisionNotification.getType();
  if (decisionType == "ab-test") {
    HashMap<String, String> experimentDecisionInfo = decisionInfo.getDecisionInfo();
    logger.debug("Experiment access related information: {}", experimentDecisionInfo.toString());
  }
  if (decisionType == "feature") {
    HashMap<String, ?> featureDecisionInfo = decisionInfo.getDecisionInfo();
    logger.debug("Feature access related information: {}", featureDecisionInfo.toString());
  }
  if (decisionType == "feature-variable") {
    HashMap<String, ?> featureVariableDecisionInfo = decisionInfo.getDecisionInfo();
    logger.debug("Feature variable related information: {}", featureVariableDecisionInfo.toString());
  }
});

// Remove a notification listener
optimizely.getNotificationCenter().removeNotificationListener(notificationId);

// Remove all notification listeners
optimizely.getNotificationCenter().clearAllNotificationListeners();

// Remove all notification listeners of a certain type
optimizely.getNotificationCenter().clearNotificationListeners(NotificationCenter.NotificationType.Track);

var optimizely = require('@optimizely/optimizely-sdk');
var optimizelyEnums = require('@optimizely/optimizely-sdk').enums;

// Get datafile into variable datafile

var optimizelyClient = optimizely.createInstance({
  datafile,
});

function onDecision(decisionObject) {
  // 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);
}

optimizelyClient.notificationCenter.addNotificationListener(
  optimizelyEnums.NOTIFICATION_TYPES.DECISION,
  onDecision,
);

  
var optimizely = require('@optimizely/optimizely-sdk');
var optimizelyEnums = require('@optimizely/optimizely-sdk').enums;

// Get datafile into variable datafile

var optimizelyClient = optimizely.createInstance({
  datafile,
});

function onDecision(decisionObject) {
  // 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);
}

optimizelyClient.notificationCenter.addNotificationListener(
  optimizelyEnums.NOTIFICATION_TYPES.DECISION,
  onDecision,
);

  
// Add a notification listener
NSNumber *notificationId = [self.optimizely.notificationCenter addDecisionNotificationListenerWithDecisionListener:^(NSString *type, NSString *userId, NSDictionary<NSString *,id> *attributes, NSDictionary<NSString *,id> *decisionInfo) {
     // process data here
}];

// Remove a specific listener
[optimizely.notificationCenter removeNotificationListenerWithNotificationId:notificationId];

// Remove all of one type of listener
[optimizely.notificationCenter clearNotificationListenersWithType:NotificationTypeDecision];

// Remove all notification listeners
[optimizely.notificationCenter clearAllNotificationListeners];

use Optimizely\Notification\NotificationType;

function onDecision($type, $userId, $attributes, $decisionInfo)
{     
    // $decisionInfo will have information based on $type
    if ($type == 'ab-test') {
      // Access experiment key and variation key
      print($decisionInfo->experimentKey);
      print($decisionInfo->variationKey);
    }

    if ($type == 'feature') {
      // Access information about feature
      print($decisionInfo->featureKey);
		  print($decisionInfo->featureEnabled);
      print($decisionInfo->source);
    }

    if ($type == 'feature-variable') {
      // Access information about feature's variable
      print($decisionInfo->featureKey);
		  print($decisionInfo->featureEnabled);
      print($decisionInfo->source);
      print($decisionInfo->variableKey);
			print($decisionInfo->variableType);
			print($decisionInfo->variableValue);
    }
}

// Add a DECISION listener
$decisionListenerId = $optimizelyClient->notificationCenter->addNotificationListener(
    NotificationType::DECISION,
    'onDecision'
);

// Remove a specific notification listener        
$optimizelyClient->notificationCenter->removeNotificationListener($decisionListenerId);

// Remove all notification listeners of a certain type
$optimizelyClient->notificationCenter->clearNotificationListeners(NotificationType::DECISION);

// Remove all notification listeners
$optimizelyClient->notificationCenter->clearAllNotificationListeners();

from optimizely.notification_center import NotificationCenter
from optimizely.helpers import enums

def on_decision(decision_type, user_id, attributes, decision_info):
  if decision_type == 'ab-test':
      # Access experiment key and variation key
      print decision_info.get('experiment_key')
      print decision_info.get('variation_key')
  if decision_type == 'feature':
      # Access information about feature
      print decision_info.get('feature_key')
      print decision_info.get('feature_enabled')
      print decision_info.get('source')
  if decision_type == 'feature_variable':
      # Access information about feature's variable
      print decision_info.get('feature_key')
      print decision_info.get('feature_enabled')
      print decision_info.get('source')
      print decision_info.get('variable_key')
      print decision_info.get('variable_type')
      print decision_info.get('variable_value')
  
# Add a DECISION Notification Listener
notification_id = optimizely_client.notification_center.add_notification_listener(
enums.NotificationTypes.DECISION, on_decision)

# Remove Notification Listener
optimizely_client.notification_center.remove_notification_listener(notification_id)

# Remove all Notification Listeners
optimizely_client.notification_center.clear_all_notifications()

# Remove all Notification Listeners of a certain type
optimizely_client.clear_notifications(enums.NotificationTypes.DECISION);

class decision_callback
  def on_decision(type, user_id, attributes, decision_info)
    puts "On Decision called with: #{type}, #{user_id}, #{attributes}, #{decision_info}"
  end
end

# Add a DECISION notification listener
callback = decision_callback.new
callback_reference = callback.method(:on_decision)
decision_listener_id = @optly.notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION], callback_reference)

# Remove a notification listener
@optly.notification_center.remove_notification_listener(decision_listener_id)

# Remove notification listeners of a certain type
@optly.notification_center.clear_notification_listeners(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:DECISION])

# Remove all notification listeners
@optly.notification_center.clear_all_notification_listeners()

// Add a notification listener
let notificationId = optimizely.notificationCenter.addDecisionNotificationListener(decisionListener: { (type, userId, attributes, decisionInfo) in
     // process data here
})  

// Remove a specific notification listener
optimizely.notificationCenter.removeNotificationListener(notificationId: notificationId!)

// Remove notification listeners of a certain type
optimizely.notificationCenter.clearNotificationListeners(type: .decision)

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

ACTIVATE notification listener

The ACTIVATE notification listener works with SDKs 3.0.x and earlier.

Important

The ACTIVATE notification listener is deprecated in Full Stack SDKs 3.1.x and later. You must use the DECISION listener.

JavaScript and Node have examples for the 3.0.0 and 3.0.1 SDKs.

import com.optimizely.ab.notification.ActivateNotificationListener;
import com.optimizely.ab.notification.NotificationCenter;

OptimizelyClient optimizelyClient = optimizelyManager.getOptimizely();

// Add an Activate listener
int notificationId = optimizelyClient.getNotificationCenter().addNotificationListener(NotificationCenter.NotificationType.Activate, new ActivateNotificationListener() {
    @Override
    public void onActivate(@Nonnull Experiment experiment, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Variation variation, @Nonnull LogEvent event) {
        logger.debug("activate called for experiment {} on {}", experiment.getKey(), userId);
    }
});

// Remove a notification listener
optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId);

// Remove all notification listeners
optimizelyClient.getNotificationCenter().clearAllNotificationListeners();

// Remove all notification listeners of a certain type.
optimizelyClient.getNotificationCenter().clearNotificationListeners(NotificationCenter.NotificationType.Activate);

using OptimizelySDK;
using OptimizelySDK.Entity;
using OptimizelySDK.Event;
using OptimizelySDK.Notifications;
using NotificationType = OptimizelySDK.Notifications.NotificationCenter.NotificationType;

var optimizelyClient = new Optimizely(datafile);


NotificationCenter.ActivateCallback OnActivate = (Experiment experiment, string userId, UserAttributes userAttributes, Variation variation, LogEvent logEvent) => {
  Console.WriteLine(experiment.Key);
  Console.WriteLine(userId);
};

// Add an Activate notification listener
int activateId = optimizelyClient.NotificationCenter.AddNotification(NotificationType.Activate, OnActivate);

// Remove notification listener
optimizelyClient.NotificationCenter.RemoveNotification(activateId);

// Clear all notification listeners of a certain type
optimizelyClient.NotificationCenter.ClearNotifications(NotificationType.Activate);

// Clear all notifications
optimizelyClient.NotificationCenter.ClearAllNotifications();

import com.optimizely.ab.notification.ActivateNotificationListener;
import com.optimizely.ab.notification.NotificationCenter;

// Add an Activate listener
int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, new ActivateNotificationListener() {
    @Override
    public void onActivate(@Nonnull Experiment experiment, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Variation variation, @Nonnull LogEvent event) {
        logger.debug("experiment {} was activated for user {}", experiment.getKey(), userId);
    }
});

// Remove a notification listener
optimizely.notificationCenter.removeNotificationListener(notificationId);

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

// Remove all notification listeners of a certain type
optimizely.notificationCenter.clearNotificationListeners(NotificationCenter.NotificationType.Activate);

// 3.0.0 SDK
var optimizelyEnums = require('@optimizely/optimizely-sdk/lib/utils/enums');

// 3.0.1 SDK and above
var optimizelyEnums = require('@optimizely/optimizely-sdk').enums;

function onActivate(activateObject) {
  console.log(
    'activate called for experiment %s',
    activateObject.experiment['key'],
  );
}

// Add an ACTIVATE notification listener
const activateId = optly.notificationCenter.addNotificationListener(
  optimizelyEnums.NOTIFICATION_TYPES.ACTIVATE,
  onActivate,
);

// Remove a specific listener
optly.notificationCenter.removeNotificationListener(activateId);

// Remove listeners for a specific notification type
optly.notificationCenter.clearNotificationListeners(
  optimizelyEnums.NOTIFICATION_TYPES.ACTIVATE,
);

// Remove all listeners
optly.notificationCenter.clearAllNotificationListeners();
  
  
// 3.0.0 SDK
var NOTIFICATION_TYPES = require("@optimizely/optimizely-sdk/lib/utils/enums")
  .NOTIFICATION_TYPES;

// 3.0.1 SDK and above
var NOTIFICATION_TYPES = require('@optimizely/optimizely-sdk').enums.NOTIFICATION_TYPES;


function onActivate(activateObject) {
  console.log(
    `activate called for experiment ${activateObject.experiment.key}`
  );
}

// Add an ACTIVATE notification listener
let activateId = optimizelyClient.notificationCenter.addNotificationListener(
  NOTIFICATION_TYPES.ACTIVATE,
  onActivate
);


// Test it out
optimizelyClient.activate("my_experiment", userId);

// Remove a specific listener
optimizelyClient.notificationCenter.removeNotificationListener(activateId);

// Remove listeners for a specific notification type
optimizelyClient.notificationCenter.clearNotificationListeners(
  NOTIFICATION_TYPES.ACTIVATE
);

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

The `ACTIVATE` notification listener is deprecated. You must use the [`DECISION`](#section-decision-notification-listener) listener.
function onActivate($experiment, $userId, $attributes, $variation)
{
    print "activate experiment " . $experiment->getKey() . " for user " . $userId;
}             

// Add an ACTIVATE listener
$activateId = $optimizelyClient->notificationCenter->addNotificationListener(
    NotificationType::ACTIVATE,
    'onActivate'
);

// Remove a specific notification listener        
$optimizelyClient->notificationCenter->removeNotificationListener($activateId);

// Remove all notification listeners of a certain type
$optimizelyClient->notificationCenter->clearNotificationListeners(NotificationType::ACTIVATE);

// Remove all notification listeners
$optimizelyClient->notificationCenter->clearAllNotificationListeners();

from optimizely.notification_center import NotificationCenter
from optimizely.helpers import enums

def on_activate(experiment, user_id, attributes, variation, event):
    print('Activated experiment {0}'.format(experiment.key))

# Add an ACTIVATE Notification Listener
notification_id = optimizely_client.notification_center.add_notification_listener(enums.NotificationTypes.ACTIVATE, on_activate)

# Remove Notification Listener
optimizely_client.notification_center.remove_notification_listener(notification_id)

# Remove all Notification Listeners
optimizely_client.notification_center.clear_all_notifications()

# Remove all Notification Listeners of a certain type
optimizely_client.clear_notifications(enums.NotificationTypes.ACTIVATE);
class activate_callback
    def on_activate(experiment, user_id, attributes, variation, event)
        @logger.log(Logger::DEBUG, "Activated experiment '#{experiment['key']}'.")
    end
end

# Add an ACTIVATE notification listener
callback = activate_callback.new
callback_reference = callback.method(:on_activate)
activate_id = @optly.notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE], callback_reference)

# Remove a notification listener
@optly.notification_center.remove_notification_listener(activate_id)

# Remove notification listeners of a certain type
@optly.notification_center.clear_notification_listeners(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE])

# Remove all notification listeners
@optly.notification_center.clear_all_notification_listeners()

The `ACTIVATE` notification listener is deprecated. You must use the [`DECISION`](#section-decision-notification-listener) listener.

TRACK notification listener

The TRACK notification listener works with SDKs 3.0.x and later.

JavaScript and Node have examples for the 3.0.0 and 3.0.1 SDKs.
Java has examples for 3.0.0 and 3.1.0 SDKs.

import com.optimizely.ab.notification.TrackNotificationListener;
import com.optimizely.ab.notification.NotificationCenter;

OptimizelyClient optimizelyClient = optimizelyManager.getOptimizely();

// Add a Track listener
int trackNotificationId = optimizelyClient.getNotificationCenter().addNotificationListener(NotificationCenter.NotificationType.Track, new TrackNotificationListener() {
    @Override
    public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags, @Nonnull LogEvent event) {
        logger.debug("track was called for event {}", eventKey);
    }
});

// Remove a notification listener
optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId);

// Remove all notification listeners
optimizelyClient.getNotificationCenter().clearAllNotificationListeners();

// Remove all notification listeners of a certain type.
optimizelyClient.getNotificationCenter().clearNotificationListeners(NotificationCenter.NotificationType.Track);

using OptimizelySDK;
using OptimizelySDK.Entity;
using OptimizelySDK.Event;
using OptimizelySDK.Notifications;
using NotificationType = OptimizelySDK.Notifications.NotificationCenter.NotificationType;

var optimizelyClient = new Optimizely(datafile);

NotificationCenter.TrackCallback OnTrack = (string eventKey, string userId, UserAttributes userAttributes, EventTags eventTags, LogEvent logEvent) => {
  Console.WriteLine(eventKey);
  Console.WriteLine(userId);
};

// Add a Track notification listener
int trackId = optimizelyClient.NotificationCenter.AddNotification(NotificationType.Track, OnTrack);

// Remove notification listener
optimizelyClient.NotificationCenter.RemoveNotification(trackId);

// Clear all notification listeners of a certain type
optimizelyClient.NotificationCenter.ClearNotifications(NotificationType.Track);

// Clear all notifications
optimizelyClient.NotificationCenter.ClearAllNotifications();

import com.optimizely.ab.notification.*;

// 3.0.0 SDK
// Add a Track listener
int trackNotificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Track, new TrackNotificationListener() {
    @Override
    public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags, @Nonnull LogEvent event) {
        logger.debug("track was called for user {} on event {}", userId, eventKey);
    }
});

// Remove a notification listener
optimizely.notificationCenter.removeNotificationListener(notificationId);

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

// Remove all notification listeners of a certain type
optimizely.notificationCenter.clearNotificationListeners(NotificationCenter.NotificationType.Track);


// 3.1.0 SDK and above
// Add a Track listener
int trackNotificationId = optimizely.addTrackNotificationHandler((TrackNotification trackNotification) -> {
  logger.debug("track was called for user {} on event {}", trackNotification.getUser(), trackNotification.getEventKey());
});

// Remove a notification listener
optimizely.getNotificationCenter().removeNotificationListener(notificationId);

// Remove all notification listeners
optimizely.getNotificationCenter().clearAllNotificationListeners();

// Remove all notification listeners of a certain type
optimizely.getNotificationCenter().clearNotificationListeners(NotificationCenter.NotificationType.Track);
// 3.0.0 SDK
var optimizelyEnums = require('@optimizely/optimizely-sdk/lib/utils/enums');

// 3.0.1 SDK and above
var optimizelyEnums = require('@optimizely/optimizely-sdk').enums;

function onTrack(trackObject) {
  console.log('track called for event %s', trackObject.eventKey);
}

// Add a TRACK notification listener
const trackId = optly.notificationCenter.addNotificationListener(
  optimizelyEnums.NOTIFICATION_TYPES.TRACK,
  onTrack,
);

// Remove a specific listener
optly.notificationCenter.removeNotificationListener(trackId);

// Remove listeners for a specific notification type
optly.notificationCenter.clearNotificationListeners(
  optimizelyEnums.NOTIFICATION_TYPES.TRACK,
);

// Remove all listeners
optly.notificationCenter.clearAllNotificationListeners();
  
  
// 3.0.0 SDK
var NOTIFICATION_TYPES = require("@optimizely/optimizely-sdk/lib/utils/enums")
  .NOTIFICATION_TYPES;

// 3.0.1 SDK and above
var NOTIFICATION_TYPES = require('@optimizely/optimizely-sdk').enums.NOTIFICATION_TYPES;

function onTrack(trackObject) {
  console.log(`track called for event ${trackObject.eventKey}`);
}

// Add an TRACK notification listener
let activateId = optimizelyClient.notificationCenter.addNotificationListener(
  NOTIFICATION_TYPES.TRACK,
  onActivate
);

// Test it out
optimizelyClient.track("my_conversion", userId);

// Remove a specific listener
optimizelyClient.notificationCenter.removeNotificationListener(trackId);

// Remove listeners for a specific notification type
optimizelyClient.notificationCenter.clearNotificationListeners(
  NOTIFICATION_TYPES.TRACK
);

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

NSNumber *notifId = [self.optimizely.notificationCenter addTrackNotificationListenerWithTrackListener:^(NSString *eventKey,
                                                                                                  NSString *userId,
                                                                                                  NSDictionary<NSString *,id> *attributes, NSDictionary<NSString *,id> *eventTags, NSDictionary<NSString *,id> *event) {
         // process data here  
}];

// see “DecisionService Notifications” for remove examples

function onTrack($eventKey, $userId, $attributes, $eventTags, $event)
{     
    print "conversion event " . $eventKey . " for user " . $userId;
}

// Add a TRACK listener
$trackId = $optimizelyClient->notificationCenter->addNotificationListener(
    NotificationType::TRACK,
    'onTrack'
);

// Remove a specific notification listener        
$optimizelyClient->notificationCenter->removeNotificationListener($trackId);

// Remove all notification listeners of a certain type
$optimizelyClient->notificationCenter->clearNotificationListeners(NotificationType::TRACK);

// Remove all notification listeners
$optimizelyClient->notificationCenter->clearAllNotificationListeners();

from optimizely.notification_center import NotificationCenter
from optimizely.helpers import enums

def on_track(event_key, user_id, attributes, event_tags, event):
    print('Track event with event_key={0}'.format(event_key))

# Add a TRACK Notification Listener
notification_id = optimizely_client.notification_center.add_notification_listener(
enums.NotificationTypes.TRACK, on_track)

# Remove Notification Listener
optimizely_client.notification_center.remove_notification_listener(notification_id)

# Remove all Notification Listeners
optimizely_client.notification_center.clear_all_notifications()

# Remove all Notification Listeners of a certain type
optimizely_client.clear_notifications(enums.NotificationTypes.TRACK);
class track_callback
    def on_track(event_key, user_id, attributes, event_tags, event)
        @logger.log(Logger::DEBUG, "Event track for  '#{event_key}'.")
    end
end

# Add a TRACK notification listener
callback = track_callback.new
callback_reference = callback.method(:on_track)
track_id = @optly.notification_center.add_notification_listener(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK], callback_reference)

# Remove a notification listener
@optly.notification_center.remove_notification_listener(track_id)

# Remove notification listeners of a certain type
@optly.notification_center.clear_notification_listeners(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:TRACK])

# Remove all notification listeners
@optly.notification_center.clear_all_notification_listeners()

let notificationId = optimizely.notificationCenter.addTrackNotificationListener(trackListener: { (eventKey, userId, attributes, eventTags, event) in
         // process data here
})

// see “DecisionService Notifications” for remove examples


Register notification listeners


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.