Skip to content

Instantly share code, notes, and snippets.

@ShuvoHabib
Created August 27, 2018 07:14
Show Gist options
  • Save ShuvoHabib/d7c9dcca9f78e0d70686f6594bff6541 to your computer and use it in GitHub Desktop.
Save ShuvoHabib/d7c9dcca9f78e0d70686f6594bff6541 to your computer and use it in GitHub Desktop.
Scroll Percentage CustomGoal 50% and 75%
(function () {
var isIntervals = setInterval(function () {
// GLOBAL VARIABLES
var jQuery = window.jQuery; // JQUERY
window.optimizely = window.optimizely || []; // OPTIMIZELY
if (jQuery !== undefined && window.optimizely !== undefined) {
var scrollFifty = true;
var scrollSeventyFive = true;
var scrollNinty = true;
// Create the scrollPercentage
jQuery(window).bind('scroll', function () {
window.scrollPercent = (jQuery(window).scrollTop() / (jQuery(document).height() - jQuery(window).height())) * 100;
// Conditional code we'll use to fire events based on scrollPercentage.
if (window.scrollPercent >= 50 && scrollFifty) {
//console.log("scroll50");
window.optimizely.push(["trackEvent", "scroll_depth_50"]);
scrollFifty = false;
}
if (window.scrollPercent >= 75 && scrollSeventyFive) {
//console.log("scroll75");
window.optimizely.push(["trackEvent", "scroll_depth_75"]);
scrollSeventyFive = false;
}
});
clearInterval(isIntervals);
}
}, 25);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment