Created
June 23, 2021 17:03
-
-
Save killa-kyle/68a403e37020b06286fd7ef6e2f9490b to your computer and use it in GitHub Desktop.
cro-test check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// check for CRO test | |
function _croTestCheck() { | |
console.log("cro_check") | |
// The polling function | |
function poll(fn, timeout, interval) { | |
var endTime = Number(new Date()) + (timeout || 2000) | |
interval = interval || 100 | |
var checkCondition = function (resolve, reject) { | |
// If the condition is met, we're done! | |
var result = fn() | |
if (result) { | |
resolve(result) | |
} | |
// If the condition isn't met but the timeout hasn't elapsed, go again | |
else if (Number(new Date()) < endTime) { | |
setTimeout(checkCondition, interval, resolve, reject) | |
} | |
// Didn't match and too much time, reject! | |
else { | |
reject(new Error("timed out for " + fn + ": " + arguments)) | |
} | |
} | |
return new Promise(checkCondition) | |
} | |
// Usage: ensure google_optimize is on the window, then check for specific experiments | |
poll( | |
function () { | |
return google_optimize | |
}, | |
5000, | |
150 | |
) | |
.then(function () { | |
// Polling done, now do something else! | |
console.log("google_optimize on the window", google_optimize) | |
checkExperiment("JEw_R3p3RVuxxxHF-HrI1Q") | |
}) | |
.catch(function () { | |
// Polling timed out, handle the error! | |
console.log("google_optimize unavailable") | |
}) | |
function checkExperiment(experimentID) { | |
console.log("experiment-id: " + experimentID) | |
if (google_optimize && google_optimize.get(experimentID)) { | |
var cro_group = google_optimize.get(experimentID) | |
console.log("cro-group: ", cro_group) | |
// group 1 for exit intent test | |
if ( | |
(cro_group == 1 || cro_group == "1") && | |
experimentID == "JEw_R3p3RVuxxxHF-HrI1Q" | |
) { | |
Main.launchExitIntentModal() | |
} | |
} | |
} | |
} // end Main code block |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment