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

// Optimizely integration for Mopinion // Check if optimizely object exists function getMopinionOptimzelyData(){ let results = []; if (typeof optimizely === 'object') { // Get experiment ID and variant ID from page, if available let state = optimizely.get('state') let activeExperiments = state.getActiveExperimentIds(); let experimentStates = state.getExperimentStates(); activeExperiments.forEach(id => { let result = {optimizely_experiment_id: id}; result.optimizely_variant_id = experimentStates[id].variation.id; results.push(result); }); } return results; } // Make these ID's available for Mopinion website data element var mopOptimizelyData = getMopinionOptimzelyData(); // Run once getMopinionOptimzelyData(); function onCampaignDecided(event) { // Update results for activity where Optimizely activates a campaign or experiment getMopinionOptimzelyData(); } // Eventlistener window["optimizely"] = window["optimizely"] || []; window["optimizely"].push({ type: "addListener", filter: { type: "lifecycle", name: "campaignDecided" }, handler: onCampaignDecided });

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_id
mopOptimizelyData[0].optimizely_variant_id

This will show like this in the feedback inbox: