Skip to content

Instantly share code, notes, and snippets.

@atma
Created November 11, 2014 12:35
Show Gist options
  • Save atma/39baafe65febf1b6d494 to your computer and use it in GitHub Desktop.
Save atma/39baafe65febf1b6d494 to your computer and use it in GitHub Desktop.
An example usage of Google Experiments on client side only
<!-- Call immediately an experiment method to prevent a FOOC (Flash of original content) -->
<button class="btn btn-link" type="button" id="button">Click me</button>
<script>typeof __abt !== "undefined" && __abt.some && __abt.some();</script>
<div classs="another-html-content"></div>
// Global var to handle the variations
window.__abt = window.__abt || {};
// Important! Use the same domain name as in GA
cxApi.setDomainName('none');
// Experiment's variations
var someExp = [
function () {
// Original, no change
},
function () {
// Do something 1
},
function () {
// Do something 2
}
],
// Configured Google experiment ID for an account button
experimentId = 'XXXXXXXXXXXXXXXXXXXXXX';
// The variation chosen for the visitor
var chosenVariation = cxApi.getChosenVariation(experimentId);
// First visit, choose randomly some variation
if (chosenVariation === cxApi.NO_CHOSEN_VARIATION) {
chosenVariation = Math.floor(Math.random() * someExp.length);
cxApi.setChosenVariation(chosenVariation, experimentId);
}
// Expose variation method to the global scope
if (chosenVariation === cxApi.NOT_PARTICIPATING) {
// Do nothing when user is not a participant
__abt.some = someExp[cxApi.ORIGINAL_VARIATION];
} else {
// Set the apropriate method
__abt.some = someExp[chosenVariation];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment