umami/scripts/umami/index.js

135 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-07-18 04:15:29 +02:00
import 'promise-polyfill/src/polyfill';
import 'unfetch/polyfill';
const {
screen: { width, height },
navigator: { language },
location: { hostname, pathname, search },
localStorage: store,
document,
2020-07-19 07:51:17 +02:00
history,
2020-07-18 04:15:29 +02:00
} = window;
2020-07-17 10:03:38 +02:00
2020-07-19 10:57:01 +02:00
/* Load script */
2020-07-18 04:15:29 +02:00
const script = document.querySelector('script[data-website-id]');
if (script) {
2020-07-17 10:03:38 +02:00
const website_id = script.getAttribute('data-website-id');
if (website_id) {
2020-07-19 07:51:17 +02:00
const sessionKey = 'umami.session';
const hostUrl = new URL(script.src).origin;
2020-07-17 10:03:38 +02:00
const screen = `${width}x${height}`;
2020-07-19 07:51:17 +02:00
let currentUrl = `${pathname}${search}`;
let currenrRef = document.referrer;
2020-07-19 22:08:13 +02:00
const listeners = [];
2020-07-19 07:51:17 +02:00
2020-07-19 10:57:01 +02:00
/* Helper methods */
2020-07-19 07:51:17 +02:00
const post = (url, params) =>
fetch(url, {
method: 'post',
cache: 'no-cache',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(params),
}).then(res => res.json());
const createSession = data =>
post(`${hostUrl}/api/session`, data).then(({ success, ...session }) => {
if (success) {
store.setItem(sessionKey, JSON.stringify(session));
return success;
}
});
const getSession = () => JSON.parse(store.getItem(sessionKey));
const getSessionData = url => ({ website_id, hostname, url, screen, language });
const pageView = (url, referrer) =>
post(`${hostUrl}/api/collect`, {
type: 'pageview',
payload: { url, referrer, session: getSession() },
}).then(({ success }) => {
if (!success) {
store.removeItem(sessionKey);
}
return success;
});
2020-07-19 11:23:15 +02:00
const trackEvent = (url, event_type, event_value) =>
2020-07-19 10:57:01 +02:00
post(`${hostUrl}/api/collect`, {
type: 'event',
2020-07-19 11:23:15 +02:00
payload: { url, event_type, event_value, session: getSession() },
2020-07-19 10:57:01 +02:00
});
2020-07-19 07:51:17 +02:00
const execute = (url, referrer) => {
const data = getSessionData(url);
if (!store.getItem(sessionKey)) {
createSession(data).then(success => success && pageView(url, referrer));
} else {
2020-07-19 09:31:16 +02:00
pageView(url, referrer).then(
success =>
!success && createSession(data).then(success => success && pageView(url, referrer)),
);
2020-07-19 07:51:17 +02:00
}
};
2020-07-19 10:57:01 +02:00
/* Handle push state */
2020-07-19 07:51:17 +02:00
const handlePush = (state, title, url) => {
2020-07-19 22:08:13 +02:00
removeEvents();
2020-07-19 07:51:17 +02:00
currenrRef = currentUrl;
currentUrl = url;
execute(currentUrl, currenrRef);
2020-07-19 22:08:13 +02:00
setTimeout(loadEvents, 300);
2020-07-19 07:51:17 +02:00
};
const hook = (type, cb) => {
const orig = history[type];
return (state, title, url) => {
const args = [state, title, url];
cb.apply(null, args);
return orig.apply(history, args);
};
};
history.pushState = hook('pushState', handlePush);
history.replaceState = hook('replaceState', handlePush);
2020-07-19 10:57:01 +02:00
/* Handle events */
2020-07-19 22:08:13 +02:00
const removeEvents = () => {
listeners.forEach(([element, type, listener]) => {
element.removeEventListener(type, listener, true);
});
listeners.length = 0;
};
const loadEvents = () => {
document.querySelectorAll("[class*='umami--']").forEach(element => {
element.className.split(' ').forEach(className => {
if (/^umami--/.test(className)) {
const [, type, value] = className.split('--');
if (type && value) {
const listener = () => trackEvent(currentUrl, type, value);
listeners.push([element, type, listener]);
element.addEventListener(type, listener, true);
}
2020-07-19 10:57:01 +02:00
}
2020-07-19 22:08:13 +02:00
});
2020-07-19 10:57:01 +02:00
});
2020-07-19 22:08:13 +02:00
};
2020-07-19 10:57:01 +02:00
/* Start */
2020-07-19 07:51:17 +02:00
execute(currentUrl, currenrRef);
2020-07-19 22:08:13 +02:00
loadEvents();
2020-07-17 10:03:38 +02:00
}
2020-07-18 04:15:29 +02:00
}