Skip to content

Instantly share code, notes, and snippets.

@selwynpolit
Last active February 10, 2016 19:33
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 selwynpolit/7c42168523863ecaa96d to your computer and use it in GitHub Desktop.
Save selwynpolit/7c42168523863ecaa96d to your computer and use it in GitHub Desktop.
<script>
(function(dataLayer, $) {
var reachedBottom = false;
//returns true if view is at or below element
function scrolledToBottom(elem) {
var $elem = $(elem);
if ($elem.length > 0) {
var $window = $(window);
var docViewTop = $window.scrollTop();
var docViewBottom = docViewTop + $window.height();
var elemTop = $elem.offset().top;
var elemBottom = elemTop + $elem.height();
return (elemBottom <= docViewBottom);
} else {
return false;
}
}
//sends an event if view is at or below element
$(document).scroll(function() {
reachedBottom = scrolledToBottom('#waypoints_identifier');
if (reachedBottom) {
dataLayer.push({'event': 'scrolled-to-bottom'});
$(document).off('scroll');
}
});
})(window.dataLayer || [], window.$ || window.jQuery);
</script>
@selwynpolit
Copy link
Author

I suspect the problem is at line 14.

@jacshfr
Copy link

jacshfr commented Feb 10, 2016

I've added a condition to account for if the element is not found on the page. I've also set a trigger on the tag to make sure it is only firing on actual blog pages(Drupal.settings.WholeFoods.contentType='blog_post').

@tmbritton
Copy link

I think the problem is this line: 26: reachedBottom = scrolledToBottom('#waypoints_identifier');

waypoints_identifier is not an element that exists in the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment