mirror of
https://github.com/plausible/analytics.git
synced 2024-12-23 17:44:43 +03:00
Update script with plausible.js
This commit is contained in:
parent
38512cadc8
commit
1e417f03a9
@ -54,6 +54,6 @@ if (registerForm) {
|
||||
}
|
||||
}
|
||||
|
||||
plausible('trigger', 'Signup', {callback: submitForm});
|
||||
plausible('Signup', {callback: submitForm});
|
||||
})
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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 %>
|
||||
|
Loading…
Reference in New Issue
Block a user