Guest User

Google Analytics Scroll Events

a guest
Jan 22nd, 2020
1,572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const scrh =  document.documentElement,
  2.       scrb =  document.body,
  3.       scrst =  'scrollTop',
  4.       scrsh =  'scrollHeight';
  5. let maxp = 0;
  6. document.addEventListener('scroll', function() {
  7.         const percent = parseInt ( (scrh[scrst]||scrb[scrst]) / ((scrh[scrsh]||scrb[scrsh]) - scrh.clientHeight) * 100);
  8.         if (percent >= 25 && maxp < 25) {
  9.             ga('send', 'event', 'Scrolling', 'moreThan25%', window.location.href, { 'nonInteraction': 1 });
  10.         } else if (percent >= 50 && maxp < 50) {
  11.             ga('send', 'event', 'Scrolling', 'moreThan50%', window.location.href, { 'nonInteraction': 1 });
  12.         } else if (percent >= 75 && maxp < 75) {
  13.             ga('send', 'event', 'Scrolling', 'moreThan75%', window.location.href, { 'nonInteraction': 1 });
  14.         } else if (percent >= 90 && maxp < 90) {
  15.             ga('send', 'event', 'Scrolling', 'moreThan90%', window.location.href, { 'nonInteraction': 1 });
  16.         }
  17.         if (percent > maxp) {
  18.             maxp = percent;
  19.         }
  20. });
Add Comment
Please, Sign In to add comment