Skip to content

Instantly share code, notes, and snippets.

@BorisChumichev
Forked from Gegam/scrolldepth.js
Last active June 29, 2017 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BorisChumichev/1f35b2f5aec072dfb34febc592d79d7d to your computer and use it in GitHub Desktop.
Save BorisChumichev/1f35b2f5aec072dfb34febc592d79d7d to your computer and use it in GitHub Desktop.
Google analytics scroll depth tracking
;(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