Skip to content

Commit

Permalink
GA: consent console
Browse files Browse the repository at this point in the history
  • Loading branch information
guanlisheng committed Feb 28, 2025
1 parent 31bda11 commit 1dc8ca1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
36 changes: 32 additions & 4 deletions layouts/partials/footer/consent.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
<div id="cookieconsent" class="cookie-banner" style="display: none">
<p>By using our website, you agree to our <a href="/docs/mmex/cookiepolicy">cookie policy</a>. We use cookies to give you the best online experience.</a></p>
<button id="cookieclose" class="btn btn-primary">Got it!</button>
</div>
<div id="cookieconsent" class="cookie-banner" style="display: none;">
<p>By using our website, you agree to our <a href="/docs/mmex/cookiepolicy">cookie policy</a>. We use cookies to give you the best online experience.</p>
<button id="cookieclose" class="btn btn-primary">Got it!</button>
</div>

<script>
window.onload = function() {
var consentBanner = document.getElementById('cookieconsent');
var consentButton = document.getElementById('cookieclose');

// Check if consent has been already given
if (localStorage.getItem('cookieConsent') === 'true') {
consentBanner.style.display = 'none'; // Hide consent banner
gtag('consent', 'update', {
'ad_storage': 'granted',
'analytics_storage': 'granted'
});
} else {
consentBanner.style.display = 'block'; // Show banner if no consent
}

// When the user clicks 'Got it!', store consent and hide the banner
consentButton.addEventListener('click', function() {
localStorage.setItem('cookieConsent', 'true'); // Store consent in localStorage
consentBanner.style.display = 'none'; // Hide banner
gtag('consent', 'update', {
'ad_storage': 'granted',
'analytics_storage': 'granted'
});
});
};
</script>
10 changes: 9 additions & 1 deletion layouts/partials/head/gtag.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}

// Default to denied consent for ad_storage and analytics_storage
gtag('js', new Date());
gtag('consent', 'default', {
'ad_storage': 'denied',
'analytics_storage': 'denied'
});

// Track Google Analytics setup (this will be triggered later by consent)
gtag('config', '{{ .Site.GoogleAnalytics }}');
</script>
</script>

0 comments on commit 1dc8ca1

Please sign in to comment.