Update script with plausible.js

This commit is contained in:
Uku Taht 2020-02-05 16:08:34 +02:00
parent 38512cadc8
commit 1e417f03a9
3 changed files with 20 additions and 13 deletions

View File

@ -54,6 +54,6 @@ if (registerForm) {
} }
} }
plausible('trigger', 'Signup', {callback: submitForm}); plausible('Signup', {callback: submitForm});
}) })
} }

View File

@ -2,6 +2,13 @@
'use strict'; 'use strict';
try { 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) { function setCookie(name,value) {
var date = new Date(); var date = new Date();
date.setTime(date.getTime() + (3*365*24*60*60*1000)); // 3 YEARS 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 (/localhost$/.test(window.location.hostname)) return ignore('website is running locally');
if (window.location.protocol === 'file:') 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'); if (window.document.visibilityState === 'prerender') return ignore('document is prerendering');
@ -76,6 +83,7 @@
var payload = getUserData() var payload = getUserData()
payload.name = eventName payload.name = eventName
payload.url = getUrl() payload.url = getUrl()
payload.domain = CONFIG['domain']
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
request.open('POST', plausibleHost + '/api/event', true); request.open('POST', plausibleHost + '/api/event', true);
@ -86,9 +94,9 @@
request.onreadystatechange = function() { request.onreadystatechange = function() {
if (request.readyState == XMLHttpRequest.DONE) { if (request.readyState == XMLHttpRequest.DONE) {
setUserData(payload) setUserData(payload)
options && options.callback && options.callback()
} }
} }
} }
function onUnload() { 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() page()
} catch (e) { } catch (e) {
new Image().src = plausibleHost + '/api/error?message=' + encodeURIComponent(e.message); new Image().src = plausibleHost + '/api/error?message=' + encodeURIComponent(e.message);

View File

@ -1,11 +1,4 @@
<%= if Mix.env() == :prod && !@conn.assigns[:skip_plausible_tracking] do %> <%= if !@conn.assigns[:skip_plausible_tracking] do %>
<script> <script async defer src="https://plausibe.io/js/plausible.js"></script>
(function (w,d,s,o,f,js,fjs) { <script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>
w[o] = w[o] || function () { (w[o].q = w[o].q || []).push(arguments) };
js = d.createElement(s), fjs = d.getElementsByTagName(s)[0];
js.id = o; js.src = f; js.async = 1; fjs.parentNode.insertBefore(js, fjs);
}(window, document, 'script', 'plausible', 'https://plausible.io/js/p.js'));
plausible('page')
</script>
<% end %> <% end %>