links/app.js

40 lines
914 B
JavaScript
Raw Permalink Normal View History

2023-01-03 14:48:18 -06:00
/* global umami */
document.addEventListener('click', getFollows);
function getFollows(evt) {
const el = evt.target;
if (el.tagName.toLowerCase() !== 'a') {
2024-05-08 16:21:13 -05:00
return true;
2023-01-03 14:48:18 -06:00
}
const name = el.dataset.name;
if (!name) {
2024-05-08 16:21:13 -05:00
return true;
2023-01-03 14:48:18 -06:00
}
const url = el.href;
if (!url) {
2024-05-08 16:21:13 -05:00
return true;
2023-01-03 14:48:18 -06:00
}
if (!window.umami) {
2024-05-08 16:21:13 -05:00
return true;
2023-01-03 14:48:18 -06:00
}
const umProm = umami.trackEvent('visit', { type: 'click', site: name });
2024-05-08 16:21:13 -05:00
if (!umProm) {
return true;
}
2023-01-03 14:48:18 -06:00
if (!el.target || el.target.match(/^_(self|parent|top)$/i)) {
// If we're not opening in a new tab, then try to send event before navigating away
// Don't wait too long though (half a second, at most)
evt.preventDefault();
umProm.then(() => location.href = url).catch(() => location.href = url);
setTimeout(() => location.href = url, 500);
}
}