1
0
mirror of https://github.com/kremalicious/umami.git synced 2024-12-30 16:57:44 +01:00
umami/tracker/index.js

110 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-07-18 04:15:29 +02:00
import 'promise-polyfill/src/polyfill';
import 'unfetch/polyfill';
2020-08-21 11:51:42 +02:00
import { post, hook, doNotTrack } 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 },
document,
history,
} = window;
const script = document.querySelector('script[data-website-id]');
2020-08-21 04:17:27 +02:00
2020-08-23 22:35:55 +02:00
// eslint-disable-next-line no-undef
2020-08-23 22:34:00 +02:00
if (!script || (__DNT__ && doNotTrack())) return;
2020-08-21 04:17:27 +02:00
const website = script.getAttribute('data-website-id');
2020-09-01 05:25:31 +02:00
const hostUrl = script.getAttribute('data-host-url');
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-08-21 04:17:27 +02:00
/* Collect metrics */
const collect = (type, params) => {
const payload = {
url: currentUrl,
referrer: currentRef,
website,
hostname,
screen,
language,
};
2020-07-19 07:51:17 +02:00
if (params) {
Object.keys(params).forEach(key => {
payload[key] = params[key];
2020-07-19 10:57:01 +02:00
});
}
2020-07-19 10:57:01 +02:00
2020-09-01 05:25:31 +02:00
return post(`${root}/api/collect`, {
type,
payload,
2020-08-21 04:17:27 +02:00
});
};
2020-07-19 10:57:01 +02:00
const pageView = () => collect('pageview').then(() => setTimeout(loadEvents, 300));
2020-07-19 07:51:17 +02:00
const pageEvent = (event_type, event_value) => collect('event', { event_type, event_value });
2020-07-19 07:51:17 +02:00
/* Handle history */
2020-07-19 07:51:17 +02:00
2020-09-11 06:12:04 +02:00
const handlePush = (state, title, url) => {
removeEvents();
currentRef = currentUrl;
2020-09-11 06:12:04 +02:00
const newUrl = url.toString();
2020-08-23 04:59:39 +02:00
2020-09-11 06:12:04 +02:00
if (newUrl.substring(0, 4) === 'http') {
const { pathname, search } = new URL(newUrl);
currentUrl = `${pathname}${search}`;
2020-08-23 04:59:39 +02:00
} else {
2020-09-03 17:53:39 +02:00
currentUrl = newUrl;
2020-08-23 04:59:39 +02:00
}
pageView();
};
2020-07-19 10:57:01 +02:00
history.pushState = hook(history, 'pushState', handlePush);
history.replaceState = hook(history, 'replaceState', handlePush);
/* Handle events */
const removeEvents = () => {
listeners.forEach(([element, type, listener]) => {
2020-07-21 04:24:33 +02:00
element && element.removeEventListener(type, listener, true);
});
listeners.length = 0;
};
const loadEvents = () => {
document.querySelectorAll("[class*='umami--']").forEach(element => {
element.className.split(' ').forEach(className => {
2020-07-21 04:24:33 +02:00
if (/^umami--([a-z]+)--([a-z0-9_]+[a-z0-9-_]+)$/.test(className)) {
const [, type, value] = className.split('--');
2020-07-21 04:24:33 +02:00
const listener = () => pageEvent(type, value);
listeners.push([element, type, listener]);
element.addEventListener(type, listener, true);
}
2020-07-19 10:57:01 +02:00
});
});
};
2020-07-19 10:57:01 +02:00
/* Start */
2020-07-19 10:57:01 +02:00
pageView();
2020-09-03 17:53:39 +02:00
if (!window.umami) {
window.umami = event_value => collect('event', { event_type: 'custom', event_value });
}
2020-08-21 04:17:27 +02:00
})(window);