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';
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);

View File

@ -1,11 +1,4 @@
<%= if Mix.env() == :prod && !@conn.assigns[:skip_plausible_tracking] do %>
<script>
(function (w,d,s,o,f,js,fjs) {
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>
<%= if !@conn.assigns[:skip_plausible_tracking] do %>
<script async defer src="https://plausibe.io/js/plausible.js"></script>
<script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>
<% end %>