2 lines
No EOL
6.3 KiB
JavaScript
2 lines
No EOL
6.3 KiB
JavaScript
var e,t,n,i,o={};function r(){return new URL(e.src).origin+"/api/event"}function a(e){return Object.assign(e,{autoCapturePageviews:!1!==e.autoCapturePageviews,logging:!1!==e.logging,bindToWindow:!1!==e.bindToWindow})}function u(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 s(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 c=!1,l=location.href,d={},f=-1,p=0,v=0;function m(){c&&(g(),n=E(),i=L())}function h(e){t=!1,l=e.u,d=e.p,f=-1,v=0,p=Date.now(),y()}function w(){t=!0}function g(){var e=k();if(!t&&(f<i||e>=3e3)){f=i;var r={n:"engagement",sd:Math.round(i/n*100),d:o.domain,u:l,p:d,e:e,v:34};p=0,v=0,o.hashBasedRouting&&(r.h=1),s(o.endpoint,r)}}function b(){"visible"===document.visibilityState&&document.hasFocus()&&0===p?p=Date.now():"hidden"!==document.visibilityState&&document.hasFocus()||(v=k(),p=0,g())}function y(){c||(document.addEventListener("visibilitychange",b),window.addEventListener("blur",b),window.addEventListener("focus",b),c=!0)}function k(){return p?v+(Date.now()-p):v}function E(){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 L(){var e,t;return e=window.innerHeight,t=window.scrollY,n<=e?n:t+e}function S(){n=E(),i=L(),new ResizeObserver(function(){n=E()}).observe(document.documentElement),document.addEventListener("scroll",function(){var e=L();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(location.hostname)||"file:"===location.protocol)return x(e,t,"localhost");if((window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)&&!window.__plausible)return x(e,t)}try{if("true"===window.localStorage.plausible_ignore)return x(e,t,"localStorage flag")}catch(e){}var r={};r.n=e,r.v=34;var a=t&&(t.u||t.url);if(r.u=a||location.href,r.d=o.domain,r.r=document.referrer||null,t&&t.props&&(r.p=t.props),t&&!1===t.interactive&&(r.i=!1),t&&t.revenue&&(r.$=t.revenue),o.customProperties){var n=o.customProperties;"function"==typeof n&&(n=o.customProperties(e)),"object"==typeof n&&(r.p=Object.assign({},n,r.p))}if(o.hashBasedRouting&&(r.h=1),"function"==typeof o.transformRequest&&!(r=o.transformRequest(r)))return x(e,t,"transformRequest");i&&h(r),s(o.endpoint,r,t)}function x(e,t,n){n&&o.logging&&console.warn("Ignoring Event: "+n),t&&t.callback&&t.callback(),"pageview"===e&&w()}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"],D=1,_=3,N=DEFAULT_FILE_TYPES;function P(e){for(;e&&(void 0===e.tagName||!C(e)||!e.href);)e=e.parentNode;return e}function C(e){return e&&e.tagName&&"a"===e.tagName.toLowerCase()}function R(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 T(e){if("auxclick"!==e.type||e.button===D){var t=P(e.target),n=t&&"string"==typeof t.href&&t.href.split("?")[0];if(!I(t,0)){if(o.outboundLinks&&O(t))return F(e,t,{name:"Outbound Link: Click",props:{url:t.href}});if(o.fileDownloads&&z(n))return F(e,t,{name:"File Download",props:{url:n}})}}}function F(e,t,n){var i;(i={props:n.props}).revenue=n.revenue,track(n.name,i)}function O(e){return e&&"string"==typeof e.href&&e.host&&e.host!==location.host}function z(e){if(!e)return!1;var t=e.split(".").pop();return N.some(function(e){return e===t})}function H(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 I(e,t){return!!e&&!(t>_)&&(!!H(e)||I(e.parentNode,t+1))}function j(e){var t=H(e)?e:e&&e.parentNode,n={name:null,props:{}};n.revenue={};var i=t&&t.classList;if(!i)return n;for(var o=0;o<i.length;o++){var r,a,u=i.item(o),s=u.match(/plausible-event-(.+)(=|--)(.+)/);s&&(r=s[1],a=s[3].replace(/\+/g," "),"name"==r.toLowerCase()?n.name=a:n.props[r]=a);var c=u.match(/plausible-revenue-(.+)(=|--)(.+)/);c&&(r=c[1],a=c[3],n.revenue[r]=a)}return n}function A(){function e(e){if("auxclick"!==e.type||e.button===D){for(var t,n,i,o=e.target,r=0;r<=_&&o;r++){if((t=o)&&t.tagName&&"form"===t.tagName.toLowerCase())return;C(o)&&(n=o),H(o)&&(i=o),o=o.parentNode}if(i){var a=j(i);if(n)a.props.url=n.href,F(e,n,a);else{var u={};u.props=a.props,u.revenue=a.revenue,track(a.name,u)}}}}document.addEventListener("click",T),document.addEventListener("auxclick",T),o.fileDownloads&&"object"==typeof o.fileDownloads&&Array.isArray(o.fileDownloads.fileExtensions)&&(N=o.fileDownloads.fileExtensions),o.formSubmissions&&document.addEventListener("submit",function(e){(e.target.hasAttribute("novalidate")||e.target.checkValidity())&&(I(e.target,0)||track("Form: Submission"))},!0),document.addEventListener("submit",function(e){var t,n=j(e.target);n.name&&((t={props:n.props}).revenue=n.revenue,track(n.name,t))}),document.addEventListener("click",e),document.addEventListener("auxclick",e)}function $(track){function e(e){(o.hashBasedRouting||!e||t!==location.pathname)&&(t=location.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 r=i.pushState;i.pushState=function(){r.apply(this,arguments),n()},window.addEventListener("popstate",n)}}"hidden"===document.visibilityState||"prerender"===document.visibilityState?document.addEventListener("visibilitychange",function(){t||"visible"!==document.visibilityState||e()}):e(),window.addEventListener("pageshow",function(t){t.persisted&&e()})}function init(e){u(a(e||{})),S(),o.autoCapturePageviews&&$(track),A(),o.bindToWindow&&"undefined"!=typeof window&&(window.plausible=track,window.plausible.s="npm",window.plausible.v=34,window.plausible.l=!0)}
|
|
export { init, track, DEFAULT_FILE_TYPES } |