|
16 | 16 | })(); |
17 | 17 | </script> |
18 | 18 | <!-- 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> |
19 | 58 | {{- end -}} |
20 | 59 | {{- end -}} |
0 commit comments