Untitled
unknown
plain_text
2 years ago
2.6 kB
20
Indexable
(function () {
"use strict";
if (typeof unsafeWindow !== "undefined") window = unsafeWindow;
var id = "162-metrics",
tag = "cv-" + id,
debug = document.cookie.indexOf("cfQA") > -1,
scrollMetric = {
scroll25: false,
scroll50: false,
scroll75: false,
scroll100: false,
};
window[tag] = {
log: function (msg) {
if (debug) console.log("[CONV]", tag, "-->", msg);
},
waitForElement: function (cssSelector, callback) {
var stop,
elementCached,
timeout,
ready = function () {
try {
elementCached = document.querySelector(cssSelector);
if (stop) return;
if (elementCached) {
callback(elementCached);
clearTimeout(timeout);
window[tag].log(cssSelector + " found");
} else {
window.requestAnimationFrame(ready);
}
} catch (err) {
window[tag].log(err.message);
}
};
window.requestAnimationFrame(ready);
timeout = setTimeout(function () {
stop = true;
window[tag].log(cssSelector + " not found");
}, 5000);
},
initMetrics: function () {
window.addEventListener("scroll", observeScroll);
// CAN 162 metrics here
},
sendEvtOpt: function (evName, isScrollMetric) {
if (isScrollMetric) evName = "conv_scrolls_" + evName + "_percent_of_page";
window["optimizely"].push({
type: "event",
eventName: evName,
});
window[tag].log("metric triggered: " + evName);
},
on: function (n, e, o) {
document.addEventListener(n, function (n) {
var r = n.target;
if (r.matches(e)) o(n);
else
for (; r && r.parentNode !== document; ) {
if (!(r = r.parentNode)) return;
r.matches(e) && o(n);
}
});
},
};
window[tag].initMetrics();
function observeScroll() {
var h = document.documentElement,
b = document.body,
st = "scrollTop",
sh = "scrollHeight";
if (!scrollMetric.scroll100 && h && b) {
var percentage = ((h[st] || b[st]) / ((h[sh] || b[sh]) - h.clientHeight)) * 100;
if (!scrollMetric.scroll25 && percentage > 25) {
scrollMetric.scroll25 = true;
window[tag].sendEvtOpt("25", true);
}
if (!scrollMetric.scroll50 && percentage > 50) {
scrollMetric.scroll50 = true;
window[tag].sendEvtOpt("50", true);
}
if (!scrollMetric.scroll75 && percentage > 75) {
scrollMetric.scroll75 = true;
window[tag].sendEvtOpt("75", true);
}
if (percentage > 99) {
scrollMetric.scroll100 = true;
window[tag].sendEvtOpt("100", true);
window.removeEventListener("scroll", observeScroll);
}
}
}
})();Editor is loading...
Leave a Comment