2020-07-18 04:15:29 +02:00
|
|
|
import 'promise-polyfill/src/polyfill';
|
|
|
|
import 'unfetch/polyfill';
|
2020-09-15 00:09:09 +02:00
|
|
|
import { doNotTrack, hook, post } 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 },
|
|
|
|
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');
|
2020-09-15 12:03:34 +02:00
|
|
|
const skipAuto = script.getAttribute('data-skip-auto');
|
2020-09-01 05:25:31 +02:00
|
|
|
const root = hostUrl
|
|
|
|
? removeTrailingSlash(hostUrl)
|
|
|
|
: new URL(script.src).href.split('/').slice(0, -1).join('/');
|
2020-07-20 10:54:21 +02:00
|
|
|
const screen = `${width}x${height}`;
|
|
|
|
const listeners = [];
|
|
|
|
|
|
|
|
let currentUrl = `${pathname}${search}`;
|
|
|
|
let currentRef = document.referrer;
|
|
|
|
|
|
|
|
/* Handle events */
|
|
|
|
|
|
|
|
const removeEvents = () => {
|
|
|
|
listeners.forEach(([element, type, listener]) => {
|
2020-07-21 04:24:33 +02:00
|
|
|
element && element.removeEventListener(type, listener, true);
|
2020-07-20 10:54:21 +02:00
|
|
|
});
|
|
|
|
listeners.length = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
const loadEvents = () => {
|
2020-09-15 00:09:09 +02:00
|
|
|
document.querySelectorAll('[class*=\'umami--\']').forEach(element => {
|
2020-07-20 10:54:21 +02:00
|
|
|
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)) {
|
2020-07-20 10:54:21 +02:00
|
|
|
const [, type, value] = className.split('--');
|
2020-09-18 11:33:17 +02:00
|
|
|
const listener = () => collectEvent(type, value);
|
2020-07-21 04:24:33 +02:00
|
|
|
|
|
|
|
listeners.push([element, type, listener]);
|
|
|
|
element.addEventListener(type, listener, true);
|
2020-07-20 10:54:21 +02:00
|
|
|
}
|
2020-07-19 10:57:01 +02:00
|
|
|
});
|
2020-07-20 10:54:21 +02:00
|
|
|
});
|
|
|
|
};
|
2020-09-16 12:07:22 +02:00
|
|
|
const collect = (type, params, uuid) => {
|
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-09-15 00:09:09 +02:00
|
|
|
};
|
2020-09-15 13:54:35 +02:00
|
|
|
|
|
|
|
if (params) {
|
|
|
|
Object.keys(params).forEach(key => {
|
|
|
|
payload[key] = params[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return post(`${root}/api/collect`, {
|
|
|
|
type,
|
|
|
|
payload,
|
2020-09-15 12:03:34 +02:00
|
|
|
});
|
2020-09-15 13:54:35 +02:00
|
|
|
};
|
2020-09-16 12:07:22 +02:00
|
|
|
const pageView = (url = currentUrl, referrer = currentRef, uuid = website) => collect('pageview', {
|
2020-09-15 13:54:35 +02:00
|
|
|
url,
|
|
|
|
referrer,
|
2020-09-15 14:10:01 +02:00
|
|
|
}, uuid);
|
2020-09-16 12:07:22 +02:00
|
|
|
|
|
|
|
/* Collect metrics */
|
|
|
|
const pageViewWithAutoEvents = (url, referrer) => pageView(url, referrer).then(() => setTimeout(loadEvents, 300));
|
|
|
|
|
|
|
|
/* Handle history */
|
|
|
|
const handlePush = (state, title, url) => {
|
|
|
|
removeEvents();
|
|
|
|
currentRef = currentUrl;
|
|
|
|
const newUrl = url.toString();
|
|
|
|
|
|
|
|
if (newUrl.substring(0, 4) === 'http') {
|
|
|
|
const { pathname, search } = new URL(newUrl);
|
|
|
|
currentUrl = `${pathname}${search}`;
|
|
|
|
} else {
|
|
|
|
currentUrl = newUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
pageViewWithAutoEvents(currentUrl, currentRef);
|
|
|
|
};
|
|
|
|
|
2020-09-18 11:33:17 +02:00
|
|
|
const collectEvent = (event_type, event_value, url = currentUrl, uuid = website) => collect('event', {
|
2020-09-15 13:54:35 +02:00
|
|
|
url,
|
|
|
|
event_type,
|
|
|
|
event_value,
|
2020-09-15 14:10:01 +02:00
|
|
|
}, uuid);
|
2020-09-16 12:07:22 +02:00
|
|
|
|
|
|
|
const registerAutoEvents = () => {
|
2020-09-15 13:54:35 +02:00
|
|
|
history.pushState = hook(history, 'pushState', handlePush);
|
|
|
|
history.replaceState = hook(history, 'replaceState', handlePush);
|
|
|
|
return pageViewWithAutoEvents(currentUrl, currentRef);
|
|
|
|
};
|
|
|
|
|
2020-09-16 12:07:22 +02:00
|
|
|
|
2020-09-18 11:33:17 +02:00
|
|
|
const umamiFunctions = { collect, pageView, collectEvent, registerAutoEvents };
|
2020-09-16 12:07:22 +02:00
|
|
|
const scheduledCalls = window.umami.calls;
|
2020-09-18 11:33:17 +02:00
|
|
|
|
|
|
|
window.umami = event_value => collect('event', { event_type: 'custom', event_value });
|
|
|
|
Object.keys(umamiFunctions).forEach((key) => {
|
|
|
|
window.umami[key] = umamiFunctions[key];
|
2020-09-15 13:54:35 +02:00
|
|
|
});
|
2020-07-19 10:57:01 +02:00
|
|
|
|
2020-09-18 11:33:17 +02:00
|
|
|
if (scheduledCalls) {
|
|
|
|
scheduledCalls.forEach(([fnName, ...params]) => {
|
|
|
|
window.umami[fnName].apply(window.umami, params);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-15 00:09:09 +02:00
|
|
|
/* Start */
|
|
|
|
if (!skipAuto) {
|
2020-09-16 12:07:22 +02:00
|
|
|
registerAutoEvents().catch(e => console.error(e));
|
2020-09-03 17:53:39 +02:00
|
|
|
}
|
2020-08-21 04:17:27 +02:00
|
|
|
})(window);
|