mirror of
https://github.com/plausible/analytics.git
synced 2024-12-23 09:33:19 +03:00
1 line
1.8 KiB
JavaScript
1 line
1.8 KiB
JavaScript
!function(){"use strict";var t,e,i,n,a=window.location,r=window.document,o=r.getElementById("plausible"),l=o.getAttribute("data-api")||(t=o.src.split("/"),e=t[0],i=t[2],e+"//"+i+"/api/event"),s=window.localStorage.plausible_ignore;function p(t){console.warn("Ignoring Event: "+t)}function d(t,e){if(/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(a.hostname)||"file:"===a.protocol)return p("localhost");if(!(window.phantom||window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)){if("true"==s)return p("localStorage flag");var i={};i.n=t,i.u=a.href,i.d=o.getAttribute("data-domain"),i.r=r.referrer||null,i.w=window.innerWidth,e&&e.meta&&(i.m=JSON.stringify(e.meta)),e&&e.props&&(i.p=JSON.stringify(e.props));var n=new XMLHttpRequest;n.open("POST",l,!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(i)),n.onreadystatechange=function(){4==n.readyState&&e&&e.callback&&e.callback()}}}function c(){n!==a.pathname&&(n=a.pathname,d("pageview"))}function u(t){for(var e=t.target,i="auxclick"==t.type&&2==t.which,n="click"==t.type;e&&(void 0===e.tagName||"a"!=e.tagName.toLowerCase()||!e.href);)e=e.parentNode;e&&e.href&&e.host&&e.host!==a.host&&((i||n)&&plausible("Outbound Link: Click",{props:{url:e.href}}),e.target&&!e.target.match(/^_(self|parent|top)$/i)||t.ctrlKey||t.metaKey||t.shiftKey||!n||(setTimeout(function(){a.href=e.href},150),t.preventDefault()))}var w,h=window.history;h.pushState&&(w=h.pushState,h.pushState=function(){w.apply(this,arguments),c()},window.addEventListener("popstate",c)),r.addEventListener("click",u),r.addEventListener("auxclick",u);var f=window.plausible&&window.plausible.q||[];window.plausible=d;for(var g=0;g<f.length;g++)d.apply(this,f[g]);"prerender"===r.visibilityState?r.addEventListener("visibilitychange",function(){n||"visible"!==r.visibilityState||c()}):c()}(); |