forked from robflaherty/scroll-depth
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsegment-scroll-depth.min.js
1 lines (1 loc) · 2.16 KB
/
segment-scroll-depth.min.js
1
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof module&&module.exports?module.exports=a(require("jquery")):a(jQuery)}(function(a){"use strict";var g,b={minHeight:0,elements:[],percentage:!0,userTiming:!1,pixelDepth:!0,nonInteraction:!0},c=a(window),d=[],e=!1,f=0;a.scrollDepth=function(h){function j(a,b,c,d){g&&(g(a,{category:"Scroll Depth",label:b,eventNonInteraction:h.nonInteraction}),h.pixelDepth&&arguments.length>2&&c>f&&(f=c,g("Pixel Depth",{category:"Scroll Depth",label:n(c),eventNonInteraction:h.nonInteraction})),h.userTiming&&arguments.length>3&&g(a,{category:"Scroll Depth",label:b,eventTiming:d}))}function k(a){return{"25%":parseInt(.25*a,10),"50%":parseInt(.5*a,10),"75%":parseInt(.75*a,10),"100%":a-5}}function l(b,c,e){a.each(b,function(b,f){a.inArray(b,d)===-1&&c>=f&&(j("Percentage",b,c,e),d.push(b))})}function m(b,c,e){a.each(b,function(b,f){a.inArray(f,d)===-1&&a(f).length&&c>=a(f).offset().top&&(j("Elements",f,c,e),d.push(f))})}function n(a){return(250*Math.floor(a/250)).toString()}function o(){q()}function p(a,b){var c,d,e,f=null,g=0,h=function(){g=new Date,f=null,e=a.apply(c,d)};return function(){var i=new Date;g||(g=i);var j=b-(i-g);return c=this,d=arguments,j<=0?(clearTimeout(f),f=null,g=i,e=a.apply(c,d)):f||(f=setTimeout(h,j)),e}}function q(){e=!0,c.on("scroll.scrollDepth",p(function(){var b=a(document).height(),f=window.innerHeight?window.innerHeight:c.height(),g=c.scrollTop()+f,j=k(b),n=+new Date-i;return d.length>=h.elements.length+(h.percentage?4:0)?(c.off("scroll.scrollDepth"),void(e=!1)):(h.elements&&m(h.elements,g,n),void(h.percentage&&l(j,g,n)))},500))}var i=+new Date;h=a.extend({},b,h),a(document).height()<h.minHeight||(g=function(a,b){analytics.track(a,b,{integrations:{All:!1,"Google Analytics":!0,Mixpanel:!1,Salesforce:!1}})},a.scrollDepth.reset=function(){d=[],f=0,c.off("scroll.scrollDepth"),q()},a.scrollDepth.addElements=function(b){"undefined"!=typeof b&&a.isArray(b)&&(a.merge(h.elements,b),e||q())},a.scrollDepth.removeElements=function(b){"undefined"!=typeof b&&a.isArray(b)&&a.each(b,function(b,c){var e=a.inArray(c,h.elements),f=a.inArray(c,d);e!=-1&&h.elements.splice(e,1),f!=-1&&d.splice(f,1)})},o())}});