umami/tracker/index.js

161 lines
3.7 KiB
JavaScript
Raw Normal View History

import { doNotTrack, hook } from '../lib/web';
2020-09-01 05:25:31 +02:00
import { removeTrailingSlash } from '../lib/url';
2020-07-18 04:15:29 +02:00
2020-08-21 04:17:27 +02:00
(window => {
const {
screen: { width, height },
navigator: { language },
location: { hostname, pathname, search },
2020-10-03 05:33:46 +02:00
sessionStorage,
document,
history,
} = window;
const script = document.querySelector('script[data-website-id]');
2020-09-18 22:40:46 +02:00
const attr = key => script && script.getAttribute(key);
2020-08-21 04:17:27 +02:00
2020-09-18 22:40:46 +02:00
const website = attr('data-website-id');
const hostUrl = attr('data-host-url');
const autoTrack = attr('data-auto-track') !== 'false';
2020-10-03 05:33:46 +02:00
const dnt = attr('data-do-not-track');
const useCache = attr('data-cache');
2020-09-18 22:40:46 +02:00
if (!script || (dnt && doNotTrack())) return;
2020-08-21 04:17:27 +02:00
2020-09-01 05:25:31 +02:00
const root = hostUrl
? removeTrailingSlash(hostUrl)
: new URL(script.src).href.split('/').slice(0, -1).join('/');
const screen = `${width}x${height}`;
const listeners = [];
let currentUrl = `${pathname}${search}`;
let currentRef = document.referrer;
2020-09-18 22:40:46 +02:00
/* Collect metrics */
2020-07-21 04:24:33 +02:00
const post = (url, data, callback) => {
const req = new XMLHttpRequest();
req.open('POST', url, true);
req.setRequestHeader('Content-Type', 'application/json');
req.onreadystatechange = () => {
if (req.readyState === 4) {
2020-10-03 05:33:46 +02:00
callback && callback(req.response);
}
};
req.send(JSON.stringify(data));
};
2020-09-16 12:07:22 +02:00
const collect = (type, params, uuid) => {
2020-10-03 05:33:46 +02:00
const key = 'umami.cache';
2020-09-15 13:54:35 +02:00
const payload = {
2020-09-15 14:10:01 +02:00
website: uuid,
2020-09-15 13:54:35 +02:00
hostname,
screen,
language,
2020-10-03 05:33:46 +02:00
cache: useCache && sessionStorage.getItem(key),
};
2020-09-15 13:54:35 +02:00
if (params) {
Object.keys(params).forEach(key => {
payload[key] = params[key];
});
}
2020-10-03 05:33:46 +02:00
return post(
`${root}/api/collect`,
{
type,
payload,
},
res => useCache && sessionStorage.setItem(key, res),
);
2020-09-15 13:54:35 +02:00
};
2020-09-16 12:07:22 +02:00
2020-09-19 00:26:45 +02:00
const trackView = (url = currentUrl, referrer = currentRef, uuid = website) =>
2020-09-18 22:40:46 +02:00
collect(
'pageview',
{
url,
referrer,
},
uuid,
);
2020-09-19 00:26:45 +02:00
const trackEvent = (event_value, event_type = 'custom', url = currentUrl, uuid = website) =>
2020-09-18 22:40:46 +02:00
collect(
'event',
{
event_type,
event_value,
url,
},
uuid,
);
/* Handle events */
2020-09-19 00:26:45 +02:00
const addEvents = () => {
2020-09-18 22:40:46 +02:00
document.querySelectorAll("[class*='umami--']").forEach(element => {
element.className.split(' ').forEach(className => {
if (/^umami--([a-z]+)--([a-z0-9_]+[a-z0-9-_]+)$/.test(className)) {
const [, type, value] = className.split('--');
2020-09-19 00:26:45 +02:00
const listener = () => trackEvent(value, type);
2020-09-18 22:40:46 +02:00
listeners.push([element, type, listener]);
element.addEventListener(type, listener, true);
}
});
});
};
const removeEvents = () => {
listeners.forEach(([element, type, listener]) => {
element && element.removeEventListener(type, listener, true);
});
listeners.length = 0;
};
/* Handle history changes */
2020-09-16 12:07:22 +02:00
const handlePush = (state, title, url) => {
removeEvents();
2020-09-18 22:40:46 +02:00
2020-09-16 12:07:22 +02:00
currentRef = currentUrl;
const newUrl = url.toString();
if (newUrl.substring(0, 4) === 'http') {
const { pathname, search } = new URL(newUrl);
currentUrl = `${pathname}${search}`;
} else {
currentUrl = newUrl;
}
2020-09-19 00:26:45 +02:00
trackView(currentUrl, currentRef);
2020-09-16 12:07:22 +02:00
2020-09-19 00:26:45 +02:00
setTimeout(addEvents, 300);
2020-09-15 13:54:35 +02:00
};
2020-09-18 22:40:46 +02:00
/* Global */
2020-09-16 12:07:22 +02:00
2020-09-18 22:40:46 +02:00
if (!window.umami) {
2020-09-19 00:26:45 +02:00
const umami = event_value => trackEvent(event_value);
umami.trackView = trackView;
umami.trackEvent = trackEvent;
2020-07-19 10:57:01 +02:00
2020-09-18 22:40:46 +02:00
window.umami = umami;
}
/* Start */
2020-09-18 22:40:46 +02:00
if (autoTrack) {
history.pushState = hook(history, 'pushState', handlePush);
history.replaceState = hook(history, 'replaceState', handlePush);
2020-09-19 00:26:45 +02:00
trackView(currentUrl, currentRef);
2020-09-19 00:26:45 +02:00
addEvents();
2020-09-03 17:53:39 +02:00
}
2020-08-21 04:17:27 +02:00
})(window);