From fb7a3fef896b2e87eacd829399958450adfb3785 Mon Sep 17 00:00:00 2001 From: Vignesh Joglekar Date: Fri, 22 Jan 2021 05:32:40 -0600 Subject: [PATCH] Adds data-exclude support and localStorage.plausible_ignore support (#489) * Adds data-exclude support and plausible_ignore support * Splits exclusion into separate script option * localStorage parsing upgrades * Additional script type additions I'm unsure about the formatting decision in tracker.ex - lmk. * Adds new compiled files This will certainly have conflicts with my other PRs related to the tracking scripts right now, I'll make one extra PR after both are done to ensure they're consolidated into the compiled scripts. * Moves localStorage blocker out of special script * Changelog * Second thoughts on localStorage exclusion * Updates `*` to not match `/` or whitespace * Fix formatting * Removes zero-length asterisks * Adds support for double glob, zero-length replacements * Update to reduce size+allow localStorage exclude Co-authored-by: Uku Taht --- CHANGELOG.md | 1 + lib/plausible_web/plugs/tracker.ex | 17 ++++++++++++++--- priv/tracker/js/analytics.js | 2 +- priv/tracker/js/plausible.exclusions.js | 1 + .../js/plausible.exclusions.outbound-links.js | 1 + priv/tracker/js/plausible.hash.exclusions.js | 1 + .../plausible.hash.exclusions.outbound-links.js | 1 + priv/tracker/js/plausible.hash.js | 2 +- .../tracker/js/plausible.hash.outbound-links.js | 2 +- priv/tracker/js/plausible.js | 2 +- priv/tracker/js/plausible.outbound-links.js | 2 +- tracker/compile.js | 4 ++++ tracker/src/plausible.js | 17 ++++++++++++++++- 13 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 priv/tracker/js/plausible.exclusions.js create mode 100644 priv/tracker/js/plausible.exclusions.outbound-links.js create mode 100644 priv/tracker/js/plausible.hash.exclusions.js create mode 100644 priv/tracker/js/plausible.hash.exclusions.outbound-links.js diff --git a/CHANGELOG.md b/CHANGELOG.md index dbd513820..ca31ae755 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. - Unique Visitors (last 30 min) as a top stat in realtime view plausible/analytics#500 - Pinned filter and date selector rows while scrolling plausible/analytics#472 - Escape keyboard shortcut to clear all filters plausible/analytics#625 +- Tracking exclusions, see our documentation [here](https://docs.plausible.io/excluding) and [here](https://docs.plausible.io/excluding-pages) for details plausible/analytics#489 - Keybindings for selecting dates/ranges plausible/analytics#630 ### Changed diff --git a/lib/plausible_web/plugs/tracker.ex b/lib/plausible_web/plugs/tracker.ex index b55a99d38..b7f51524c 100644 --- a/lib/plausible_web/plugs/tracker.ex +++ b/lib/plausible_web/plugs/tracker.ex @@ -4,14 +4,25 @@ defmodule PlausibleWeb.Tracker do @templates [ "plausible.js", - "plausible.hash.js", + "plausible.exclusions.js", + "plausible.hash.exclusions.js", "plausible.hash.outbound-links.js", - "plausible.outbound-links.js", + "plausible.hash.exclusions.outbound-links.js", + "plausible.exclusions.outbound-links.js", "p.js" ] @aliases %{ "plausible.js" => ["analytics.js"], - "plausible.hash.outbound-links.js" => ["plausible.outbound-links.hash.js"] + "plausible.hash.outbound-links.js" => ["plausible.outbound-links.hash.js"], + "plausible.hash.exclusions.js" => ["plausible.exclusions.hash.js"], + "plausible.exclusions.outbound-links.js" => ["plausible.outbound-links.exclusions.js"], + "plausible.hash.exclusions.outbound-links.js" => [ + "plausible.exclusions.hash.outbound-links.js", + "plausible.exclusions.outbound-links.hash.js", + "plausible.hash.outbound-links.exclusions.js", + "plausible.outbound-links.hash.exclusions.js", + "plausible.outbound-links.exclusions.hash.js" + ] } #  1 hour diff --git a/priv/tracker/js/analytics.js b/priv/tracker/js/analytics.js index ad8f6d368..98b0ec827 100644 --- a/priv/tracker/js/analytics.js +++ b/priv/tracker/js/analytics.js @@ -1 +1 @@ -!function(i,r){"use strict";var e,o=i.location,s=i.document,t=s.querySelector('[src*="'+r+'"]'),l=t&&t.getAttribute("data-domain");function n(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var n,a;i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver||((n={}).n=e,n.u=o.href,n.d=l,n.r=s.referrer||null,n.w=i.innerWidth,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=JSON.stringify(t.props)),(a=new XMLHttpRequest).open("POST",r+"/api/event",!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()})}function a(){e!==o.pathname&&(e=o.pathname,n("pageview"))}try{var p,c=i.history;c.pushState&&(p=c.pushState,c.pushState=function(){p.apply(this,arguments),a()},i.addEventListener("popstate",a));var u=i.plausible&&i.plausible.q||[];i.plausible=n;for(var h=0;h"); \ No newline at end of file +!function(r,i){"use strict";var e,o=r.location,s=r.document,t=s.querySelector('[src*="'+i+'"]'),l=t&&t.getAttribute("data-domain"),p=r.localStorage.plausible_ignore;function c(e){console.warn("Ignoring Event: "+e)}function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return c("localhost");if(!(r.phantom||r._phantom||r.__nightmare||r.navigator.webdriver)){if("true"==p)return c("localStorage flag");var a={};a.n=e,a.u=o.href,a.d=l,a.r=s.referrer||null,a.w=r.innerWidth,t&&t.meta&&(a.m=JSON.stringify(t.meta)),t&&t.props&&(a.p=JSON.stringify(t.props));var n=new XMLHttpRequest;n.open("POST",i+"/api/event",!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(a)),n.onreadystatechange=function(){4==n.readyState&&t&&t.callback&&t.callback()}}}function n(){e!==o.pathname&&(e=o.pathname,a("pageview"))}try{var u,h=r.history;h.pushState&&(u=h.pushState,h.pushState=function(){u.apply(this,arguments),n()},r.addEventListener("popstate",n));var g=r.plausible&&r.plausible.q||[];r.plausible=a;for(var f=0;f"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.exclusions.js b/priv/tracker/js/plausible.exclusions.js new file mode 100644 index 000000000..f4f39fbad --- /dev/null +++ b/priv/tracker/js/plausible.exclusions.js @@ -0,0 +1 @@ +!function(i,o){"use strict";var e,l=i.location,s=i.document,t=s.querySelector('[src*="'+o+'"]'),p=t&&t.getAttribute("data-domain"),c=i.localStorage.plausible_ignore,u=t&&t.getAttribute("data-exclude").split(",");function g(e){console.warn("Ignoring Event: "+e)}function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(l.hostname)||"file:"===l.protocol)return g("localhost");if(!(i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver)){if("true"==c)return g("localStorage flag");if(u)for(var a=0;a"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.exclusions.outbound-links.js b/priv/tracker/js/plausible.exclusions.outbound-links.js new file mode 100644 index 000000000..6b8729d96 --- /dev/null +++ b/priv/tracker/js/plausible.exclusions.outbound-links.js @@ -0,0 +1 @@ +!function(n,o){"use strict";var e,l=n.location,s=n.document,t=s.querySelector('[src*="'+o+'"]'),c=t&&t.getAttribute("data-domain"),p=n.localStorage.plausible_ignore,u=t&&t.getAttribute("data-exclude").split(",");function h(e){console.warn("Ignoring Event: "+e)}function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(l.hostname)||"file:"===l.protocol)return h("localhost");if(!(n.phantom||n._phantom||n.__nightmare||n.navigator.webdriver)){if("true"==p)return h("localStorage flag");if(u)for(var a=0;a"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.hash.exclusions.js b/priv/tracker/js/plausible.hash.exclusions.js new file mode 100644 index 000000000..efa52f734 --- /dev/null +++ b/priv/tracker/js/plausible.hash.exclusions.js @@ -0,0 +1 @@ +!function(i,o){"use strict";var e,l=i.location,s=i.document,t=s.querySelector('[src*="'+o+'"]'),c=t&&t.getAttribute("data-domain"),p=i.localStorage.plausible_ignore,u=t&&t.getAttribute("data-exclude").split(",");function g(e){console.warn("Ignoring Event: "+e)}function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(l.hostname)||"file:"===l.protocol)return g("localhost");if(!(i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver)){if("true"==p)return g("localStorage flag");if(u)for(var a=0;a"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.hash.exclusions.outbound-links.js b/priv/tracker/js/plausible.hash.exclusions.outbound-links.js new file mode 100644 index 000000000..3f854ae4a --- /dev/null +++ b/priv/tracker/js/plausible.hash.exclusions.outbound-links.js @@ -0,0 +1 @@ +!function(i,o){"use strict";var e,l=i.location,s=i.document,t=s.querySelector('[src*="'+o+'"]'),c=t&&t.getAttribute("data-domain"),p=i.localStorage.plausible_ignore,u=t&&t.getAttribute("data-exclude").split(",");function h(e){console.warn("Ignoring Event: "+e)}function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(l.hostname)||"file:"===l.protocol)return h("localhost");if(!(i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver)){if("true"==p)return h("localStorage flag");if(u)for(var a=0;a"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.hash.js b/priv/tracker/js/plausible.hash.js index d13164855..83501f190 100644 --- a/priv/tracker/js/plausible.hash.js +++ b/priv/tracker/js/plausible.hash.js @@ -1 +1 @@ -!function(i,r){"use strict";var e,o=i.location,s=i.document,t=s.querySelector('[src*="'+r+'"]'),l=t&&t.getAttribute("data-domain");function n(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var n,a;i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver||((n={}).n=e,n.u=o.href,n.d=l,n.r=s.referrer||null,n.w=i.innerWidth,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=JSON.stringify(t.props)),n.h=1,(a=new XMLHttpRequest).open("POST",r+"/api/event",!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()})}function a(){e=o.pathname,n("pageview")}try{i.addEventListener("hashchange",a);var c=i.plausible&&i.plausible.q||[];i.plausible=n;for(var p=0;p"); \ No newline at end of file +!function(r,i){"use strict";var e,o=r.location,l=r.document,t=l.querySelector('[src*="'+i+'"]'),s=t&&t.getAttribute("data-domain"),c=r.localStorage.plausible_ignore;function p(e){console.warn("Ignoring Event: "+e)}function n(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return p("localhost");if(!(r.phantom||r._phantom||r.__nightmare||r.navigator.webdriver)){if("true"==c)return p("localStorage flag");var n={};n.n=e,n.u=o.href,n.d=s,n.r=l.referrer||null,n.w=r.innerWidth,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=JSON.stringify(t.props)),n.h=1;var a=new XMLHttpRequest;a.open("POST",i+"/api/event",!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()}}}function a(){e=o.pathname,n("pageview")}try{r.addEventListener("hashchange",a);var u=r.plausible&&r.plausible.q||[];r.plausible=n;for(var g=0;g"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.hash.outbound-links.js b/priv/tracker/js/plausible.hash.outbound-links.js index 7f8536ded..8083122c9 100644 --- a/priv/tracker/js/plausible.hash.outbound-links.js +++ b/priv/tracker/js/plausible.hash.outbound-links.js @@ -1 +1 @@ -!function(r,i){"use strict";var e,o=r.location,s=r.document,t=s.querySelector('[src*="'+i+'"]'),l=t&&t.getAttribute("data-domain");function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var a,n;r.phantom||r._phantom||r.__nightmare||r.navigator.webdriver||((a={}).n=e,a.u=o.href,a.d=l,a.r=s.referrer||null,a.w=r.innerWidth,t&&t.meta&&(a.m=JSON.stringify(t.meta)),t&&t.props&&(a.p=JSON.stringify(t.props)),a.h=1,(n=new XMLHttpRequest).open("POST",i+"/api/event",!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(a)),n.onreadystatechange=function(){4==n.readyState&&t&&t.callback&&t.callback()})}function n(){e=o.pathname,a("pageview")}try{r.addEventListener("hashchange",n),s.addEventListener("click",function(e){for(var t=e.target;t&&(void 0===t.tagName||"a"!=t.tagName.toLowerCase()||!t.href);)t=t.parentNode;t&&t.href&&t.host&&t.host!==o.host&&(plausible("Outbound Link: Click",{props:{url:t.href}}),t.target&&!t.target.match(/^_(self|parent|top)$/i)||(setTimeout(function(){o.href=t.href},150),e.preventDefault()))});var c=r.plausible&&r.plausible.q||[];r.plausible=a;for(var p=0;p"); \ No newline at end of file +!function(n,i){"use strict";var e,o=n.location,l=n.document,t=l.querySelector('[src*="'+i+'"]'),s=t&&t.getAttribute("data-domain"),c=n.localStorage.plausible_ignore;function p(e){console.warn("Ignoring Event: "+e)}function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return p("localhost");if(!(n.phantom||n._phantom||n.__nightmare||n.navigator.webdriver)){if("true"==c)return p("localStorage flag");var a={};a.n=e,a.u=o.href,a.d=s,a.r=l.referrer||null,a.w=n.innerWidth,t&&t.meta&&(a.m=JSON.stringify(t.meta)),t&&t.props&&(a.p=JSON.stringify(t.props)),a.h=1;var r=new XMLHttpRequest;r.open("POST",i+"/api/event",!0),r.setRequestHeader("Content-Type","text/plain"),r.send(JSON.stringify(a)),r.onreadystatechange=function(){4==r.readyState&&t&&t.callback&&t.callback()}}}function r(){e=o.pathname,a("pageview")}function u(e){for(var t=e.target,a="auxclick"==e.type&&2==e.which,r="click"==e.type;t&&(void 0===t.tagName||"a"!=t.tagName.toLowerCase()||!t.href);)t=t.parentNode;t&&t.href&&t.host&&t.host!==o.host&&((a||r)&&plausible("Outbound Link: Click",{props:{url:t.href}}),t.target&&!t.target.match(/^_(self|parent|top)$/i)||e.ctrlKey||e.metaKey||e.shiftKey||!r||(setTimeout(function(){o.href=t.href},150),e.preventDefault()))}try{n.addEventListener("hashchange",r),l.addEventListener("click",u),l.addEventListener("auxclick",u);var h=n.plausible&&n.plausible.q||[];n.plausible=a;for(var f=0;f"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.js b/priv/tracker/js/plausible.js index ad8f6d368..98b0ec827 100644 --- a/priv/tracker/js/plausible.js +++ b/priv/tracker/js/plausible.js @@ -1 +1 @@ -!function(i,r){"use strict";var e,o=i.location,s=i.document,t=s.querySelector('[src*="'+r+'"]'),l=t&&t.getAttribute("data-domain");function n(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var n,a;i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver||((n={}).n=e,n.u=o.href,n.d=l,n.r=s.referrer||null,n.w=i.innerWidth,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=JSON.stringify(t.props)),(a=new XMLHttpRequest).open("POST",r+"/api/event",!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()})}function a(){e!==o.pathname&&(e=o.pathname,n("pageview"))}try{var p,c=i.history;c.pushState&&(p=c.pushState,c.pushState=function(){p.apply(this,arguments),a()},i.addEventListener("popstate",a));var u=i.plausible&&i.plausible.q||[];i.plausible=n;for(var h=0;h"); \ No newline at end of file +!function(r,i){"use strict";var e,o=r.location,s=r.document,t=s.querySelector('[src*="'+i+'"]'),l=t&&t.getAttribute("data-domain"),p=r.localStorage.plausible_ignore;function c(e){console.warn("Ignoring Event: "+e)}function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return c("localhost");if(!(r.phantom||r._phantom||r.__nightmare||r.navigator.webdriver)){if("true"==p)return c("localStorage flag");var a={};a.n=e,a.u=o.href,a.d=l,a.r=s.referrer||null,a.w=r.innerWidth,t&&t.meta&&(a.m=JSON.stringify(t.meta)),t&&t.props&&(a.p=JSON.stringify(t.props));var n=new XMLHttpRequest;n.open("POST",i+"/api/event",!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(a)),n.onreadystatechange=function(){4==n.readyState&&t&&t.callback&&t.callback()}}}function n(){e!==o.pathname&&(e=o.pathname,a("pageview"))}try{var u,h=r.history;h.pushState&&(u=h.pushState,h.pushState=function(){u.apply(this,arguments),n()},r.addEventListener("popstate",n));var g=r.plausible&&r.plausible.q||[];r.plausible=a;for(var f=0;f"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.outbound-links.js b/priv/tracker/js/plausible.outbound-links.js index 35dc7ee42..461e8dabe 100644 --- a/priv/tracker/js/plausible.outbound-links.js +++ b/priv/tracker/js/plausible.outbound-links.js @@ -1 +1 @@ -!function(r,i){"use strict";var e,o=r.location,s=r.document,t=s.querySelector('[src*="'+i+'"]'),l=t&&t.getAttribute("data-domain");function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var a,n;r.phantom||r._phantom||r.__nightmare||r.navigator.webdriver||((a={}).n=e,a.u=o.href,a.d=l,a.r=s.referrer||null,a.w=r.innerWidth,t&&t.meta&&(a.m=JSON.stringify(t.meta)),t&&t.props&&(a.p=JSON.stringify(t.props)),(n=new XMLHttpRequest).open("POST",i+"/api/event",!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(a)),n.onreadystatechange=function(){4==n.readyState&&t&&t.callback&&t.callback()})}function n(){e!==o.pathname&&(e=o.pathname,a("pageview"))}try{var p,c=r.history;c.pushState&&(p=c.pushState,c.pushState=function(){p.apply(this,arguments),n()},r.addEventListener("popstate",n)),s.addEventListener("click",function(e){for(var t=e.target;t&&(void 0===t.tagName||"a"!=t.tagName.toLowerCase()||!t.href);)t=t.parentNode;t&&t.href&&t.host&&t.host!==o.host&&(plausible("Outbound Link: Click",{props:{url:t.href}}),t.target&&!t.target.match(/^_(self|parent|top)$/i)||(setTimeout(function(){o.href=t.href},150),e.preventDefault()))});var u=r.plausible&&r.plausible.q||[];r.plausible=a;for(var h=0;h"); \ No newline at end of file +!function(n,i){"use strict";var e,o=n.location,s=n.document,t=s.querySelector('[src*="'+i+'"]'),l=t&&t.getAttribute("data-domain"),c=n.localStorage.plausible_ignore;function p(e){console.warn("Ignoring Event: "+e)}function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return p("localhost");if(!(n.phantom||n._phantom||n.__nightmare||n.navigator.webdriver)){if("true"==c)return p("localStorage flag");var a={};a.n=e,a.u=o.href,a.d=l,a.r=s.referrer||null,a.w=n.innerWidth,t&&t.meta&&(a.m=JSON.stringify(t.meta)),t&&t.props&&(a.p=JSON.stringify(t.props));var r=new XMLHttpRequest;r.open("POST",i+"/api/event",!0),r.setRequestHeader("Content-Type","text/plain"),r.send(JSON.stringify(a)),r.onreadystatechange=function(){4==r.readyState&&t&&t.callback&&t.callback()}}}function r(){e!==o.pathname&&(e=o.pathname,a("pageview"))}function u(e){for(var t=e.target,a="auxclick"==e.type&&2==e.which,r="click"==e.type;t&&(void 0===t.tagName||"a"!=t.tagName.toLowerCase()||!t.href);)t=t.parentNode;t&&t.href&&t.host&&t.host!==o.host&&((a||r)&&plausible("Outbound Link: Click",{props:{url:t.href}}),t.target&&!t.target.match(/^_(self|parent|top)$/i)||e.ctrlKey||e.metaKey||e.shiftKey||!r||(setTimeout(function(){o.href=t.href},150),e.preventDefault()))}try{var h,f=n.history;f.pushState&&(h=f.pushState,f.pushState=function(){h.apply(this,arguments),r()},n.addEventListener("popstate",r)),s.addEventListener("click",u),s.addEventListener("auxclick",u);var d=n.plausible&&n.plausible.q||[];n.plausible=a;for(var g=0;g"); \ No newline at end of file diff --git a/tracker/compile.js b/tracker/compile.js index 268667d6a..e33ba321d 100644 --- a/tracker/compile.js +++ b/tracker/compile.js @@ -16,8 +16,12 @@ function compilefile(input, output, templateVars = {}) { } compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.js')) +compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.exclusions.js'), {exclusionMode: true}) compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.hash.js'), {hashMode: true}) +compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.hash.exclusions.js'), {hashMode: true, exclusionMode: true}) compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.outbound-links.js'), {outboundLinks: true}) +compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.exclusions.outbound-links.js'), {outboundLinks: true, exclusionMode: true}) compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.hash.outbound-links.js'), {hashMode: true, outboundLinks: true}) +compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.hash.exclusions.outbound-links.js'), {hashMode: true, outboundLinks: true, exclusionMode: true}) compilefile(relPath('src/p.js'), relPath('../priv/tracker/js/p.js')) fs.copyFileSync(relPath('../priv/tracker/js/plausible.js'), relPath('../priv/tracker/js/analytics.js')) diff --git a/tracker/src/plausible.js b/tracker/src/plausible.js index b79f6d8c2..eb460d176 100644 --- a/tracker/src/plausible.js +++ b/tracker/src/plausible.js @@ -6,11 +6,26 @@ var scriptEl = document.querySelector('[src*="' + plausibleHost +'"]') var domain = scriptEl && scriptEl.getAttribute('data-domain') + var plausible_ignore = window.localStorage.plausible_ignore; + {{#if exclusionMode}} + var excludedPaths = scriptEl && scriptEl.getAttribute('data-exclude').split(','); + {{/if}} var lastPage; + function warn(reason) { + console.warn('Ignoring Event: ' + reason); + } + function trigger(eventName, options) { - if (/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(location.hostname) || location.protocol === 'file:') return console.warn('Ignoring event on localhost'); + if (/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(location.hostname) || location.protocol === 'file:') return warn('localhost'); if (window.phantom || window._phantom || window.__nightmare || window.navigator.webdriver) return; + if (plausible_ignore=="true") return warn('localStorage flag') + {{#if exclusionMode}} + if (excludedPaths) + for (var i = 0; i < excludedPaths.length; i++) + if (eventName == "pageview" && location.pathname.match(new RegExp('^' + excludedPaths[i].trim().replace(/\*\*/g, '.*').replace(/[^\.]\*/g, '[^\\s\/]*') + '\/?$'))) + return warn('exclusion rule'); + {{/if}} var payload = {} payload.n = eventName