|
45 | 45 | /> |
46 | 46 | </noscript> |
47 | 47 |
|
48 | | -<script is:inline> |
| 48 | +<!-- |
| 49 | + data-astro-rerun: with <ClientRouter/>, this inline script re-executes on every |
| 50 | + View-Transition navigation, so the consent buttons get re-bound on the freshly |
| 51 | + swapped DOM and each SPA page records its own analytics page_view. Cross-navigation |
| 52 | + state (analytics already loaded?) is kept on `window`, which persists across swaps. |
| 53 | +--> |
| 54 | +<script is:inline data-astro-rerun> |
49 | 55 | (function () { |
50 | | - if (window.__libredbConsentWired) return; |
51 | | - window.__libredbConsentWired = true; |
52 | | - |
53 | 56 | var GA_ID = 'G-Q59L35YFPW'; |
54 | 57 | var META_PIXEL_ID = '25949277121347381'; |
55 | 58 | var STORAGE_KEY = 'libredb-cookie-consent'; |
56 | | - var analyticsLoaded = false; |
57 | 59 |
|
58 | 60 | function loadGA() { |
59 | | - if (document.querySelector('script[src*="googletagmanager.com/gtag"]')) return; |
| 61 | + if (window.__libredbGA) return; |
| 62 | + window.__libredbGA = true; |
60 | 63 | var s = document.createElement('script'); |
61 | 64 | s.async = true; |
62 | 65 | s.src = 'https://www.googletagmanager.com/gtag/js?id=' + GA_ID; |
63 | 66 | document.head.appendChild(s); |
64 | 67 | window.dataLayer = window.dataLayer || []; |
65 | | - window.gtag = window.gtag || function () { dataLayer.push(arguments); }; |
66 | | - gtag('js', new Date()); |
67 | | - gtag('config', GA_ID); |
| 68 | + window.gtag = window.gtag || function () { window.dataLayer.push(arguments); }; |
| 69 | + window.gtag('js', new Date()); |
| 70 | + window.gtag('config', GA_ID); |
68 | 71 | } |
69 | 72 |
|
70 | 73 | function loadMetaPixel() { |
|
77 | 80 | t.src=v;s=b.getElementsByTagName(e)[0]; |
78 | 81 | s.parentNode.insertBefore(t,s)}(window,document,'script', |
79 | 82 | 'https://connect.facebook.net/en_US/fbevents.js'); |
80 | | - fbq('init', META_PIXEL_ID); |
| 83 | + window.fbq('init', META_PIXEL_ID); |
81 | 84 | } |
82 | 85 |
|
83 | 86 | 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'); |
| 87 | + if (window.gtag) window.gtag('event', 'page_view', { page_path: location.pathname, page_location: location.href, page_title: document.title }); |
| 88 | + if (window.fbq) window.fbq('track', 'PageView'); |
86 | 89 | } |
87 | 90 |
|
88 | 91 | function enableAnalytics() { |
89 | 92 | loadGA(); |
90 | 93 | loadMetaPixel(); |
91 | | - analyticsLoaded = true; |
92 | 94 | // GA `config` records this page; fire the single Meta PageView for it too |
93 | | - if (window.fbq) fbq('track', 'PageView'); |
| 95 | + if (window.fbq) window.fbq('track', 'PageView'); |
94 | 96 | } |
95 | 97 |
|
96 | 98 | function hideBanner() { |
|
109 | 111 | } |
110 | 112 | } |
111 | 113 |
|
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; |
| 114 | + var consent = localStorage.getItem(STORAGE_KEY); |
121 | 115 |
|
122 | | - // Not yet decided: show banner after 1s and (re)bind buttons on the freshly-rendered DOM |
123 | | - setTimeout(showBanner, 1000); |
| 116 | + if (consent === 'declined') return; |
124 | 117 |
|
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(); |
| 118 | + if (consent === 'accepted') { |
| 119 | + // First time analytics are enabled this session, load them (GA `config` + |
| 120 | + // Meta PageView count this page). On later SPA navigations just track the view. |
| 121 | + if (window.__libredbAnalytics) { |
| 122 | + trackPageView(); |
| 123 | + } else { |
| 124 | + window.__libredbAnalytics = true; |
130 | 125 | enableAnalytics(); |
131 | | - }); |
132 | | - if (decline) decline.addEventListener('click', function () { |
133 | | - localStorage.setItem(STORAGE_KEY, 'declined'); |
134 | | - hideBanner(); |
135 | | - }); |
| 126 | + } |
| 127 | + return; |
136 | 128 | } |
137 | 129 |
|
138 | | - document.addEventListener('astro:page-load', wire); |
| 130 | + // Not yet decided: show the banner after 1s and bind the buttons on this |
| 131 | + // (freshly swapped) DOM. A fresh node each navigation means no duplicate listeners. |
| 132 | + setTimeout(showBanner, 1000); |
| 133 | + |
| 134 | + var accept = document.getElementById('cookie-accept'); |
| 135 | + var decline = document.getElementById('cookie-decline'); |
| 136 | + if (accept) accept.addEventListener('click', function () { |
| 137 | + localStorage.setItem(STORAGE_KEY, 'accepted'); |
| 138 | + hideBanner(); |
| 139 | + window.__libredbAnalytics = true; |
| 140 | + enableAnalytics(); |
| 141 | + }); |
| 142 | + if (decline) decline.addEventListener('click', function () { |
| 143 | + localStorage.setItem(STORAGE_KEY, 'declined'); |
| 144 | + hideBanner(); |
| 145 | + }); |
139 | 146 | })(); |
140 | 147 | </script> |
0 commit comments