Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save robisatthefunction/36001d59ba3116570a6cb2a55baf62b5 to your computer and use it in GitHub Desktop.

Select an option

Save robisatthefunction/36001d59ba3116570a6cb2a55baf62b5 to your computer and use it in GitHub Desktop.
// Constant for consistent event naming
const OPTIMIZELY_EVENT_NAME = 'Optimizely_Experiment';
// Helper function to safely retrieve the FullStory API
// You may want to add code to wait for it to be available
function _fs() {
const fsNamespace = window['_fs_namespace'];
const fsApi = fsNamespace && window[fsNamespace];
return typeof fsApi === 'function' ? fsApi : () => {};
}
// Optimizely decision listener callback
const onDecision = ({ type, userId, attributes, decisionInfo }) => {
// Defensive: ensure decisionInfo exists and the event was dispatched
if (decisionInfo && decisionInfo.decisionEventDispatched) {
const experimentKey = decisionInfo.ruleKey;
const variationKey = decisionInfo.variationKey;
const decisionObject = {
experiment: experimentKey,
variation: variationKey,
};
// Track the decision in FullStory
_fs()('trackEvent', {
name: OPTIMIZELY_EVENT_NAME,
properties: decisionObject,
});
// Optional: remove listener if only needed once
// optimizelyClient.notificationCenter.removeNotificationListener(notificationId);
}
};
// Register the listener with Optimizely
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