-
-
Save BorisChumichev/1f35b2f5aec072dfb34febc592d79d7d to your computer and use it in GitHub Desktop.
Google analytics scroll depth tracking
This file contains 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
;(function() { | |
var passedMilestones = [] | |
, nonInteractiveThreshold = 20 | |
window.addEventListener('scroll', function(e) { | |
var scrollTop = window.scrollY | |
, body = document.body | |
, html = document.documentElement | |
, docHeight = Math.max( | |
body.scrollHeight, | |
body.offsetHeight, | |
html.clientHeight, | |
html.scrollHeight, | |
html.offsetHeight | |
) | |
, winHeight = window.innerHeight | |
, scrollPercent = (scrollTop) / (docHeight - winHeight) | |
, scrollPercentRounded = Math.round( scrollPercent * 100 / 10 ) * 10 | |
if (~passedMilestones.indexOf(scrollPercentRounded)) return | |
passedMilestones.push(scrollPercentRounded) | |
ga( | |
'COUNTERNAME.send', | |
'event', | |
'Scroll depth (' + location.hostname + location.pathname + ')', | |
scrollPercentRounded + '%', | |
{ nonInteraction: scrollPercentRounded < nonInteractiveThreshold } | |
) | |
}) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment