Skip to content

Instantly share code, notes, and snippets.

@samba
Last active April 16, 2018 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samba/afcb4941fc64ae89f0cbee9713785114 to your computer and use it in GitHub Desktop.
Save samba/afcb4941fc64ae89f0cbee9713785114 to your computer and use it in GitHub Desktop.
GTM Sampling Variable
function(){
// Sets and gets a sample ID for bucket selection in A/B and related testing methodologies.
var ckPattern = /sampleId=(\d+)/g;
var sampleId = -1;
// TODO: adopt a different method for selecting the corp top level domain
var domain = document.location.hostname;
document.cookie.replace(ckPattern, function($0, $d){
sampleId = Number($d);
});
if (sampleId < 0){
sampleId = (Math.random() * 1E10) >>> 0;
document.cookie = ('sampleId=' + sampleId + '; path=/; domain=' + domain + ';');
}
return sampleId;
}
function(){
// Evaluate the sample ID into a specific bucket selection
var sampleId = {{sampleId}};
var limit = 100;
var split = 70; // i.e. 30% are sampled as "true"
return (Number(sampleId) % limit > split) ? 'true' : 'false';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment