Skip to content

Commit 8f0423a

Browse files
committed
fix: improved matomo analytics
1 parent 86df936 commit 8f0423a

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

layouts/partials/analytics/matomo.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,44 @@
1616
})();
1717
</script>
1818
<!-- End Matomo Code -->
19+
20+
<!-- CSP-Friendly Matomo Tracking Fallback -->
21+
<script>
22+
// Check if standard Matomo tracking failed due to CSP
23+
window.addEventListener('load', function() {
24+
// If _paq is defined but matomo.js failed to load, use fallback
25+
if (typeof _paq !== 'undefined' && !window.Matomo) {
26+
console.warn('Matomo: Standard tracking blocked, using CSP-friendly fallback');
27+
28+
// Manual tracking without loading external scripts
29+
fetch('{{ .matomoURL }}matomo.php?' + new URLSearchParams({
30+
'idsite': '{{ .matomoSiteId }}',
31+
'rec': '1',
32+
'action_name': document.title,
33+
'url': window.location.href,
34+
'urlref': document.referrer,
35+
'rand': Math.floor(Math.random() * 1000000),
36+
'h': new Date().getHours(),
37+
'm': new Date().getMinutes(),
38+
's': new Date().getSeconds()
39+
}), {
40+
method: 'GET',
41+
mode: 'no-cors'
42+
}).catch(function() {
43+
// Ultimate fallback: use image pixel for tracking
44+
var img = document.createElement('img');
45+
img.src = '{{ .matomoURL }}matomo.php?idsite={{ .matomoSiteId }}&rec=1&action_name=' +
46+
encodeURIComponent(document.title) + '&url=' +
47+
encodeURIComponent(window.location.href) + '&rand=' +
48+
Math.floor(Math.random() * 1000000);
49+
img.style.position = 'absolute';
50+
img.style.left = '-1000px';
51+
img.style.width = '1px';
52+
img.style.height = '1px';
53+
document.body.appendChild(img);
54+
});
55+
}
56+
});
57+
</script>
1958
{{- end -}}
2059
{{- end -}}

static/_headers

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
# Content Security Policy to allow Matomo analytics
3+
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://analytics.krotanote.xyz http://localhost:8080; connect-src 'self' https://analytics.krotanote.xyz http://localhost:8080; img-src 'self' data: https://analytics.krotanote.xyz http://localhost:8080; style-src 'self' 'unsafe-inline'; font-src 'self' data:
4+
5+
# Security headers
6+
X-Frame-Options: DENY
7+
X-Content-Type-Options: nosniff
8+
X-XSS-Protection: 1; mode=block
9+
Referrer-Policy: strict-origin-when-cross-origin

static/netlify.toml

Whitespace-only changes.

0 commit comments

Comments
 (0)