2020-09-18 23:16:57 +02:00
|
|
|
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 => {
|
2020-07-20 10:54:21 +02:00
|
|
|
const {
|
|
|
|
screen: { width, height },
|
|
|
|
navigator: { language },
|
|
|
|
location: { hostname, pathname, search },
|
2020-11-02 07:01:30 +01:00
|
|
|
localStorage,
|
2020-10-03 05:33:46 +02:00
|
|
|
sessionStorage,
|
2020-07-20 10:54:21 +02:00
|
|
|
document,
|
|
|
|
history,
|
|
|
|
} = window;
|
|
|
|
|
|
|
|
const script = document.querySelector('script[data-website-id]');
|
2020-08-21 04:17:27 +02:00
|
|
|
|
2020-10-14 03:32:24 +02:00
|
|
|
if (!script) return;
|
|
|
|
|
2021-11-08 20:01:35 +01:00
|
|
|
const attr = script.getAttribute.bind(script);
|
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');
|
2021-11-08 20:01:35 +01:00
|
|
|
const domain = attr('data-domains') || '';
|
|
|
|
const domains = domain.split(',').map(n => n.trim());
|
2020-09-18 22:40:46 +02:00
|
|
|
|
2021-11-08 20:01:35 +01:00
|
|
|
const eventClass = /^umami--([a-z]+)--([\w]+[\w-]*)$/;
|
|
|
|
const eventSelect = "[class*='umami--']";
|
|
|
|
const cacheKey = 'umami.cache';
|
|
|
|
|
|
|
|
const disableTracking = () =>
|
|
|
|
(localStorage && localStorage.getItem('umami.disabled')) ||
|
2020-10-05 07:27:59 +02:00
|
|
|
(dnt && doNotTrack()) ||
|
2021-11-08 20:01:35 +01:00
|
|
|
(domain && !domains.includes(hostname));
|
2020-08-21 04:17:27 +02:00
|
|
|
|
2020-09-01 05:25:31 +02:00
|
|
|
const root = hostUrl
|
|
|
|
? removeTrailingSlash(hostUrl)
|
2020-12-05 07:41:30 +01:00
|
|
|
: script.src.split('/').slice(0, -1).join('/');
|
2020-07-20 10:54:21 +02:00
|
|
|
const screen = `${width}x${height}`;
|
2021-11-08 20:01:35 +01:00
|
|
|
const listeners = {};
|
2020-07-20 10:54:21 +02:00
|
|
|
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
|
|
|
|
2020-09-18 23:16:57 +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) {
|
2021-11-08 20:01:35 +01:00
|
|
|
callback(req.response);
|
2020-09-18 23:16:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
req.send(JSON.stringify(data));
|
|
|
|
};
|
|
|
|
|
2020-09-16 12:07:22 +02:00
|
|
|
const collect = (type, params, uuid) => {
|
2021-11-08 20:01:35 +01:00
|
|
|
if (disableTracking()) return;
|
2020-10-03 05:33:46 +02:00
|
|
|
|
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,
|
2021-11-08 20:01:35 +01:00
|
|
|
cache: useCache && sessionStorage.getItem(cacheKey),
|
2020-09-15 00:09:09 +02:00
|
|
|
};
|
2020-09-15 13:54:35 +02:00
|
|
|
|
2021-11-08 20:01:35 +01:00
|
|
|
Object.keys(params).forEach(key => {
|
|
|
|
payload[key] = params[key];
|
|
|
|
});
|
2020-09-15 13:54:35 +02:00
|
|
|
|
2020-10-14 03:32:24 +02:00
|
|
|
post(
|
2020-10-03 05:33:46 +02:00
|
|
|
`${root}/api/collect`,
|
|
|
|
{
|
|
|
|
type,
|
|
|
|
payload,
|
|
|
|
},
|
2021-11-08 20:01:35 +01:00
|
|
|
res => useCache && sessionStorage.setItem(cacheKey, res),
|
2020-10-03 05:33:46 +02:00
|
|
|
);
|
2020-09-15 13:54:35 +02:00
|
|
|
};
|
2020-09-16 12:07:22 +02:00
|
|
|
|
2021-11-08 20:01:35 +01:00
|
|
|
const trackView = (url = currentUrl, referrer = currentRef, uuid = website) => {
|
2020-09-18 22:40:46 +02:00
|
|
|
collect(
|
|
|
|
'pageview',
|
|
|
|
{
|
|
|
|
url,
|
|
|
|
referrer,
|
|
|
|
},
|
|
|
|
uuid,
|
|
|
|
);
|
2021-11-08 20:01:35 +01:00
|
|
|
};
|
2020-09-18 22:40:46 +02:00
|
|
|
|
2021-11-08 20:01:35 +01: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,
|
|
|
|
);
|
2021-11-08 20:01:35 +01:00
|
|
|
};
|
2020-09-18 22:40:46 +02:00
|
|
|
|
|
|
|
/* Handle events */
|
|
|
|
|
2022-01-15 04:10:46 +01:00
|
|
|
const addEvents = node => {
|
|
|
|
const elements = node.querySelectorAll(eventSelect);
|
|
|
|
Array.prototype.forEach.call(elements, addEvent);
|
|
|
|
};
|
|
|
|
|
2021-11-08 20:01:35 +01:00
|
|
|
const addEvent = element => {
|
2022-01-15 04:10:46 +01:00
|
|
|
element.className &&
|
|
|
|
element.className.split(' ').forEach(className => {
|
|
|
|
if (!eventClass.test(className)) return;
|
2020-09-18 22:40:46 +02:00
|
|
|
|
2022-01-15 04:10:46 +01:00
|
|
|
const [, type, value] = className.split('--');
|
|
|
|
const listener = listeners[className]
|
|
|
|
? listeners[className]
|
|
|
|
: (listeners[className] = () => trackEvent(value, type));
|
2021-11-08 20:01:35 +01:00
|
|
|
|
2022-01-15 04:10:46 +01:00
|
|
|
element.addEventListener(type, listener, true);
|
|
|
|
});
|
2020-09-18 22:40:46 +02:00
|
|
|
};
|
|
|
|
|
2021-11-08 20:01:35 +01:00
|
|
|
const monitorMutate = mutations => {
|
|
|
|
mutations.forEach(mutation => {
|
|
|
|
const element = mutation.target;
|
|
|
|
addEvent(element);
|
2022-01-15 04:10:46 +01:00
|
|
|
addEvents(element);
|
2020-09-18 22:40:46 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Handle history changes */
|
2020-09-16 12:07:22 +02:00
|
|
|
|
|
|
|
const handlePush = (state, title, url) => {
|
2021-02-02 07:49:00 +01:00
|
|
|
if (!url) return;
|
|
|
|
|
|
|
|
currentRef = currentUrl;
|
|
|
|
const newUrl = url.toString();
|
|
|
|
|
|
|
|
if (newUrl.substring(0, 4) === 'http') {
|
|
|
|
currentUrl = '/' + newUrl.split('/').splice(3).join('/');
|
|
|
|
} else {
|
|
|
|
currentUrl = newUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentUrl !== currentRef) {
|
2021-11-08 20:01:35 +01:00
|
|
|
trackView();
|
2020-09-16 12:07:22 +02:00
|
|
|
}
|
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) {
|
2021-11-08 20:01:35 +01:00
|
|
|
const umami = eventValue => trackEvent(eventValue);
|
2020-09-19 00:26:45 +02:00
|
|
|
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;
|
2020-09-18 11:33:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-15 00:09:09 +02:00
|
|
|
/* Start */
|
2020-09-18 22:40:46 +02:00
|
|
|
|
2021-11-08 20:01:35 +01:00
|
|
|
if (autoTrack && !disableTracking()) {
|
2020-09-18 22:40:46 +02:00
|
|
|
history.pushState = hook(history, 'pushState', handlePush);
|
|
|
|
history.replaceState = hook(history, 'replaceState', handlePush);
|
|
|
|
|
2021-11-08 20:01:35 +01:00
|
|
|
const update = () => {
|
2022-01-15 04:10:46 +01:00
|
|
|
if (document.readyState === 'complete') {
|
|
|
|
addEvents(document);
|
|
|
|
trackView();
|
|
|
|
|
|
|
|
const observer = new MutationObserver(monitorMutate);
|
|
|
|
observer.observe(document, { childList: true, subtree: true });
|
2021-11-08 20:01:35 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
document.addEventListener('readystatechange', update, true);
|
|
|
|
update();
|
2020-09-03 17:53:39 +02:00
|
|
|
}
|
2020-08-21 04:17:27 +02:00
|
|
|
})(window);
|