diff --git a/lib/plausible_web/plugs/tracker.ex b/lib/plausible_web/plugs/tracker.ex index 8bd4a1b29..31780c53d 100644 --- a/lib/plausible_web/plugs/tracker.ex +++ b/lib/plausible_web/plugs/tracker.ex @@ -2,7 +2,7 @@ defmodule PlausibleWeb.Tracker do import Plug.Conn use Agent - base_variants = ["hash", "outbound-links", "exclusions", "compat", "local"] + base_variants = ["hash", "outbound-links", "exclusions", "compat", "local", "manual"] base_filenames = ["plausible", "script"] # Generates Power Set of all variants diff --git a/priv/tracker/js/analytics.js b/priv/tracker/js/analytics.js index 8b024ba0a..c9f974dba 100644 --- a/priv/tracker/js/analytics.js +++ b/priv/tracker/js/analytics.js @@ -1 +1 @@ -!function(){"use strict";var t,a=window.location,o=window.document,e=window.localStorage,r=o.currentScript,s=r.getAttribute("data-api")||new URL(r.src).origin+"/api/event",l=e&&e.plausible_ignore;function w(t){console.warn("Ignoring Event: "+t)}function i(t,e){if(/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(a.hostname)||"file:"===a.protocol)return w("localhost");if(!(window.phantom||window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)){if("true"==l)return w("localStorage flag");var i={};i.n=t,i.u=a.href,i.d=r.getAttribute("data-domain"),i.r=o.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 n(){t!==a.pathname&&(t=a.pathname,i("pageview"))}var p,d=window.history;d.pushState&&(p=d.pushState,d.pushState=function(){p.apply(this,arguments),n()},window.addEventListener("popstate",n));var u=window.plausible&&window.plausible.q||[];window.plausible=i;for(var c=0;c a.length > 0).map(a => a.sort()); compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.js')) diff --git a/tracker/src/plausible.js b/tracker/src/plausible.js index f48a6333b..7aa07b09d 100644 --- a/tracker/src/plausible.js +++ b/tracker/src/plausible.js @@ -15,7 +15,6 @@ {{#if exclusions}} var excludedPaths = scriptEl && scriptEl.getAttribute('data-exclude').split(','); {{/if}} - var lastPage; function warn(reason) { console.warn('Ignoring Event: ' + reason); @@ -75,20 +74,6 @@ } } - function page() { - {{#unless hash}} - if (lastPage === location.pathname) return; - {{/unless}} - lastPage = location.pathname - trigger('pageview') - } - - function handleVisibilityChange() { - if (!lastPage && document.visibilityState === 'visible') { - page() - } - } - {{#if outbound_links}} function handleOutbound(event) { var link = event.target; @@ -120,20 +105,6 @@ } {{/if}} - {{#if hash}} - window.addEventListener('hashchange', page) - {{else}} - var his = window.history - if (his.pushState) { - var originalPushState = his['pushState'] - his.pushState = function() { - originalPushState.apply(this, arguments) - page(); - } - window.addEventListener('popstate', page) - } - {{/if}} - {{#if outbound_links}} registerOutboundLinkEvents() {{/if}} @@ -144,9 +115,42 @@ trigger.apply(this, queue[i]) } - if (document.visibilityState === 'prerender') { - document.addEventListener("visibilitychange", handleVisibilityChange); - } else { - page() - } + {{#unless manual}} + var lastPage; + + function page() { + {{#unless hash}} + if (lastPage === location.pathname) return; + {{/unless}} + lastPage = location.pathname + trigger('pageview') + } + + {{#if hash}} + window.addEventListener('hashchange', page) + {{else}} + var his = window.history + if (his.pushState) { + var originalPushState = his['pushState'] + his.pushState = function() { + originalPushState.apply(this, arguments) + page(); + } + window.addEventListener('popstate', page) + } + {{/if}} + + function handleVisibilityChange() { + if (!lastPage && document.visibilityState === 'visible') { + page() + } + } + + + if (document.visibilityState === 'prerender') { + document.addEventListener("visibilitychange", handleVisibilityChange); + } else { + page() + } + {{/unless}} })();