From a378935aacdd7c57105c3d9b9178347bc6704ee2 Mon Sep 17 00:00:00 2001
From: Uku Taht <uku.taht@gmail.com>
Date: Thu, 17 Jun 2021 10:55:45 +0300
Subject: [PATCH] Add IE11 compatibility mode

---
 lib/plausible_web/plugs/tracker.ex                            | 2 +-
 priv/tracker/js/plausible.compat.exclusions.hash.js           | 1 +
 .../js/plausible.compat.exclusions.hash.outbound-links.js     | 1 +
 priv/tracker/js/plausible.compat.exclusions.js                | 1 +
 priv/tracker/js/plausible.compat.exclusions.outbound-links.js | 1 +
 priv/tracker/js/plausible.compat.hash.js                      | 1 +
 priv/tracker/js/plausible.compat.hash.outbound-links.js       | 1 +
 priv/tracker/js/plausible.compat.js                           | 1 +
 priv/tracker/js/plausible.compat.outbound-links.js            | 1 +
 tracker/compile.js                                            | 2 +-
 tracker/src/plausible.js                                      | 4 ++++
 11 files changed, 14 insertions(+), 2 deletions(-)
 create mode 100644 priv/tracker/js/plausible.compat.exclusions.hash.js
 create mode 100644 priv/tracker/js/plausible.compat.exclusions.hash.outbound-links.js
 create mode 100644 priv/tracker/js/plausible.compat.exclusions.js
 create mode 100644 priv/tracker/js/plausible.compat.exclusions.outbound-links.js
 create mode 100644 priv/tracker/js/plausible.compat.hash.js
 create mode 100644 priv/tracker/js/plausible.compat.hash.outbound-links.js
 create mode 100644 priv/tracker/js/plausible.compat.js
 create mode 100644 priv/tracker/js/plausible.compat.outbound-links.js

diff --git a/lib/plausible_web/plugs/tracker.ex b/lib/plausible_web/plugs/tracker.ex
index ee399fa0..61a808e2 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"]
+  base_variants = ["hash", "outbound-links", "exclusions", "compat"]
 
   # Generates Power Set of all variants
   variants =
diff --git a/priv/tracker/js/plausible.compat.exclusions.hash.js b/priv/tracker/js/plausible.compat.exclusions.hash.js
new file mode 100644
index 00000000..d685b93e
--- /dev/null
+++ b/priv/tracker/js/plausible.compat.exclusions.hash.js
@@ -0,0 +1 @@
+!function(){"use strict";var e,r=window.location,o=window.document,l=o.getElementById("plausible"),s=l.getAttribute("data-api")||new URL(l.src).origin+"/api/event",w=window.localStorage.plausible_ignore,d=l&&l.getAttribute("data-exclude").split(",");function p(e){console.warn("Ignoring Event: "+e)}function t(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(r.hostname)||"file:"===r.protocol)return p("localhost");if(!(window.phantom||window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)){if("true"==w)return p("localStorage flag");if(d)for(var i=0;i<d.length;i++)if("pageview"==e&&r.pathname.match(new RegExp("^"+d[i].trim().replace(/\*\*/g,".*").replace(/([^\.])\*/g,"$1[^\\s/]*")+"/?$")))return p("exclusion rule");var n={};n.n=e,n.u=r.href,n.d=l.getAttribute("data-domain"),n.r=o.referrer||null,n.w=window.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",s,!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()}}}function i(){e=r.pathname,t("pageview")}window.addEventListener("hashchange",i);var n=window.plausible&&window.plausible.q||[];window.plausible=t;for(var a=0;a<n.length;a++)t.apply(this,n[a]);"prerender"===o.visibilityState?o.addEventListener("visibilitychange",function(){e||"visible"!==o.visibilityState||i()}):i()}();
\ No newline at end of file
diff --git a/priv/tracker/js/plausible.compat.exclusions.hash.outbound-links.js b/priv/tracker/js/plausible.compat.exclusions.hash.outbound-links.js
new file mode 100644
index 00000000..36cfd219
--- /dev/null
+++ b/priv/tracker/js/plausible.compat.exclusions.hash.outbound-links.js
@@ -0,0 +1 @@
+!function(){"use strict";var e,r=window.location,o=window.document,l=o.getElementById("plausible"),s=l.getAttribute("data-api")||new URL(l.src).origin+"/api/event",c=window.localStorage.plausible_ignore,p=l&&l.getAttribute("data-exclude").split(",");function d(e){console.warn("Ignoring Event: "+e)}function t(e,t){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"==c)return d("localStorage flag");if(p)for(var i=0;i<p.length;i++)if("pageview"==e&&r.pathname.match(new RegExp("^"+p[i].trim().replace(/\*\*/g,".*").replace(/([^\.])\*/g,"$1[^\\s/]*")+"/?$")))return d("exclusion rule");var n={};n.n=e,n.u=r.href,n.d=l.getAttribute("data-domain"),n.r=o.referrer||null,n.w=window.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",s,!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()}}}function i(){e=r.pathname,t("pageview")}function n(e){for(var t=e.target,i="auxclick"==e.type&&2==e.which,n="click"==e.type;t&&(void 0===t.tagName||"a"!=t.tagName.toLowerCase()||!t.href);)t=t.parentNode;t&&t.href&&t.host&&t.host!==r.host&&((i||n)&&plausible("Outbound Link: Click",{props:{url:t.href}}),t.target&&!t.target.match(/^_(self|parent|top)$/i)||e.ctrlKey||e.metaKey||e.shiftKey||!n||(setTimeout(function(){r.href=t.href},150),e.preventDefault()))}window.addEventListener("hashchange",i),o.addEventListener("click",n),o.addEventListener("auxclick",n);var a=window.plausible&&window.plausible.q||[];window.plausible=t;for(var u=0;u<a.length;u++)t.apply(this,a[u]);"prerender"===o.visibilityState?o.addEventListener("visibilitychange",function(){e||"visible"!==o.visibilityState||i()}):i()}();
\ No newline at end of file
diff --git a/priv/tracker/js/plausible.compat.exclusions.js b/priv/tracker/js/plausible.compat.exclusions.js
new file mode 100644
index 00000000..749df0fd
--- /dev/null
+++ b/priv/tracker/js/plausible.compat.exclusions.js
@@ -0,0 +1 @@
+!function(){"use strict";var e,r=window.location,o=window.document,l=o.getElementById("plausible"),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(e){console.warn("Ignoring Event: "+e)}function t(e,t){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"==e&&r.pathname.match(new RegExp("^"+w[i].trim().replace(/\*\*/g,".*").replace(/([^\.])\*/g,"$1[^\\s/]*")+"/?$")))return d("exclusion rule");var n={};n.n=e,n.u=r.href,n.d=l.getAttribute("data-domain"),n.r=o.referrer||null,n.w=window.innerWidth,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=JSON.stringify(t.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&&t&&t.callback&&t.callback()}}}function i(){e!==r.pathname&&(e=r.pathname,t("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=t;for(var c=0;c<u.length;c++)t.apply(this,u[c]);"prerender"===o.visibilityState?o.addEventListener("visibilitychange",function(){e||"visible"!==o.visibilityState||i()}):i()}();
\ No newline at end of file
diff --git a/priv/tracker/js/plausible.compat.exclusions.outbound-links.js b/priv/tracker/js/plausible.compat.exclusions.outbound-links.js
new file mode 100644
index 00000000..70e0cb44
--- /dev/null
+++ b/priv/tracker/js/plausible.compat.exclusions.outbound-links.js
@@ -0,0 +1 @@
+!function(){"use strict";var e,r=window.location,o=window.document,l=o.getElementById("plausible"),s=l.getAttribute("data-api")||new URL(l.src).origin+"/api/event",p=window.localStorage.plausible_ignore,c=l&&l.getAttribute("data-exclude").split(",");function u(e){console.warn("Ignoring Event: "+e)}function t(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(r.hostname)||"file:"===r.protocol)return u("localhost");if(!(window.phantom||window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)){if("true"==p)return u("localStorage flag");if(c)for(var i=0;i<c.length;i++)if("pageview"==e&&r.pathname.match(new RegExp("^"+c[i].trim().replace(/\*\*/g,".*").replace(/([^\.])\*/g,"$1[^\\s/]*")+"/?$")))return u("exclusion rule");var a={};a.n=e,a.u=r.href,a.d=l.getAttribute("data-domain"),a.r=o.referrer||null,a.w=window.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",s,!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(a)),n.onreadystatechange=function(){4==n.readyState&&t&&t.callback&&t.callback()}}}function i(){e!==r.pathname&&(e=r.pathname,t("pageview"))}function a(e){for(var t=e.target,i="auxclick"==e.type&&2==e.which,a="click"==e.type;t&&(void 0===t.tagName||"a"!=t.tagName.toLowerCase()||!t.href);)t=t.parentNode;t&&t.href&&t.host&&t.host!==r.host&&((i||a)&&plausible("Outbound Link: Click",{props:{url:t.href}}),t.target&&!t.target.match(/^_(self|parent|top)$/i)||e.ctrlKey||e.metaKey||e.shiftKey||!a||(setTimeout(function(){r.href=t.href},150),e.preventDefault()))}var n,d=window.history;d.pushState&&(n=d.pushState,d.pushState=function(){n.apply(this,arguments),i()},window.addEventListener("popstate",i)),o.addEventListener("click",a),o.addEventListener("auxclick",a);var w=window.plausible&&window.plausible.q||[];window.plausible=t;for(var h=0;h<w.length;h++)t.apply(this,w[h]);"prerender"===o.visibilityState?o.addEventListener("visibilitychange",function(){e||"visible"!==o.visibilityState||i()}):i()}();
\ No newline at end of file
diff --git a/priv/tracker/js/plausible.compat.hash.js b/priv/tracker/js/plausible.compat.hash.js
new file mode 100644
index 00000000..26c82c36
--- /dev/null
+++ b/priv/tracker/js/plausible.compat.hash.js
@@ -0,0 +1 @@
+!function(){"use strict";var e,a=window.location,o=window.document,r=o.getElementById("plausible"),l=r.getAttribute("data-api")||new URL(r.src).origin+"/api/event",s=window.localStorage.plausible_ignore;function w(e){console.warn("Ignoring Event: "+e)}function t(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(a.hostname)||"file:"===a.protocol)return w("localhost");if(!(window.phantom||window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)){if("true"==s)return w("localStorage flag");var i={};i.n=e,i.u=a.href,i.d=r.getAttribute("data-domain"),i.r=o.referrer||null,i.w=window.innerWidth,t&&t.meta&&(i.m=JSON.stringify(t.meta)),t&&t.props&&(i.p=JSON.stringify(t.props)),i.h=1;var n=new XMLHttpRequest;n.open("POST",l,!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(i)),n.onreadystatechange=function(){4==n.readyState&&t&&t.callback&&t.callback()}}}function i(){e=a.pathname,t("pageview")}window.addEventListener("hashchange",i);var n=window.plausible&&window.plausible.q||[];window.plausible=t;for(var d=0;d<n.length;d++)t.apply(this,n[d]);"prerender"===o.visibilityState?o.addEventListener("visibilitychange",function(){e||"visible"!==o.visibilityState||i()}):i()}();
\ No newline at end of file
diff --git a/priv/tracker/js/plausible.compat.hash.outbound-links.js b/priv/tracker/js/plausible.compat.hash.outbound-links.js
new file mode 100644
index 00000000..a21d850d
--- /dev/null
+++ b/priv/tracker/js/plausible.compat.hash.outbound-links.js
@@ -0,0 +1 @@
+!function(){"use strict";var e,a=window.location,r=window.document,o=r.getElementById("plausible"),l=o.getAttribute("data-api")||new URL(o.src).origin+"/api/event",s=window.localStorage.plausible_ignore;function c(e){console.warn("Ignoring Event: "+e)}function t(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(a.hostname)||"file:"===a.protocol)return c("localhost");if(!(window.phantom||window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)){if("true"==s)return c("localStorage flag");var i={};i.n=e,i.u=a.href,i.d=o.getAttribute("data-domain"),i.r=r.referrer||null,i.w=window.innerWidth,t&&t.meta&&(i.m=JSON.stringify(t.meta)),t&&t.props&&(i.p=JSON.stringify(t.props)),i.h=1;var n=new XMLHttpRequest;n.open("POST",l,!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(i)),n.onreadystatechange=function(){4==n.readyState&&t&&t.callback&&t.callback()}}}function i(){e=a.pathname,t("pageview")}function n(e){for(var t=e.target,i="auxclick"==e.type&&2==e.which,n="click"==e.type;t&&(void 0===t.tagName||"a"!=t.tagName.toLowerCase()||!t.href);)t=t.parentNode;t&&t.href&&t.host&&t.host!==a.host&&((i||n)&&plausible("Outbound Link: Click",{props:{url:t.href}}),t.target&&!t.target.match(/^_(self|parent|top)$/i)||e.ctrlKey||e.metaKey||e.shiftKey||!n||(setTimeout(function(){a.href=t.href},150),e.preventDefault()))}window.addEventListener("hashchange",i),r.addEventListener("click",n),r.addEventListener("auxclick",n);var d=window.plausible&&window.plausible.q||[];window.plausible=t;for(var p=0;p<d.length;p++)t.apply(this,d[p]);"prerender"===r.visibilityState?r.addEventListener("visibilitychange",function(){e||"visible"!==r.visibilityState||i()}):i()}();
\ No newline at end of file
diff --git a/priv/tracker/js/plausible.compat.js b/priv/tracker/js/plausible.compat.js
new file mode 100644
index 00000000..48e92b13
--- /dev/null
+++ b/priv/tracker/js/plausible.compat.js
@@ -0,0 +1 @@
+!function(){"use strict";var t,a=window.location,o=window.document,r=o.getElementById("plausible"),l=r.getAttribute("data-api")||new URL(r.src).origin+"/api/event",s=window.localStorage.plausible_ignore;function w(t){console.warn("Ignoring Event: "+t)}function e(t,e){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(a.hostname)||"file:"===a.protocol)return w("localhost");if(!(window.phantom||window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)){if("true"==s)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",l,!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(i)),n.onreadystatechange=function(){4==n.readyState&&e&&e.callback&&e.callback()}}}function i(){t!==a.pathname&&(t=a.pathname,e("pageview"))}var n,p=window.history;p.pushState&&(n=p.pushState,p.pushState=function(){n.apply(this,arguments),i()},window.addEventListener("popstate",i));var d=window.plausible&&window.plausible.q||[];window.plausible=e;for(var u=0;u<d.length;u++)e.apply(this,d[u]);"prerender"===o.visibilityState?o.addEventListener("visibilitychange",function(){t||"visible"!==o.visibilityState||i()}):i()}();
\ No newline at end of file
diff --git a/priv/tracker/js/plausible.compat.outbound-links.js b/priv/tracker/js/plausible.compat.outbound-links.js
new file mode 100644
index 00000000..769b3a22
--- /dev/null
+++ b/priv/tracker/js/plausible.compat.outbound-links.js
@@ -0,0 +1 @@
+!function(){"use strict";var t,a=window.location,r=window.document,o=r.getElementById("plausible"),l=o.getAttribute("data-api")||new URL(o.src).origin+"/api/event",s=window.localStorage.plausible_ignore;function p(t){console.warn("Ignoring Event: "+t)}function e(t,e){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(a.hostname)||"file:"===a.protocol)return p("localhost");if(!(window.phantom||window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)){if("true"==s)return p("localStorage flag");var i={};i.n=t,i.u=a.href,i.d=o.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",l,!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(i)),n.onreadystatechange=function(){4==n.readyState&&e&&e.callback&&e.callback()}}}function i(){t!==a.pathname&&(t=a.pathname,e("pageview"))}function n(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 d,c=window.history;c.pushState&&(d=c.pushState,c.pushState=function(){d.apply(this,arguments),i()},window.addEventListener("popstate",i)),r.addEventListener("click",n),r.addEventListener("auxclick",n);var u=window.plausible&&window.plausible.q||[];window.plausible=e;for(var w=0;w<u.length;w++)e.apply(this,u[w]);"prerender"===r.visibilityState?r.addEventListener("visibilitychange",function(){t||"visible"!==r.visibilityState||i()}):i()}();
\ No newline at end of file
diff --git a/tracker/compile.js b/tracker/compile.js
index 7f67804f..511ab31f 100644
--- a/tracker/compile.js
+++ b/tracker/compile.js
@@ -16,7 +16,7 @@ function compilefile(input, output, templateVars = {}) {
   fs.writeFileSync(output, result.code)
 }
 
-const base_variants = ["hash", "outbound-links", "exclusions"]
+const base_variants = ["hash", "outbound-links", "exclusions", "compat"]
 const variants = [...g.clone.powerSet(base_variants)].filter(a => 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 2209c6b8..6511f575 100644
--- a/tracker/src/plausible.js
+++ b/tracker/src/plausible.js
@@ -4,7 +4,11 @@
   var location = window.location
   var document = window.document
 
+  {{#if compat}}
+  var scriptEl = document.getElementById('plausible');
+  {{else}}
   var scriptEl = document.currentScript;
+  {{/if}}
   var endpoint = scriptEl.getAttribute('data-api') || new URL(scriptEl.src).origin + '/api/event'
   var plausible_ignore = window.localStorage.plausible_ignore;
   {{#if exclusions}}
-- 
GitLab