|
47 | 47 |
|
48 | 48 | <script is:inline> |
49 | 49 | (function () { |
| 50 | + if (window.__libredbConsentWired) return; |
| 51 | + window.__libredbConsentWired = true; |
| 52 | + |
50 | 53 | var GA_ID = 'G-Q59L35YFPW'; |
51 | 54 | var META_PIXEL_ID = '25949277121347381'; |
52 | 55 | var STORAGE_KEY = 'libredb-cookie-consent'; |
| 56 | + var analyticsLoaded = false; |
53 | 57 |
|
54 | 58 | function loadGA() { |
55 | 59 | if (document.querySelector('script[src*="googletagmanager.com/gtag"]')) return; |
|
58 | 62 | s.src = 'https://www.googletagmanager.com/gtag/js?id=' + GA_ID; |
59 | 63 | document.head.appendChild(s); |
60 | 64 | window.dataLayer = window.dataLayer || []; |
61 | | - function gtag() { dataLayer.push(arguments); } |
| 65 | + window.gtag = window.gtag || function () { dataLayer.push(arguments); }; |
62 | 66 | gtag('js', new Date()); |
63 | 67 | gtag('config', GA_ID); |
64 | 68 | } |
|
74 | 78 | s.parentNode.insertBefore(t,s)}(window,document,'script', |
75 | 79 | 'https://connect.facebook.net/en_US/fbevents.js'); |
76 | 80 | fbq('init', META_PIXEL_ID); |
77 | | - fbq('track', 'PageView'); |
78 | 81 | } |
79 | 82 |
|
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'); |
86 | 86 | } |
87 | 87 |
|
88 | | - var consent = localStorage.getItem(STORAGE_KEY); |
89 | | - |
90 | | - if (consent === 'accepted') { |
| 88 | + function enableAnalytics() { |
91 | 89 | loadGA(); |
92 | 90 | 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'); |
94 | 94 | } |
95 | 95 |
|
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 | + } |
98 | 102 | } |
99 | 103 |
|
100 | | - // First visit: show banner after 1s delay |
101 | | - setTimeout(function () { |
| 104 | + function showBanner() { |
102 | 105 | var banner = document.getElementById('cookie-consent-banner'); |
103 | 106 | if (banner) { |
104 | 107 | banner.classList.remove('translate-y-full'); |
105 | 108 | banner.classList.add('translate-y-0'); |
106 | 109 | } |
107 | | - }, 1000); |
| 110 | + } |
108 | 111 |
|
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 | + } |
115 | 137 |
|
116 | | - document.getElementById('cookie-decline')?.addEventListener('click', function () { |
117 | | - localStorage.setItem(STORAGE_KEY, 'declined'); |
118 | | - hideBanner(); |
119 | | - }); |
| 138 | + document.addEventListener('astro:page-load', wire); |
120 | 139 | })(); |
121 | 140 | </script> |
0 commit comments