user1778543
user1778543

Reputation: 167

Analytics event to fire without click

I have a web article where I want to know how many users are getting to a particular section. There is no click event associated with this section or reaching it, the user simply scrolls down the page.

How can I setup an event for this?

Thanks!

Upvotes: 0

Views: 58

Answers (1)

Brian Norman
Brian Norman

Reputation: 564

Try something like this, replacing my-element with the id of your section.

var scrollTop     = $(window).scrollTop(),
    elementOffset = $('#my-element').offset().top,
    distance      = (elementOffset - scrollTop),
    hasEventBeenSent = false;

function checkScrollDepth() {
  if (!hasEventBeenSent) {
    var scrollPos = $(document).scrollTop();
    if (scrollPos >= distance) {
      sendScrollEvent();
    }
  }
}

function sendScrollEvent() {
  ga('send', 'event', 'Scroll Event', 'event_action', 'event_label');
  hasEventBeenSent = true;
}

$(window).on('scroll', checkScrollDepth);

Upvotes: 1

Related Questions