Skip to content

Instantly share code, notes, and snippets.

@bebosudo
Last active February 2, 2021 20:03
Show Gist options
  • Save bebosudo/01ef89feb2a9213fb2825e279c60247a to your computer and use it in GitHub Desktop.
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/
<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