diff --git a/assets/js/app.js b/assets/js/app.js index bbfed39f0..71234b9cf 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -54,6 +54,6 @@ if (registerForm) { } } - plausible('trigger', 'Signup', {callback: submitForm}); + plausible('Signup', {callback: submitForm}); }) } diff --git a/assets/js/plausible.js b/assets/js/plausible.js index 034022d85..cee5003a9 100644 --- a/assets/js/plausible.js +++ b/assets/js/plausible.js @@ -2,6 +2,13 @@ 'use strict'; try { + const scriptEl = window.document.querySelector('[src*="' + plausibleHost +'"]') + const domainAttr = scriptEl && scriptEl.getAttribute('data-domain') + + const CONFIG = { + domain: domainAttr || window.location.hostname + } + function setCookie(name,value) { var date = new Date(); date.setTime(date.getTime() + (3*365*24*60*60*1000)); // 3 YEARS @@ -68,7 +75,7 @@ })) } - function trigger(eventName) { + function trigger(eventName, options) { if (/localhost$/.test(window.location.hostname)) return ignore('website is running locally'); if (window.location.protocol === 'file:') return ignore('website is running locally'); if (window.document.visibilityState === 'prerender') return ignore('document is prerendering'); @@ -76,6 +83,7 @@ var payload = getUserData() payload.name = eventName payload.url = getUrl() + payload.domain = CONFIG['domain'] var request = new XMLHttpRequest(); request.open('POST', plausibleHost + '/api/event', true); @@ -86,9 +94,9 @@ request.onreadystatechange = function() { if (request.readyState == XMLHttpRequest.DONE) { setUserData(payload) + options && options.callback && options.callback() } } - } function onUnload() { @@ -110,6 +118,12 @@ } } + const queue = (window.plausible && window.plausible.q) || [] + window.plausible = trigger + for (var i = 0; i < queue.length; i++) { + trigger.apply(this, queue[i]) + } + page() } catch (e) { new Image().src = plausibleHost + '/api/error?message=' + encodeURIComponent(e.message); diff --git a/lib/plausible_web/templates/layout/_tracking.html.eex b/lib/plausible_web/templates/layout/_tracking.html.eex index 72037de99..1faeacb1c 100644 --- a/lib/plausible_web/templates/layout/_tracking.html.eex +++ b/lib/plausible_web/templates/layout/_tracking.html.eex @@ -1,11 +1,4 @@ -<%= if Mix.env() == :prod && !@conn.assigns[:skip_plausible_tracking] do %> - +<%= if !@conn.assigns[:skip_plausible_tracking] do %> + + <% end %>