macobo-test-tracker
Version:
Plausible Analytics official frontend tracking library
2 lines • 6.53 kB
JavaScript
var e,t,n,i,r=window.location,a=window.document,o={};function s(){return new URL(e.src).origin+"/api/event"}function u(e){return Object.assign(e,{autoCapturePageviews:!1!==e.autoCapturePageviews,logging:!1!==e.logging,bindToWindow:!1!==e.bindToWindow})}function c(e){if(o.isInitialized)throw Error("plausible.init() can only be called once");if(!e||!e.domain)throw Error("plausible.init(): domain argument is required");e.endpoint||(e.endpoint="https://plausible.io/api/event"),Object.assign(o,e),o.isInitialized=!0}function l(e,t,n){window.fetch&&fetch(e,{method:"POST",headers:{"Content-Type":"text/plain"},keepalive:!0,body:JSON.stringify(t)}).then(function(e){n&&n.callback&&n.callback({status:e.status})}).catch(function(e){n&&n.callback&&n.callback({error:e})})}var d=!1,f=location.href,p={},v=-1,w=0,h=0;function m(){d&&(y(),n=S(),i=x())}function g(e){t=!1,f=e.u,p=e.p,v=-1,h=0,w=Date.now(),E()}function b(){t=!0}function y(){var e=L();if(!t&&(v<i||e>=3e3)){v=i;var r={n:"engagement",sd:Math.round(i/n*100),d:o.domain,u:f,p:p,e:e,v:27};w=0,h=0,o.hashBasedRouting&&(r.h=1),l(o.endpoint,r)}}function k(){"visible"===document.visibilityState&&document.hasFocus()&&0===w?w=Date.now():"hidden"!==document.visibilityState&&document.hasFocus()||(h=L(),w=0,y())}function E(){d||(document.addEventListener("visibilitychange",k),window.addEventListener("blur",k),window.addEventListener("focus",k),d=!0)}function L(){return w?h+(Date.now()-w):h}function S(){var e=document.body||{},t=document.documentElement||{};return Math.max(e.scrollHeight||0,e.offsetHeight||0,e.clientHeight||0,t.scrollHeight||0,t.offsetHeight||0,t.clientHeight||0)}function x(){var e=document.body||{},t=document.documentElement||{},i=window.innerHeight||t.clientHeight||0,r=window.scrollY||t.scrollTop||e.scrollTop||0;return n<=i?n:r+i}function D(){n=S(),i=x(),window.addEventListener("load",function(){n=S();var e=0,t=setInterval(function(){n=S(),15==++e&&clearInterval(t)},200)}),document.addEventListener("scroll",function(){n=S();var e=x();e>i&&(i=e)})}function track(e,t){if(!o.isInitialized)throw Error("plausible.track() can only be called after plausible.init()");var n,i="pageview"===e;if(i&&m(),!o.captureOnLocalhost){if(/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(r.hostname)||"file:"===r.protocol)return T(e,t,"localhost");if((window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)&&!window.__plausible)return T(e,t)}try{if("true"===window.localStorage.plausible_ignore)return T(e,t,"localStorage flag")}catch(e){}var s={};s.n=e,s.v=27;var u=t&&(t.u||t.url);if(s.u=u||r.href,s.d=o.domain,s.r=a.referrer||null,t&&t.props&&(s.p=t.props),t&&!1===t.interactive&&(s.i=!1),t&&t.revenue&&(s.$=t.revenue),o.customProperties){var n=o.customProperties;"function"==typeof n&&(n=o.customProperties(e)),"object"==typeof n&&(s.p=Object.assign({},n,s.p))}if(o.hashBasedRouting&&(s.h=1),"function"==typeof o.transformRequest&&!(s=o.transformRequest(s)))return T(e,t,"transformRequest");i&&g(s),l(o.endpoint,s,t)}function T(e,t,n){n&&o.logging&&console.warn("Ignoring Event: "+n),t&&t.callback&&t.callback(),"pageview"===e&&b()}var DEFAULT_FILE_TYPES=["pdf","xlsx","docx","txt","rtf","csv","exe","key","pps","ppt","pptx","7z","pkg","rar","gz","zip","avi","mov","mp4","mpeg","wmv","midi","mp3","wav","wma","dmg"],_=1,N=3,P=DEFAULT_FILE_TYPES;function C(e){for(;e&&(void 0===e.tagName||!F(e)||!e.href);)e=e.parentNode;return e}function F(e){return e&&e.tagName&&"a"===e.tagName.toLowerCase()}function H(e,t){if(e.defaultPrevented)return!1;var n=t.target;return(!n||"string"==typeof n&&!!n.match(/^_(self|parent|top)$/i))&&!e.ctrlKey&&!e.metaKey&&!e.shiftKey&&"click"===e.type}function I(e){if("auxclick"!==e.type||e.button===_){var t=C(e.target),n=t&&"string"==typeof t.href&&t.href.split("?")[0];if(!A(t,0)){if(o.outboundLinks&&O(t))return R(e,t,{name:"Outbound Link: Click",props:{url:t.href}});if(o.fileDownloads&&z(n))return R(e,t,{name:"File Download",props:{url:n}})}}}function R(e,t,n){var i={props:n.props};i.revenue=n.revenue,track(n.name,i)}function O(e){return e&&"string"==typeof e.href&&e.host&&e.host!==r.host}function z(e){if(!e)return!1;var t=e.split(".").pop();return P.some(function(e){return e===t})}function j(e){var t=e&&e.classList;if(t){for(var n=0;n<t.length;n++)if(t.item(n).match(/plausible-event-name(=|--)(.+)/))return!0}return!1}function A(e,t){return!!e&&!(t>N)&&(!!j(e)||A(e.parentNode,t+1))}function $(e){var t=j(e)?e:e&&e.parentNode,n={name:null,props:{}};n.revenue={};var i=t&&t.classList;if(!i)return n;for(var r=0;r<i.length;r++){var a=i.item(r),o=a.match(/plausible-event-(.+)(=|--)(.+)/);if(o){var s=o[1],u=o[3].replace(/\+/g," ");"name"==s.toLowerCase()?n.name=u:n.props[s]=u}var c=a.match(/plausible-revenue-(.+)(=|--)(.+)/);if(c){var s=c[1],u=c[3];n.revenue[s]=u}}return n}function q(){function e(e){if("auxclick"!==e.type||e.button===_){for(var t,n,i,r=e.target,a=0;a<=N&&r;a++){if((t=r)&&t.tagName&&"form"===t.tagName.toLowerCase())return;F(r)&&(n=r),j(r)&&(i=r),r=r.parentNode}if(i){var o=$(i);if(n)o.props.url=n.href,R(e,n,o);else{var s={};s.props=o.props,s.revenue=o.revenue,track(o.name,s)}}}}a.addEventListener("click",I),a.addEventListener("auxclick",I),o.fileDownloads&&"object"==typeof o.fileDownloads&&Array.isArray(o.fileDownloads.fileExtensions)&&(P=o.fileDownloads.fileExtensions),o.formSubmissions&&a.addEventListener("submit",function(e){(e.target.hasAttribute("novalidate")||e.target.checkValidity())&&(A(e.target,0)||track("Form: Submission"))},!0),a.addEventListener("submit",function(e){var t,n=$(e.target);if(n.name){var t={props:n.props};t.revenue=n.revenue,track(n.name,t)}}),a.addEventListener("click",e),a.addEventListener("auxclick",e)}function B(track){function e(e){(o.hashBasedRouting||!e||t!==r.pathname)&&(t=r.pathname,track("pageview"))}var t,n=function(){e(!0)};if(o.hashBasedRouting)window.addEventListener("hashchange",n);else{var i=window.history;if(i.pushState){var s=i.pushState;i.pushState=function(){s.apply(this,arguments),n()},window.addEventListener("popstate",n)}}"hidden"===a.visibilityState||"prerender"===a.visibilityState?a.addEventListener("visibilitychange",function(){t||"visible"!==a.visibilityState||e()}):e(),window.addEventListener("pageshow",function(t){t.persisted&&e()})}function init(e){c(u(e||{})),D(),o.autoCapturePageviews&&B(track),q(),o.bindToWindow&&"undefined"!=typeof window&&(window.plausible=track,window.plausible.s="npm",window.plausible.v=27,window.plausible.l=!0)}
export { init, track, DEFAULT_FILE_TYPES }