The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Set up a notification listener using the PHP SDK

How to set up and remove notification listeners in the Optimizely Feature Experimentation PHP SDK.

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.

📘

Note

The follow code examples assume you have already initialized the PHP SDK and have created an optimizelyClient instance.

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.

use Optimizely\Notification\NotificationType;

// 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(NotificationType::DECISION);

Set up each type of notification listener

The example code below shows how to set up each type of notification listener.

// import your third-party analytics integration here
#######################################
# SET UP ACTIVATE NOTIFICATION LISTENER
#######################################
public function onActivate($experiment, $userId, $attributes, $variation) {
}

$optimizelyClient->notificationCenter->addNotificationListener(
    NotificationType::ACTIVATE,
    'onActivate'
);

#######################################
# SET UP DECISION NOTIFICATION LISTENER
#######################################
public function onDecision($type, $userId, $attributes, $decisionInfo) {
}

$optimizelyClient->notificationCenter->addNotificationListener(
    NotificationType::DECISION,
    'onDecision'
);

#######################################
# SET UP OPTIMIZELY CONFIG NOTIFICATION LISTENER
#######################################
// listen to OPTIMIZELY_CONFIG_UPDATE to get updated data
// You will get notifications whenever the datafile is updated except for SDK initialization
public function onConfigUpdate() {
}

$optimizelyClient->notificationCenter->addNotificationListener(
    NotificationType::OPTIMIZELY_CONFIG_UPDATE,
    'onConfigUpdate'
);

#######################################
# SET UP TRACK LISTENER
#######################################
public function onTrack($eventKey, $userId, $attributes, $eventTags, $event) {
}

$optimizelyClient->notificationCenter->addNotificationListener(
    NotificationType::TRACK,
    'onTrack'
);