2022-02-16 05:58:17 +01:00
|
|
|
import { doNotTrack, hook } from '../lib/web';
|
|
|
|
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 },
|
2022-08-09 19:27:35 +02:00
|
|
|
location,
|
2020-11-02 07:01:30 +01:00
|
|
|
localStorage,
|
2020-07-20 10:54:21 +02:00
|
|
|
document,
|
|
|
|
history,
|
|
|
|
} = window;
|
2022-08-09 19:27:35 +02:00
|
|
|
const { hostname, pathname, search } = location;
|
|
|
|
const { currentScript } = document;
|
|
|
|
|
|
|
|
if (!currentScript) return;
|
|
|
|
|
|
|
|
const _data = 'data-';
|
|
|
|
const _false = 'false';
|
|
|
|
const attr = currentScript.getAttribute.bind(currentScript);
|
|
|
|
const website = attr(_data + 'website-id');
|
|
|
|
const hostUrl = attr(_data + 'host-url');
|
|
|
|
const autoTrack = attr(_data + 'auto-track') !== _false;
|
|
|
|
const dnt = attr(_data + 'do-not-track');
|
|
|
|
const cssEvents = attr(_data + 'css-events') !== _false;
|
|
|
|
const domain = attr(_data + 'domains') || '';
|
2021-11-08 20:01:35 +01:00
|
|
|
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--']";
|
|
|
|
|
2022-03-11 08:04:05 +01:00
|
|
|
const trackingDisabled = () =>
|
2021-11-08 20:01:35 +01:00
|
|
|
(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)
|
2022-08-09 19:27:35 +02:00
|
|
|
: currentScript.src.split('/').slice(0, -1).join('/');
|
|
|
|
const endpoint = `${root}/api/collect`;
|
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;
|
2022-03-19 06:26:23 +01:00
|
|
|
let cache;
|
2020-07-20 10:54:21 +02:00
|
|
|
|
2020-09-18 22:40:46 +02:00
|
|
|
/* Collect metrics */
|
2020-07-21 04:24:33 +02:00
|
|
|
|
2022-03-05 05:36:13 +01:00
|
|
|
const getPayload = () => ({
|
|
|
|
website,
|
|
|
|
hostname,
|
|
|
|
screen,
|
|
|
|
language,
|
|
|
|
url: currentUrl,
|
|
|
|
});
|
|
|
|
|
|
|
|
const assign = (a, b) => {
|
|
|
|
Object.keys(b).forEach(key => {
|
2022-08-09 19:27:35 +02:00
|
|
|
if (b[key] !== undefined) a[key] = b[key];
|
2021-11-08 20:01:35 +01:00
|
|
|
});
|
2022-03-05 05:36:13 +01:00
|
|
|
return a;
|
|
|
|
};
|
|
|
|
|
|
|
|
const collect = (type, payload) => {
|
2022-03-11 08:04:05 +01:00
|
|
|
if (trackingDisabled()) return;
|
2020-09-15 13:54:35 +02:00
|
|
|
|
2022-08-09 19:27:35 +02:00
|
|
|
return fetch(endpoint, {
|
2022-07-06 15:56:37 +02:00
|
|
|
method: 'POST',
|
2022-08-09 19:27:35 +02:00
|
|
|
body: JSON.stringify({ type, payload }),
|
|
|
|
headers: assign({ 'Content-Type': 'application/json' }, { ['x-umami-cache']: cache }),
|
|
|
|
})
|
|
|
|
.then(res => res.text())
|
|
|
|
.then(text => (cache = text));
|
2020-09-15 13:54:35 +02:00
|
|
|
};
|
2020-09-16 12:07:22 +02:00
|
|
|
|
2022-08-09 19:27:35 +02:00
|
|
|
const trackView = (url = currentUrl, referrer = currentRef, uuid = website) =>
|
2020-09-18 22:40:46 +02:00
|
|
|
collect(
|
|
|
|
'pageview',
|
2022-03-05 05:36:13 +01:00
|
|
|
assign(getPayload(), {
|
|
|
|
website: uuid,
|
2020-09-18 22:40:46 +02:00
|
|
|
url,
|
|
|
|
referrer,
|
2022-03-05 05:36:13 +01:00
|
|
|
}),
|
2020-09-18 22:40:46 +02:00
|
|
|
);
|
|
|
|
|
2022-08-09 19:27:35 +02:00
|
|
|
const trackEvent = (event_name, event_data, url = currentUrl, uuid = website) =>
|
2020-09-18 22:40:46 +02:00
|
|
|
collect(
|
|
|
|
'event',
|
2022-03-05 05:36:13 +01:00
|
|
|
assign(getPayload(), {
|
|
|
|
website: uuid,
|
|
|
|
url,
|
2022-07-30 07:30:09 +02:00
|
|
|
event_name,
|
|
|
|
event_data,
|
2022-03-05 05:36:13 +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-08-09 19:27:35 +02:00
|
|
|
const get = element.getAttribute.bind(element);
|
|
|
|
(get('class') || '').split(' ').forEach(className => {
|
2022-01-21 23:49:18 +01:00
|
|
|
if (!eventClass.test(className)) return;
|
2020-09-18 22:40:46 +02:00
|
|
|
|
2022-08-09 19:27:35 +02:00
|
|
|
const [, event, name] = className.split('--');
|
2022-07-30 07:30:09 +02:00
|
|
|
|
2022-01-21 23:49:18 +01:00
|
|
|
const listener = listeners[className]
|
|
|
|
? listeners[className]
|
2022-08-09 19:27:35 +02:00
|
|
|
: (listeners[className] = e => {
|
|
|
|
if (
|
|
|
|
event === 'click' &&
|
|
|
|
element.tagName === 'A' &&
|
|
|
|
!(
|
|
|
|
e.ctrlKey ||
|
|
|
|
e.shiftKey ||
|
|
|
|
e.metaKey ||
|
|
|
|
(e.button && e.button === 1) ||
|
|
|
|
get('target')
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
e.preventDefault();
|
|
|
|
trackEvent(name).then(() => {
|
|
|
|
location.href = get('href');
|
|
|
|
});
|
2022-03-05 05:36:13 +01:00
|
|
|
} else {
|
2022-07-30 07:30:09 +02:00
|
|
|
trackEvent(name);
|
2022-03-05 05:36:13 +01:00
|
|
|
}
|
|
|
|
});
|
2021-11-08 20:01:35 +01:00
|
|
|
|
2022-08-09 19:27:35 +02:00
|
|
|
element.addEventListener(event, listener, true);
|
2022-01-21 23:49:18 +01:00
|
|
|
});
|
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
|
|
|
};
|
|
|
|
|
2022-03-11 08:04:05 +01:00
|
|
|
const observeDocument = () => {
|
|
|
|
const monitorMutate = mutations => {
|
|
|
|
mutations.forEach(mutation => {
|
|
|
|
const element = mutation.target;
|
|
|
|
addEvent(element);
|
|
|
|
addEvents(element);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const observer = new MutationObserver(monitorMutate);
|
|
|
|
observer.observe(document, { childList: true, subtree: true });
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2022-03-11 08:04:05 +01:00
|
|
|
if (autoTrack && !trackingDisabled()) {
|
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') {
|
|
|
|
trackView();
|
|
|
|
|
2022-03-11 08:04:05 +01:00
|
|
|
if (cssEvents) {
|
|
|
|
addEvents(document);
|
|
|
|
observeDocument();
|
|
|
|
}
|
2021-11-08 20:01:35 +01:00
|
|
|
}
|
|
|
|
};
|
2022-03-05 05:36:13 +01:00
|
|
|
|
2021-11-08 20:01:35 +01:00
|
|
|
document.addEventListener('readystatechange', update, true);
|
2022-03-05 05:36:13 +01:00
|
|
|
|
2021-11-08 20:01:35 +01:00
|
|
|
update();
|
2020-09-03 17:53:39 +02:00
|
|
|
}
|
2020-08-21 04:17:27 +02:00
|
|
|
})(window);
|