mirror of
https://github.com/plausible/analytics.git
synced 2024-12-23 17:44:43 +03:00
1 line
2.1 KiB
JavaScript
1 line
2.1 KiB
JavaScript
!function(){"use strict";var e=window.document.currentScript,r=new URL(e.src).origin;function o(e){console.warn("[Plausible] Ignoring event because "+e)}function a(){var e,i,n=JSON.parse((e="plausible_user",(i=document.cookie.match(new RegExp("(?:^|; )"+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,"\\$1")+"=([^;]*)")))?decodeURIComponent(i[1]):null));return n?{initial_referrer:n.initial_referrer&&decodeURIComponent(n.initial_referrer),initial_source:n.initial_source&&decodeURIComponent(n.initial_source)}:(n={initial_referrer:window.document.referrer||null,initial_source:getSourceFromQueryParam()},function(e,i){var n=new Date;n.setTime(n.getTime()+94608e6);var t="; expires="+n.toUTCString();document.cookie=e+"="+(i||"")+t+"; samesite=strict; path=/"}("plausible_user",JSON.stringify({initial_referrer:n.initial_referrer&&encodeURIComponent(n.initial_referrer),initial_source:n.initial_source&&encodeURIComponent(n.initial_source)})),n)}function i(e,i){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(window.location.hostname))return o("website is running locally");if("file:"===window.location.protocol)return o("website is running locally");if("prerender"===window.document.visibilityState)return o("document is prerendering");var n=c.trackAcquisition?a():{};n.n=e,n.u=window.location.href,n.d=c.domain,n.r=window.document.referrer||null,n.w=window.innerWidth;var t=new XMLHttpRequest;t.open("POST",r+"/api/event",!0),t.setRequestHeader("Content-Type","text/plain"),t.send(JSON.stringify(n)),t.onreadystatechange=function(){t.readyState==XMLHttpRequest.DONE&&i&&i.callback&&i.callback()}}function n(e){i("pageview",e)}try{var c={domain:window.location.hostname},t={page:n,trigger:i,trackPushState:function(){var e,i=window.history;i.pushState&&(e=i.pushState,i.pushState=function(){e.apply(this,arguments),n()}),window.addEventListener("popstate",n)},configure:function(e,i){c[e]=i}},s=window.plausible.q||[];window.plausible=function(){var e=[].slice.call(arguments),i=e.shift();t[i].apply(this,e)};for(var l=0;l<s.length;l++)window.plausible.apply(this,s[l])}catch(e){(new Image).src=r+"/api/error?message="+encodeURIComponent(e.message)}}(); |