Skip to content

Commit 27fbb49

Browse files
committed
fix: rebind cookie-consent + footer scripts and track SPA page-views across View Transitions
1 parent 116748d commit 27fbb49

2 files changed

Lines changed: 47 additions & 28 deletions

File tree

src/components/CookieConsent.astro

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@
4747

4848
<script is:inline>
4949
(function () {
50+
if (window.__libredbConsentWired) return;
51+
window.__libredbConsentWired = true;
52+
5053
var GA_ID = 'G-Q59L35YFPW';
5154
var META_PIXEL_ID = '25949277121347381';
5255
var STORAGE_KEY = 'libredb-cookie-consent';
56+
var analyticsLoaded = false;
5357

5458
function loadGA() {
5559
if (document.querySelector('script[src*="googletagmanager.com/gtag"]')) return;
@@ -58,7 +62,7 @@
5862
s.src = 'https://www.googletagmanager.com/gtag/js?id=' + GA_ID;
5963
document.head.appendChild(s);
6064
window.dataLayer = window.dataLayer || [];
61-
function gtag() { dataLayer.push(arguments); }
65+
window.gtag = window.gtag || function () { dataLayer.push(arguments); };
6266
gtag('js', new Date());
6367
gtag('config', GA_ID);
6468
}
@@ -74,48 +78,63 @@
7478
s.parentNode.insertBefore(t,s)}(window,document,'script',
7579
'https://connect.facebook.net/en_US/fbevents.js');
7680
fbq('init', META_PIXEL_ID);
77-
fbq('track', 'PageView');
7881
}
7982

80-
function hideBanner() {
81-
var banner = document.getElementById('cookie-consent-banner');
82-
if (banner) {
83-
banner.classList.add('translate-y-full');
84-
banner.classList.remove('translate-y-0');
85-
}
83+
function trackPageView() {
84+
if (window.gtag) gtag('event', 'page_view', { page_path: location.pathname, page_location: location.href, page_title: document.title });
85+
if (window.fbq) fbq('track', 'PageView');
8686
}
8787

88-
var consent = localStorage.getItem(STORAGE_KEY);
89-
90-
if (consent === 'accepted') {
88+
function enableAnalytics() {
9189
loadGA();
9290
loadMetaPixel();
93-
return;
91+
analyticsLoaded = true;
92+
// GA `config` records this page; fire the single Meta PageView for it too
93+
if (window.fbq) fbq('track', 'PageView');
9494
}
9595

96-
if (consent === 'declined') {
97-
return;
96+
function hideBanner() {
97+
var banner = document.getElementById('cookie-consent-banner');
98+
if (banner) {
99+
banner.classList.add('translate-y-full');
100+
banner.classList.remove('translate-y-0');
101+
}
98102
}
99103

100-
// First visit: show banner after 1s delay
101-
setTimeout(function () {
104+
function showBanner() {
102105
var banner = document.getElementById('cookie-consent-banner');
103106
if (banner) {
104107
banner.classList.remove('translate-y-full');
105108
banner.classList.add('translate-y-0');
106109
}
107-
}, 1000);
110+
}
108111

109-
document.getElementById('cookie-accept')?.addEventListener('click', function () {
110-
localStorage.setItem(STORAGE_KEY, 'accepted');
111-
hideBanner();
112-
loadGA();
113-
loadMetaPixel();
114-
});
112+
function wire() {
113+
var consent = localStorage.getItem(STORAGE_KEY);
114+
115+
if (consent === 'accepted') {
116+
if (!analyticsLoaded) enableAnalytics();
117+
else trackPageView();
118+
return;
119+
}
120+
if (consent === 'declined') return;
121+
122+
// Not yet decided: show banner after 1s and (re)bind buttons on the freshly-rendered DOM
123+
setTimeout(showBanner, 1000);
124+
125+
var accept = document.getElementById('cookie-accept');
126+
var decline = document.getElementById('cookie-decline');
127+
if (accept) accept.addEventListener('click', function () {
128+
localStorage.setItem(STORAGE_KEY, 'accepted');
129+
hideBanner();
130+
enableAnalytics();
131+
});
132+
if (decline) decline.addEventListener('click', function () {
133+
localStorage.setItem(STORAGE_KEY, 'declined');
134+
hideBanner();
135+
});
136+
}
115137

116-
document.getElementById('cookie-decline')?.addEventListener('click', function () {
117-
localStorage.setItem(STORAGE_KEY, 'declined');
118-
hideBanner();
119-
});
138+
document.addEventListener('astro:page-load', wire);
120139
})();
121140
</script>

src/components/Footer.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ const sections = [
212212
</div>
213213
</footer>
214214

215-
<script>
215+
<script data-astro-rerun>
216216
// Accordion functionality
217217
document.querySelectorAll('.accordion-trigger').forEach((trigger) => {
218218
trigger.addEventListener('click', () => {

0 commit comments

Comments
 (0)