analytics/assets/js/p.js

128 lines
4.0 KiB
JavaScript
Raw Normal View History

2019-09-02 14:29:19 +03:00
(function(window, plausibleHost){
'use strict';
try {
const CONFIG = {
domain: window.location.hostname
}
2019-09-02 14:29:19 +03:00
function setCookie(name,value) {
var date = new Date();
date.setTime(date.getTime() + (3*365*24*60*60*1000)); // 3 YEARS
var expires = "; expires=" + date.toUTCString();
2020-02-26 16:55:34 +03:00
document.cookie = name + "=" + (value || "") + expires + "; samesite=strict; path=/";
2019-09-02 14:29:19 +03:00
}
function getCookie(name) {
2020-02-26 16:55:34 +03:00
let matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : null;
2019-09-02 14:29:19 +03:00
}
function ignore(reason) {
2019-10-31 08:39:51 +03:00
console.warn('[Plausible] Ignoring event because ' + reason);
2019-09-02 14:29:19 +03:00
}
function getUrl() {
return window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.search;
}
2020-02-12 12:44:18 +03:00
function getSourceFromQueryParam() {
const result = window.location.search.match(/[?&](ref|source|utm_source)=([^?&]+)/);
return result ? result[2] : null
}
2019-10-31 05:35:33 +03:00
function getUserData() {
var userData = JSON.parse(getCookie('plausible_user'))
if (userData) {
2020-03-24 16:29:24 +03:00
return {
initial_referrer: userData.initial_referrer && decodeURIComponent(userData.initial_referrer),
initial_source: userData.initial_source && decodeURIComponent(userData.initial_source)
2020-02-12 12:44:18 +03:00
}
2019-10-31 05:35:33 +03:00
} else {
2020-03-24 16:29:24 +03:00
userData = {
initial_referrer: window.document.referrer || null,
2020-02-12 12:44:18 +03:00
initial_source: getSourceFromQueryParam(),
2019-10-31 05:35:33 +03:00
}
2020-03-24 16:29:24 +03:00
setCookie('plausible_user', JSON.stringify({
initial_referrer: userData.initial_referrer && encodeURIComponent(userData.initial_referrer),
initial_source: userData.initial_source && encodeURIComponent(userData.initial_source),
}))
return userData
}
2019-10-31 05:35:33 +03:00
}
2019-10-31 08:39:51 +03:00
function trigger(eventName, options) {
2019-09-02 14:29:19 +03:00
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');
2020-03-24 16:29:24 +03:00
var payload = CONFIG['trackAcquisition'] ? getUserData() : {}
payload.name = eventName
2019-10-31 05:35:33 +03:00
payload.url = getUrl()
payload.domain = CONFIG['domain']
2020-03-24 16:29:24 +03:00
payload.referrer = window.document.referrer || null
2020-02-12 12:44:18 +03:00
payload.source = getSourceFromQueryParam()
payload.user_agent = window.navigator.userAgent
payload.screen_width = window.innerWidth
2019-09-02 14:29:19 +03:00
var request = new XMLHttpRequest();
2019-10-31 06:49:46 +03:00
request.open('POST', plausibleHost + '/api/event', true);
2019-09-02 14:29:19 +03:00
request.setRequestHeader('Content-Type', 'text/plain');
2019-10-31 05:35:33 +03:00
request.send(JSON.stringify(payload));
2019-09-02 14:29:19 +03:00
request.onreadystatechange = function() {
if (request.readyState == XMLHttpRequest.DONE) {
2019-10-31 08:39:51 +03:00
options && options.callback && options.callback()
2019-09-02 14:29:19 +03:00
}
}
}
2019-10-31 08:39:51 +03:00
function page(options) {
trigger('pageview', options)
2019-09-02 14:29:19 +03:00
}
function trackPushState() {
var his = window.history
if (his.pushState) {
var originalFn = his['pushState']
his.pushState = function() {
originalFn.apply(this, arguments)
page();
}
}
window.addEventListener('popstate', page)
2019-09-02 14:29:19 +03:00
}
function configure(key, val) {
CONFIG[key] = val
}
2019-09-02 14:29:19 +03:00
const functions = {
page: page,
trigger: trigger,
trackPushState: trackPushState,
configure: configure
2019-09-02 14:29:19 +03:00
}
const queue = window.plausible.q || []
window.plausible = function() {
var args = [].slice.call(arguments);
var funcName = args.shift();
functions[funcName].apply(this, args);
};
for (var i = 0; i < queue.length; i++) {
window.plausible.apply(this, queue[i])
}
} catch (e) {
new Image().src = plausibleHost + '/api/error?message=' + encodeURIComponent(e.message);
}
})(window, BASE_URL);