Rich-text Reply

Getting experiment name and variation name key/value pair within GTM via Optimizely X API

quarrazzella 10-18-16
Accepted Solution

Getting experiment name and variation name key/value pair within GTM via Optimizely X API

 

Greetings to the community!

 

Here's a function (source: https://help.optimizely.com/Integrate_Other_Platforms/Implementing_Optimizely_with_Google_Tag_Manage...) that returns the experiment name and variation name key/value pair via Optimizely Classic API. 

 

 

function () {
      var o = window.optimizely,
          aE = o.activeExperiments,
          v = o.data.variations,
          gaKey = "",
          gaValue = "",
          rD = o.data.state.redirectExperiment;

      for(var i = 0; i < aE.length; i++) {
         var e = aE[i],
         vId = o.variationIdsMap[e],
         vIndex = o.variationMap[e],
         isMvt = vId.length > 1,
         eName = o.data.experiments[e].name;

         gaKey = (isMvt ? "MVT" : "AB") + " Test: " + eName + "";
         if (!isMvt) {
             gaValue = (vIndex === 0 ? "Control" : "V") + vIndex + ": " + o.variationNamesMap[e];
         } else {
             var s = o.data.sections,
                 sId = o.data.experiments[e].section_ids;
             for (i = 0; i < sId.length; i++) {
                if(i == 0) {
                  gaValue = s[sId[i]].name.toString() + ': ' + v[vId[i]].name;
                } else {
                  gaValue += ', ' + s[sId[i]].name.toString() + ': ' + v[vId[i]].name;
                }
             }
         }
         if (gaKey.length !== 0 && gaValue.length !== 0 && o.allExperiments[e].universal_analytics && o.allExperiments[e].universal_analytics.slot == 3) {
           return gaKey+": "+gaValue;
         }
     }

     if (rD !== undefined) {
         var rDe = rD.experimentId,
             rvId = o.variationIdsMap[rDe],
             rVIndex = o.variationMap[rDe],
             rIsMvt = rvId.length > 1,
             rEName = o.data.experiments[rDe].name,
             rGaKey = "",
             rGaValue = "";

         rGaKey = (rIsMvt ? "MVT Redirect" : "AB Redirect") + " Test: " + rEName + "";

         if (!rIsMvt) {
             rGaValue = (rVIndex === 0 ? "Control" : "V") + rVIndex + " Redirect: " + o.variationNamesMap[rDe];
         } else {
             var sR = o.data.sections,
                 sIdR = o.data.experiments[rDe].section_ids;
            for(i = 0; i < sIdR.length; i++) {
                 rGaValueArray = sR[sIdR[i]].name.toString() + "Redirect: " + v[rvId[i]].name;
             }
         }
         if (rGaKey.length !== 0 && rGaValue.length !== 0 && o.allExperiments[rDe].universal_analytics && o.allExperiments[rDe].universal_analytics.slot == 3) {
           return rGaKey+": "+rGaValue;
         }
     }
  }

I've written a version that uses Optimizely X API and it seems to work fine. But I'm almost certain that my code can be (must be) improved. So, can someone share X API version of the below function?

Thanks in advance!    

 

rockymcg 10-20-16
 

Re: Getting experiment name and variation name key/value pair within GTM via Optimizely X API

Hello,

In order to get this sorted, I'd like to follow up with you in a support ticket. I will send you more details there.
Optimizely

Re: Getting experiment name and variation name key/value pair within GTM via Optimizely X API

[ Edited ]

Thank you very much, Rocky! The script we've tested works just fine. Hope to see it soon in Optimizely knowledge base.

santhony7 02-27-17
 

Re: Getting experiment name and variation name key/value pair within GTM via Optimizely X API

[ Edited ]

Does this code work with Optimizely X? Can you post your X version? Thanks!

 

Never mind. Sorry!

Anthony Smith