Optimizely
When collecting feedback from your visitors knowing which experiment(s) was active and which variant of that experiment was shown can be quite useful information to have in your feedback results. This article will explain how to get this data and how to add it to the feedback that your customers leave.
Prerequisites
Make sure that both the Mopinion deployment script and Optimizely are being loaded on the page. The following code must also be added to the page. This can be done using a tag manager or by hosting this script and then linking to it.
If you are on an Enterprise license, than we can host this script for you. In that case, please contact us at support@mopinion.com
The code snippet
(function() {
const setData = function() {
if (typeof optimizely === 'object') {
try {
const activeExperiments = optimizely.get('state').getActiveExperimentIds();
const experimentStates = optimizely.get('state').getExperimentStates();
const optimizelyData = activeExperiments.map(id => {
let variantId = null;
if (typeof experimentStates[id] === 'object' && typeof experimentStates[id].variation === 'object') {
variantId = experimentStates[id].variation.id;
}
return {
optimizely_experiment_id: id,
optimizely_variant_id: variantId
};
});
window.mopOptimizelyData = optimizelyData;
} catch (e) {
}
}
};
setData();
window.optimizely = window.optimizely || [];
window.optimizely.push({
type: "addListener",
filter: {
type: "lifecycle",
name: "campaignDecided"
},
handler: setData
});
})();Website data element
In the surveys where you want to add the Optimizely data, you’ll need to add a website data element.
This website data element should listen to the JS variable mopOptimizelyData:
Feedback result
Adding mopOptimizelyData as the JS variable directly will store all experiments and their used variant id as an array like so:
It might be useful to have the experiment ID and variant ID as separate data points in the feedback. This would allow you to use these in the chart builder for example. In that case, you can separate these by adding them as separate JS variables:
mopOptimizelyData[0].optimizely_experiment_idmopOptimizelyData[0].optimizely_variant_idThis will show like this in the feedback inbox: