Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robisatthefunction/28377ad89609192182f4cf1cdc0d5a14 to your computer and use it in GitHub Desktop.
Save robisatthefunction/28377ad89609192182f4cf1cdc0d5a14 to your computer and use it in GitHub Desktop.
// Client-side JavaScript example
// Notification Listeners execute whenever a bucketing decision is made on a visitor for an A/B Test or Targeted Delivery.
// https://docs.developers.optimizely.com/feature-experimentation/docs/set-up-notification-listener-javascript
const onDecision = ({ type, userId, attributes, decisionInfo }) => {
// Check if a decision event was dispatched. This will only happen if the visitor is put into an A/B Test.
if (decisionInfo['decisionEventDispatched']) {
// Get experiment bucketing info
let experimentKey = decisionInfo['ruleKey']
let variationKey = decisionInfo['variationKey']
let decisionObject = {};
decisionObject[experimentKey] = variationKey;
// https://docs.mparticle.com/developers/sdk/web/event-tracking/
window.mParticle.logEvent('Experiment Viewed', window.mParticle.EventType.Other, decisionObject);
}
const notificationId = optimizelyClient.notificationCenter.addNotificationListener(enums.NOTIFICATION_TYPES.DECISION, onDecision);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment