@jason_paddock, @kathleen_jo - Thanks @john_oren 1) We used some of the logic in your code and mixed/matched with a piece of code I found on http://editor.javascriptkit.com/?eg=scrolltop-pct2 2) The reason we needed to persist the scrolled value was we wanted to wait until we got to the next page to pass the percent page scrolled to s.prop65. So prop64 (which is the previous page URL) and prop65 (which is the percent scrolled on the previous page) can be co-related. But I removed the persist data on percentage scrolled because it was causing issues So here is the JS code we have now that is working. Unfortunately the code sets the ppscroll cookie for a day. We are still figuring out how to set the ppscroll cookie just for a page view. But the code works for now: function getDocHeight() { var D = document; return Math.max( D.body.scrollHeight, D.documentElement.scrollHeight, D.body.offsetHeight, D.documentElement.offsetHeight, D.body.clientHeight, D.documentElement.clientHeight ); } var winheight, docheight, trackLength, throttlescroll; function getmeasurements(){ winheight= window.innerHeight || (document.documentElement || document.body).clientHeight; docheight = getDocHeight(); trackLength = docheight - winheight; } function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i = 0; i <ca.length; i++) { var c = ca[i]; while (c.charAt(0)==' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length,c.length); } } return ""; } function setCookie(c_name, value, exdays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()) + "; path=/"; document.cookie = c_name + "=" + c_value; } function amountscrolled(){ if (ppscrollcookie == null || ppscrollcookie == "") { var scrollTop = window.pageYOffset || (document.documentElement || document.body.parentNode || document.body).scrollTop; var percentScrolled = Math.floor(scrollTop/trackLength * 100) // gets percentage scrolled (ie: 80 or NaN if tracklength == 0); var percent_page_scrolled = "0"; if(percentScrolled > 0 && percentScrolled <= 25) { percent_page_scrolled = "25"; setCookie("ppscroll", percent_page_scrolled, 1); } else if (percentScrolled > 25 && percentScrolled <= 50) { percent_page_scrolled = "50"; setCookie("ppscroll", percent_page_scrolled, 1); } else if(percentScrolled > 50 && percentScrolled <= 75) { percent_page_scrolled = "75"; setCookie("ppscroll", percent_page_scrolled, 1); } else if(percentScrolled > 75 && percentScrolled <= 100) { percent_page_scrolled = "100"; setCookie("ppscroll", percent_page_scrolled, 1); } console.log(percent_page_scrolled); } else { setCookie("ppscroll", ppscrollcookie, 1); } } getmeasurements(); var ppscrollcookie = getCookie('ppscroll'); console.log("Scroll Percent Present--> "+ppscrollcookie); window.addEventListener("resize", function(){ getmeasurements(); }, false); window.addEventListener("scroll", function(){ clearTimeout(throttlescroll) throttlescroll = setTimeout(function(){ // throttle code inside scroll to once every 50 milliseconds amountscrolled(); }, 50) }, false);
... View more