-
Notifications
You must be signed in to change notification settings - Fork 2
/
analytics-body.html
29 lines (28 loc) · 1.06 KB
/
analytics-body.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<script>
let links = document.querySelectorAll("a[data-analytics]");
for (var i = 0; i < links.length; i++) {
links[i].addEventListener('click', handleLinkEvent);
links[i].addEventListener('auxclick', handleLinkEvent);
}
function handleLinkEvent(event) {
var link = event.target;
var middle = event.type == "auxclick" && event.which == 2;
var click = event.type == "click";
while (link && (typeof link.tagName == 'undefined' || link.tagName.toLowerCase() != 'a' || !link.href)) {
link = link.parentNode;
}
if (middle || click) {
let attributes = link.getAttribute('data-analytics').split(/,(.+)/);
let events = [JSON.parse(attributes[0]), JSON.parse(attributes[1] || '{}')];
plausible(...events);
}
if (!link.target) {
if (!(event.ctrlKey || event.metaKey || event.shiftKey) && click) {
setTimeout(function () {
location.href = link.href;
}, 150);
event.preventDefault();
}
}
}
</script>