Skip to content

Commit 12f8b19

Browse files
Fix Plausible link events dropping the actual click event sometimes
1 parent 096cd18 commit 12f8b19

2 files changed

Lines changed: 45 additions & 17 deletions

File tree

_layouts/default.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
{{- content -}}
1313
</div>
1414
{%- include footer.html -%}
15-
<script async src="/js/wi-v2024.07.07.js"></script>
15+
<script async src="/js/wi-v2026.03.14.js"></script>
1616
</body>
1717
</html>

js/wi.js

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: none
3-
permalink: "/js/wi-v2024.07.07.js"
3+
permalink: "/js/wi-v2026.03.14.js"
44
---
55
/* Wurst-Imperium JS */
66

@@ -10,13 +10,49 @@ for (var i = 0; i < links.length; i++) {
1010
links[i].addEventListener('click', handleLinkEvent);
1111
links[i].addEventListener('auxclick', handleLinkEvent);
1212
}
13+
function sendPlausibleEvent(eventName, eventProps) {
14+
let script = document.querySelector('script[data-domain][src*="/js/script"]');
15+
if (!script || !script.getAttribute("data-domain")) {
16+
plausible(eventName, {props: eventProps});
17+
return;
18+
}
19+
20+
let endpoint = script.getAttribute("data-api");
21+
if (!endpoint)
22+
endpoint = new URL(script.src).origin + "/api/event";
23+
24+
let payload = {
25+
n: eventName,
26+
u: location.href,
27+
d: script.getAttribute("data-domain"),
28+
r: document.referrer || null,
29+
p: eventProps,
30+
v: 33
31+
};
32+
let body = JSON.stringify(payload);
33+
34+
if (navigator.sendBeacon) {
35+
let blob = new Blob([body], {type: "text/plain"});
36+
navigator.sendBeacon(endpoint, blob);
37+
return;
38+
}
39+
40+
if (window.fetch) {
41+
fetch(endpoint, {
42+
method: "POST",
43+
headers: {"Content-Type": "text/plain"},
44+
body: body,
45+
keepalive: true
46+
}).catch(function() {});
47+
return;
48+
}
49+
50+
plausible(eventName, {props: eventProps});
51+
}
1352
function handleLinkEvent(event) {
14-
var link = event.target;
15-
var middle = event.type == "auxclick" && event.which == 2;
53+
var link = event.currentTarget;
54+
var middle = event.type == "auxclick" && event.button == 1;
1655
var click = event.type == "click";
17-
while (link && (typeof link.tagName == 'undefined' || link.tagName.toLowerCase() != 'a' || !link.href)) {
18-
link = link.parentNode;
19-
}
2056
if (middle || click) {
2157
let eventName = link.getAttribute('data-analytics');
2258
let eventProps = {};
@@ -27,18 +63,10 @@ function handleLinkEvent(event) {
2763
eventProps[propName] = eventPropsAttrs[i].value;
2864
}
2965
}
30-
plausible(eventName, {props: eventProps});
31-
if (/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(window.location.hostname) || "file:" === window.location.protocol)
66+
sendPlausibleEvent(eventName, eventProps);
67+
if (/^localhost$|(^|\.)wurstclient\.local$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(window.location.hostname) || "file:" === window.location.protocol)
3268
console.log(eventName, {props: eventProps});
3369
}
34-
if (!link.target) {
35-
if (!(event.ctrlKey || event.metaKey || event.shiftKey) && click) {
36-
setTimeout(function () {
37-
location.href = link.href;
38-
}, 150);
39-
event.preventDefault();
40-
}
41-
}
4270
}
4371

4472
/* Custom header navbar */

0 commit comments

Comments
 (0)