feat: add consent banner of analystics
This commit is contained in:
@@ -1,9 +1,50 @@
|
||||
<!-- Google tag (gtag.js) -->
|
||||
<script async src="https://www.googletagmanager.com/gtag/js?id=G-JFCYXHXCN1"></script>
|
||||
<script>
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag(){dataLayer.push(arguments);}
|
||||
gtag('js', new Date());
|
||||
<!-- Cookie Consent Banner -->
|
||||
<div id="cookie-consent-banner" style="display:none; position:fixed; bottom:0; left:0; right:0; background:#333; color:#fff; padding:15px; text-align:center; z-index:9999;">
|
||||
We use cookies and analytics to improve your experience.
|
||||
<button id="accept-cookies" style="margin-left:10px; padding:5px 10px;">Accept</button>
|
||||
<button id="reject-cookies" style="margin-left:5px; padding:5px 10px;">Reject</button>
|
||||
</div>
|
||||
|
||||
gtag('config', 'G-JFCYXHXCN1');
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
const banner = document.getElementById('cookie-consent-banner');
|
||||
const accepted = localStorage.getItem('cookies-accepted');
|
||||
|
||||
// Show banner if no decision yet
|
||||
if (!accepted) {
|
||||
banner.style.display = 'block';
|
||||
} else if (accepted === 'true') {
|
||||
loadGoogleAnalytics();
|
||||
}
|
||||
|
||||
// Accept cookies
|
||||
document.getElementById('accept-cookies').addEventListener('click', function() {
|
||||
localStorage.setItem('cookies-accepted', 'true');
|
||||
banner.style.display = 'none';
|
||||
loadGoogleAnalytics();
|
||||
});
|
||||
|
||||
// Reject cookies
|
||||
document.getElementById('reject-cookies').addEventListener('click', function() {
|
||||
localStorage.setItem('cookies-accepted', 'false');
|
||||
banner.style.display = 'none';
|
||||
});
|
||||
});
|
||||
|
||||
// Function to load GA after consent
|
||||
function loadGoogleAnalytics() {
|
||||
if (!document.getElementById('ga-script')) {
|
||||
const script = document.createElement('script');
|
||||
script.id = 'ga-script';
|
||||
script.async = true;
|
||||
script.src = "https://www.googletagmanager.com/gtag/js?id=G-JFCYXHXCN1";
|
||||
document.head.appendChild(script);
|
||||
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag(){dataLayer.push(arguments);}
|
||||
gtag('js', new Date());
|
||||
gtag('config', 'G-JFCYXHXCN1');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user