mirror of
https://github.com/plausible/analytics.git
synced 2024-12-26 11:02:52 +03:00
e80fc7bb8e
* Use `document.currentScript` to get script host * Add data-api
1 line
1.5 KiB
JavaScript
1 line
1.5 KiB
JavaScript
!function(){"use strict";var t,r=window.location,o=window.document,l=o.currentScript,s=l.getAttribute("data-api")||new URL(l.src).origin+"/api/event",p=window.localStorage.plausible_ignore,w=l&&l.getAttribute("data-exclude").split(",");function d(t){console.warn("Ignoring Event: "+t)}function e(t,e){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(r.hostname)||"file:"===r.protocol)return d("localhost");if(!(window.phantom||window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)){if("true"==p)return d("localStorage flag");if(w)for(var i=0;i<w.length;i++)if("pageview"==t&&r.pathname.match(new RegExp("^"+w[i].trim().replace(/\*\*/g,".*").replace(/([^\.])\*/g,"$1[^\\s/]*")+"/?$")))return d("exclusion rule");var n={};n.n=t,n.u=r.href,n.d=l.getAttribute("data-domain"),n.r=o.referrer||null,n.w=window.innerWidth,e&&e.meta&&(n.m=JSON.stringify(e.meta)),e&&e.props&&(n.p=JSON.stringify(e.props));var a=new XMLHttpRequest;a.open("POST",s,!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&e&&e.callback&&e.callback()}}}function i(){t!==r.pathname&&(t=r.pathname,e("pageview"))}var n,a=window.history;a.pushState&&(n=a.pushState,a.pushState=function(){n.apply(this,arguments),i()},window.addEventListener("popstate",i));var u=window.plausible&&window.plausible.q||[];window.plausible=e;for(var c=0;c<u.length;c++)e.apply(this,u[c]);"prerender"===o.visibilityState?o.addEventListener("visibilitychange",function(){t||"visible"!==o.visibilityState||i()}):i()}(); |