Last active
February 2, 2021 20:03
-
-
Save bebosudo/01ef89feb2a9213fb2825e279c60247a to your computer and use it in GitHub Desktop.
Track user scrolling through page with GA - Copied from: https://pastebin.com/8wjY9eHa, see original article https://growthrocks.com/blog/scroll-tracking-google-analytics/
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
<script> | |
document.addEventListener('scroll', function(){ | |
var h = document.documentElement, | |
b = document.body, | |
st = 'scrollTop', | |
sh = 'scrollHeight'; | |
var percent = parseInt ( (h[st]||b[st]) / ((h[sh]||b[sh]) - h.clientHeight) * 100); | |
console.log(percent + "%"); | |
if (percent == 25) | |
{ | |
ga('send', 'event', 'Scrolling', 'moreThan25%', '{{Page URL}}', { 'nonInteraction': 1 }); | |
} | |
else if (percent == 50) | |
{ | |
ga('send', 'event', 'Scrolling', 'moreThan50%', '{{Page URL}}', { 'nonInteraction': 1 }); | |
} | |
else if (percent == 75) | |
{ | |
ga('send', 'event', 'Scrolling', 'moreThan75%', '{{Page URL}}', { 'nonInteraction': 1 }); | |
} | |
else if (percent == 90) | |
{ | |
ga('send', 'event', 'Scrolling', 'moreThan90%', '{{Page URL}}', { 'nonInteraction': 1 }); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment