Skip to content

Instantly share code, notes, and snippets.

@mattm
Last active September 5, 2016 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattm/25759f145154b5932617ebb75e5c4666 to your computer and use it in GitHub Desktop.
Save mattm/25759f145154b5932617ebb75e5c4666 to your computer and use it in GitHub Desktop.
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