mirror of
https://github.com/plausible/analytics.git
synced 2024-12-23 09:33:19 +03:00
a7eab3f17b
Fixes #1225
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=window.localStorage,l=r.getElementById("plausible"),s=l.getAttribute("data-api")||(t=l.src.split("/"),e=t[0],i=t[2],e+"//"+i+"/api/event"),p=o&&o.plausible_ignore;function d(t){console.warn("Ignoring Event: "+t)}function c(t,e){if(/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(a.hostname)||"file:"===a.protocol)return d("localhost");if(!(window.phantom||window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)){if("true"==p)return d("localStorage flag");var i={};i.n=t,i.u=a.href,i.d=l.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",s,!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(i)),n.onreadystatechange=function(){4==n.readyState&&e&&e.callback&&e.callback()}}}function u(){n!==a.pathname&&(n=a.pathname,c("pageview"))}function w(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 h,f=window.history;f.pushState&&(h=f.pushState,f.pushState=function(){h.apply(this,arguments),u()},window.addEventListener("popstate",u)),r.addEventListener("click",w),r.addEventListener("auxclick",w);var g=window.plausible&&window.plausible.q||[];window.plausible=c;for(var v=0;v<g.length;v++)c.apply(this,g[v]);"prerender"===r.visibilityState?r.addEventListener("visibilitychange",function(){n||"visible"!==r.visibilityState||u()}):u()}(); |