Created
May 18, 2015 14:24
-
-
Save ibrennan/3046c652dfb0aead815c to your computer and use it in GitHub Desktop.
Google Analytics Scroll Position 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
// Implementation for tracking the vertical scroll of a page and firing events at 25%, 50%, 75%, 100% positions | |
// Requires jQuery (feel free to fork a version that doesn't!) | |
var scrollPercentages = { | |
scroll25: false, | |
scroll50: false, | |
scroll75: false, | |
scroll100: false | |
} | |
$(window).scroll(function(){ | |
var percentage = ( ( $(document).scrollTop() + window.innerHeight ) / $(document).height() ) * 100 | |
if(percentage === 100 && scrollPercentages.scroll100 === false){ | |
scrollPercentages.scroll100 = true; | |
ga('send', 'event', { eventCategory: 'Scroll', eventAction: '100%'}); | |
} else if(percentage > 75 && scrollPercentages.scroll75 === false){ | |
scrollPercentages.scroll75 = true; | |
ga('send', 'event', { eventCategory: 'Scroll', eventAction: '75%'}); | |
} else if(percentage > 50 && scrollPercentages.scroll50 === false){ | |
scrollPercentages.scroll50 = true; | |
ga('send', 'event', { eventCategory: 'Scroll', eventAction: '50%'}); | |
} else if(percentage > 25 && scrollPercentages.scroll25 === false){ | |
scrollPercentages.scroll25 = true; | |
ga('send', 'event', { eventCategory: 'Scroll', eventAction: '25%'}); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment