Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Track how far down your site's homepage visitors scroll
// This code assumes that each section of your homepage uses a "section" tag
// and each one has an id corresponding to its purpose: "testimonials", "pricing", etc
( function() {
var viewedSections = [];
$( window ).on( 'scroll', function() {
$( 'section' ).each( function() {
var sectionName = $( this ).attr( 'id' );
var distanceToTopOfViewport = this.getBoundingClientRect().top;
if ( sectionName && distanceToTopOfViewport < 600 ) {
var hasAlreadyBeenTracked = viewedSections.indexOf( sectionName ) !== -1;
if ( ! hasAlreadyBeenTracked ) {
viewedSections.push( sectionName );
analytics.record( 'Viewed Homepage Section', {
name: sectionName
} );
}
}
} );
} );
} )();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.