Skip to content

Commit 261a7ef

Browse files
committed
Fix Google Analytics production issues
- Consolidate Google Analytics and Consent Mode into single implementation - Remove duplicate gtag initializations that could cause conflicts - Ensure proper consent defaults for production deployment - Fix potential domain/referrer issues for dailynest.github.io - Build successful with 52 pages generated
1 parent 42e51f1 commit 261a7ef

File tree

3 files changed

+41
-45
lines changed

3 files changed

+41
-45
lines changed

public/sw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Minimal service worker for DailyNest
2-
- Provides a safe no-op fetch handler and basic install/activate flows
2+
- Provides basic install/activate flows for immediate activation
33
- Listens for push and notificationclick events
44
- Expand this with caching or push handling as needed
55
*/

src/components/bases/head.astro

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,28 @@ const OGImage = new URL(meta.ogImage, Astro.url).href;
4545
crossorigin="anonymous"></script>
4646
)}
4747

48-
<!-- Google Consent Mode v2 -->
49-
{hasGoogleAdSense() && (
50-
<script is:inline>
51-
// Initialize Google Consent Mode v2
52-
window.dataLayer = window.dataLayer || [];
53-
function gtag(){dataLayer.push(arguments);}
54-
gtag('js', new Date());
55-
56-
// Set default consent state (denied until user consents)
57-
gtag('consent', 'default', {
58-
'ad_storage': 'denied',
59-
'ad_user_data': 'denied',
60-
'ad_personalization': 'denied',
61-
'analytics_storage': 'denied',
62-
'functionality_storage': 'granted',
63-
'security_storage': 'granted'
64-
});
65-
</script>
48+
<!-- Google Analytics and Consent Mode -->
49+
{hasGoogleAnalytics() && (
50+
<>
51+
<script async src={`https://www.googletagmanager.com/gtag/js?id=${GOOGLE_CONFIG.GA_MEASUREMENT_ID}`}></script>
52+
<script is:inline define:vars={{ gaId: GOOGLE_CONFIG.GA_MEASUREMENT_ID }}>
53+
window.dataLayer = window.dataLayer || [];
54+
function gtag(){dataLayer.push(arguments);}
55+
gtag('js', new Date());
56+
57+
// Set default consent state (granted by default)
58+
gtag('consent', 'default', {
59+
'ad_storage': 'granted',
60+
'ad_user_data': 'granted',
61+
'ad_personalization': 'granted',
62+
'analytics_storage': 'granted',
63+
'functionality_storage': 'granted',
64+
'security_storage': 'granted'
65+
});
66+
67+
gtag('config', gaId);
68+
</script>
69+
</>
6670
)}
6771

6872
<!-- Google Tag Manager -->
@@ -76,19 +80,7 @@ const OGImage = new URL(meta.ogImage, Astro.url).href;
7680
</script>
7781
)}
7882

79-
<!-- Google Analytics (gtag.js) -->
80-
{hasGoogleAnalytics() && (
81-
<>
82-
<script async src={`https://www.googletagmanager.com/gtag/js?id=${GOOGLE_CONFIG.GA_MEASUREMENT_ID}`}></script>
83-
<script is:inline define:vars={{ gaId: GOOGLE_CONFIG.GA_MEASUREMENT_ID }}>
84-
window.dataLayer = window.dataLayer || [];
85-
function gtag(){dataLayer.push(arguments);}
86-
gtag('js', new Date());
8783

88-
gtag('config', gaId);
89-
</script>
90-
</>
91-
)}
9284

9385
<!-- RSS & Sitemap -->
9486
<link rel="sitemap" href="/sitemap-index.xml" />
@@ -103,8 +95,8 @@ const OGImage = new URL(meta.ogImage, Astro.url).href;
10395
<link rel="canonical" href={meta.canonical || canonicalURL} />
10496

10597
<!-- Page Metadata -->
106-
<title>{isArticleMeta(meta) || Astro.url.pathname.includes('/authors/') ? meta.title : Astro.url.pathname.includes('/categories/') ? `${Astro.url.pathname.split('/')[2].charAt(0).toUpperCase() + Astro.url.pathname.split('/')[2].slice(1)} Articles | ${SITE.title}` : `${meta.title} - Technology, Programming & Lifestyle`}</title>
107-
<meta name="title" content={isArticleMeta(meta) || Astro.url.pathname.includes('/authors/') ? meta.metaTitle : Astro.url.pathname.includes('/categories/') ? `${Astro.url.pathname.split('/')[2].charAt(0).toUpperCase() + Astro.url.pathname.split('/')[2].slice(1)} Articles` : `${meta.metaTitle} - Technology, Programming & Lifestyle`} />
98+
<title>{isArticleMeta(meta) || Astro.url.pathname.includes('/authors/') ? meta.title : Astro.url.pathname.includes('/categories/') ? `${Astro.url.pathname.split('/')[2].charAt(0).toUpperCase() + Astro.url.pathname.split('/')[2].slice(1)} Articles | ${SITE.title}` : meta.title}</title>
99+
<meta name="title" content={isArticleMeta(meta) || Astro.url.pathname.includes('/authors/') ? meta.metaTitle : Astro.url.pathname.includes('/categories/') ? `${Astro.url.pathname.split('/')[2].charAt(0).toUpperCase() + Astro.url.pathname.split('/')[2].slice(1)} Articles` : meta.metaTitle} />
108100
<meta name="description" content={meta.description} />
109101
{meta.robots ? <meta name="robots" content={meta.robots} /> : null}
110102
{
@@ -127,7 +119,7 @@ const OGImage = new URL(meta.ogImage, Astro.url).href;
127119
<!-- Open Graph / Facebook -->
128120
<meta property="og:type" content={meta.type} />
129121
<meta property="og:url" content={canonicalURL} />
130-
<meta property="og:title" content={isArticleMeta(meta) || Astro.url.pathname.includes('/authors/') ? meta.title : Astro.url.pathname.includes('/categories/') ? `${Astro.url.pathname.split('/')[2].charAt(0).toUpperCase() + Astro.url.pathname.split('/')[2].slice(1)} Articles | ${SITE.title}` : `${meta.title} - Technology, Programming & Lifestyle`} />
122+
<meta property="og:title" content={isArticleMeta(meta) || Astro.url.pathname.includes('/authors/') ? meta.title : Astro.url.pathname.includes('/categories/') ? `${Astro.url.pathname.split('/')[2].charAt(0).toUpperCase() + Astro.url.pathname.split('/')[2].slice(1)} Articles | ${SITE.title}` : meta.title} />
131123
<meta property="og:description" content={meta.description} />
132124
<meta property="og:image" content={OGImage} />
133125
<meta property="og:image:alt" content={meta.ogImageAlt} />
@@ -136,7 +128,7 @@ const OGImage = new URL(meta.ogImage, Astro.url).href;
136128
<meta property="twitter:site" content={Astro.site} />
137129
<meta property="twitter:card" content="summary_large_image" />
138130
<meta property="twitter:url" content={canonicalURL} />
139-
<meta property="twitter:title" content={isArticleMeta(meta) || Astro.url.pathname.includes('/authors/') ? meta.title : Astro.url.pathname.includes('/categories/') ? `${Astro.url.pathname.split('/')[2].charAt(0).toUpperCase() + Astro.url.pathname.split('/')[2].slice(1)} Articles | ${SITE.title}` : `${meta.title} - Technology, Programming & Lifestyle`} />
131+
<meta property="twitter:title" content={isArticleMeta(meta) || Astro.url.pathname.includes('/authors/') ? meta.title : Astro.url.pathname.includes('/categories/') ? `${Astro.url.pathname.split('/')[2].charAt(0).toUpperCase() + Astro.url.pathname.split('/')[2].slice(1)} Articles | ${SITE.title}` : meta.title} />
140132
<meta property="twitter:description" content={meta.description} />
141133
<meta property="twitter:image" content={OGImage} />
142134
<meta property="twitter:image:alt" content={meta.ogImageAlt} />

src/components/shared/cookie-consent.astro

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,21 @@
159159
const consent = this.getCookieConsent();
160160

161161
if (!consent) {
162-
// Show banner if no consent given yet
163-
this.showBanner();
162+
// Apply default consent (all granted)
163+
const defaultConsent = {
164+
analytics: true,
165+
advertising: true,
166+
functional: true,
167+
timestamp: Date.now(),
168+
};
169+
this.applyConsent(defaultConsent);
170+
} else {
171+
// Apply existing preferences
172+
this.applyConsent(consent);
164173
}
165174

166175
// Always setup event listeners (for customize functionality)
167176
this.setupEventListeners();
168-
169-
if (consent) {
170-
// Apply existing preferences
171-
this.applyConsent(consent);
172-
}
173177
}
174178

175179
showBanner() {
@@ -209,9 +213,9 @@
209213
if (rejectBtn) {
210214
rejectBtn.addEventListener("click", () => {
211215
const consent = {
212-
analytics: false,
213-
advertising: true, // Always true for AdSense (mandatory)
214-
functional: true, // Always true for functionality (mandatory)
216+
analytics: false, // Only reject analytics
217+
advertising: true, // Keep ads enabled
218+
functional: true, // Keep functional enabled
215219
timestamp: Date.now(),
216220
};
217221
this.saveConsent(consent);

0 commit comments

Comments
 (0)