Skip to content

Instantly share code, notes, and snippets.

@markis
Last active August 29, 2015 14:22
Show Gist options
  • Save markis/33fbbb5e56a7810ee721 to your computer and use it in GitHub Desktop.
Save markis/33fbbb5e56a7810ee721 to your computer and use it in GitHub Desktop.
/*
Retrieve the name of the the optimizely test with DFP in it and then parse out the number after AB-###.
This is for the ad-ops team to use as a way to know what tests are running on the pages that they are serving up ads
Test names should follow this pattern: AB-123 description dfp
*/
function getDFP() {
if ('undefined' != typeof optimizely && 'undefined' != typeof optimizely.allExperiments && 'undefined' != typeof optimizely.variationMap) {
var allTests = optimizely.allExperiments;
var variationMap = optimizely.variationMap;
var abRegEx = /AB-([\d]+)/i; // regular expression to parse out AB-### and isolate the digits
var dfpRegEx = /\bdfp\b/i; // regular expression to parse out dfp with word boundary characters
var charMap = {0:'', 1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f', 7:'g', 8:'h', 9:'i', 10:'j',
11:'k', 12:'l', 13:'m', 14:'n', 15:'o', 16:'p', 17:'q', 18:'r', 19:'s', 20:'t', 21:'u',
22:'v', 23:'w', 24:'x', 25:'y', 26:'z'};
for (var testId in variationMap) {
var testName = '';
var testDefined = false;
var testEnabled = false;
if (!variationMap.hasOwnProperty(testId)) {
continue;
}
testDefined = allTests.hasOwnProperty(testId);
testEnabled = testDefined && allTests[testId].hasOwnProperty('enabled');
if (testEnabled && allTests[testId].hasOwnProperty('name')) {
testName = allTests[testId].name;
if (dfpRegEx.test(testName)) {
var abTestMatches = testName.match(abRegEx);
if (abTestMatches && abTestMatches.length > 1) {
return abTestMatches[1] + charMap[variationMap[testId]];
}
}
}
}
}
return null;
}
googletag.pubads().setTargeting('vers', getDFP());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment