Skip to content

Instantly share code, notes, and snippets.

@dstokes
Created June 3, 2012 01:53
Show Gist options
  • Save dstokes/2860942 to your computer and use it in GitHub Desktop.
Save dstokes/2860942 to your computer and use it in GitHub Desktop.
Detect Optimizely
var styles =
{ padding: '4px'
, width: '100%'
, textAlign: 'center'
, position: 'absolute'
, background: '#BADA55'
, borderBottom:'solid 1px #666'
, opacity: '0.5'
};
// get a reference to the actual window
var w = window.unsafeWindow || (
unsafeWindow = (function() {
var el = document.createElement('p');
el.setAttribute('onclick', 'return window;');
return el.onclick();
}())
);
function detectOptimizely() {
var experiments = ((w.optimizely || {}).activeExperiments || []), bod, el;
if(experiments.length === 0) return;
bod = w.document.body
el = w.document.createElement('p');
console.log('Optimizely is running experiments: ', experiments);
for(prop in styles)
styles.hasOwnProperty(prop) && (el.style[prop] = styles[prop]);
el.innerText = 'Optimizely is currently running an AB test';
bod.insertBefore(el, bod.firstChild);
}
// only run on mint pages
// (prevents multiple execution caused by iframes)
if(w.location.origin.indexOf('mint') > 0)
setTimeout(detectOptimizely, 3500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment