Skip to content

Commit b33a2be

Browse files
committed
improved consent popup and updated readme.md
1 parent e71827f commit b33a2be

File tree

4 files changed

+152
-36
lines changed

4 files changed

+152
-36
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
RUN_KEYSTATIC=false
22

3+
# Build environment (development or production)
4+
PUBLIC_BUILD_ENV=development
5+
36
# Google AdSense configuration
47
PUBLIC_GOOGLE_ADSENSE_CLIENT_ID=ca-pub-XXXXXXXXXXXXXXXXXXXXX
58

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
# DailyNest 📰
1+
<h1 align="center">DailyNest 📰</h1>
2+
3+
<div align="center">
4+
5+
[![License](https://img.shields.io/github/license/DailyNest/dailynest.github.io)](https://github.com/DailyNest/dailynest.github.io/blob/main/LICENSE)
6+
[![Open Source](https://img.shields.io/badge/Open%20Source-Yes-green)](https://github.com/DailyNest/dailynest.github.io)
7+
[![GitHub issues](https://img.shields.io/github/issues/DailyNest/dailynest.github.io)](https://github.com/DailyNest/dailynest.github.io/issues)
8+
[![GitHub pull requests](https://img.shields.io/github/issues-pr/DailyNest/dailynest.github.io)](https://github.com/DailyNest/dailynest.github.io/pulls)
9+
[![GitHub stars](https://img.shields.io/github/stars/DailyNest/dailynest.github.io)](https://github.com/DailyNest/dailynest.github.io/stargazers)
10+
[![GitHub forks](https://img.shields.io/github/forks/DailyNest/dailynest.github.io)](https://github.com/DailyNest/dailynest.github.io/network)
11+
[![GitHub contributors](https://img.shields.io/github/contributors/DailyNest/dailynest.github.io)](https://github.com/DailyNest/dailynest.github.io/graphs/contributors)
12+
[![Website](https://img.shields.io/badge/Website-dailynest.github.io-blue)](https://dailynest.github.io/)
13+
[![HitCount](https://hits.dwyl.com/dailynest/dailynestgithubio.svg?style=flat-square)](http://hits.dwyl.com/dailynest/dailynestgithubio)
14+
15+
16+
</div>
217

318
A tech tutorials and news site built with Astro, focused on clear, readable content and a fast, accessible experience.
419

@@ -77,10 +92,16 @@ This project leverages cutting-edge web technologies:
7792
- [Vercel](https://vercel.com) - Deployment platform
7893
- [HugeIcons](https://hugeicons.com) - Icon library
7994

80-
## 📄 License
95+
## Contributors
8196

82-
This project is licensed under the Apache License 2.0. See `LICENSE.md` for details.
97+
<a href="https://github.com/DailyNest/dailynest.github.io/graphs/contributors">
98+
<img src="https://contrib.rocks/image?repo=DailyNest/dailynest.github.io" />
99+
</a>
83100

84101
## 🤝 Contributing
85102

86103
Contributions, issues, and feature requests are welcome! Feel free to check the [issues page](https://github.com/DailyNest/dailynest.github.io/issues).
104+
105+
## 📄 License
106+
107+
This project is licensed under the Apache License 2.0. See `LICENSE.md` for details.

src/components/bases/head.astro

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,38 @@ const OGImage = new URL(meta.ogImage, Astro.url).href;
4848
<!-- Google Analytics and Consent Mode -->
4949
{hasGoogleAnalytics() && (
5050
<>
51-
<script async src={`https://www.googletagmanager.com/gtag/js?id=${GOOGLE_CONFIG.GA_MEASUREMENT_ID}`}></script>
51+
<script src={`https://www.googletagmanager.com/gtag/js?id=${GOOGLE_CONFIG.GA_MEASUREMENT_ID}`}></script>
5252
<script is:inline define:vars={{ gaId: GOOGLE_CONFIG.GA_MEASUREMENT_ID }}>
5353
// Set default consent state BEFORE loading gtag
5454
window.dataLayer = window.dataLayer || [];
5555
function gtag(){dataLayer.push(arguments);}
5656

57-
// Set default consent state - advertising is mandatory, others denied until user chooses
57+
// Debug: Log when gtag is defined
58+
// console.log("Google Analytics script loaded, gtag function defined");
59+
60+
// Set default consent state - advertising and functional are mandatory, others denied until user chooses
5861
gtag('consent', 'default', {
5962
'ad_storage': 'granted', // Always granted for AdSense
6063
'ad_user_data': 'granted', // Always granted for AdSense
6164
'ad_personalization': 'granted', // Always granted for AdSense
62-
'analytics_storage': 'denied',
63-
'functionality_storage': 'denied',
64-
'security_storage': 'denied',
65-
'personalization_storage': 'denied'
65+
'analytics_storage': 'denied', // Will be updated when user consents
66+
'functionality_storage': 'granted', // Always granted for functionality
67+
'security_storage': 'granted', // Always granted for functionality
68+
'personalization_storage': 'denied' // Will be updated when user consents
6669
});
6770

6871
gtag('js', new Date());
69-
gtag('config', gaId);
72+
gtag('config', gaId, {
73+
'anonymize_ip': true,
74+
'allow_google_signals': false,
75+
'allow_ad_personalization_signals': false
76+
});
77+
78+
// Debug: Confirm gtag config is set
79+
// console.log("Google Analytics configured with ID:", gaId);
80+
81+
// Make gtag globally available for debugging
82+
window.gtag = gtag;
7083
</script>
7184
</>
7285
)}

src/components/shared/cookie-consent.astro

Lines changed: 105 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,14 @@
106106
type="checkbox"
107107
id="functional-cookies"
108108
class="checkbox checkbox-sm"
109+
checked
110+
disabled
109111
/>
110112
<div>
111113
<label
112114
for="functional-cookies"
113-
class="font-medium text-sm cursor-pointer"
114-
>Functional Cookies</label
115+
class="font-medium text-sm cursor-pointer opacity-60"
116+
>Functional Cookies (Required)</label
115117
>
116118
<p class="text-xs text-base-content/60 mt-1">
117119
Essential for website functionality and your preferences
@@ -170,14 +172,18 @@
170172
}
171173

172174
initialize() {
175+
// console.log("Initializing cookie consent");
173176
// Check if user has already made a choice
174177
const consent = this.getCookieConsent();
178+
// console.log("Existing consent:", consent);
175179

176180
if (!consent) {
177181
// Show banner for first-time users - no default consent applied
182+
// console.log("No existing consent, showing banner");
178183
this.showBanner();
179184
} else {
180185
// Apply existing preferences
186+
// console.log("Applying existing consent");
181187
this.applyConsent(consent);
182188
}
183189

@@ -194,6 +200,7 @@
194200
}
195201

196202
hideBanner() {
203+
// console.log("Hiding banner");
197204
this.consentBanner.classList.add("translate-y-full");
198205
setTimeout(() => {
199206
this.consentBanner.style.display = "none";
@@ -205,27 +212,29 @@
205212
const acceptBtn = document.getElementById("accept-cookies");
206213
if (acceptBtn) {
207214
acceptBtn.addEventListener("click", () => {
215+
// console.log("Accept All clicked");
208216
const consent = {
209217
analytics: true,
210218
advertising: true, // Always true for AdSense (mandatory)
211-
functional: true,
219+
functional: true, // Always true for functionality (mandatory)
212220
personalization: true,
213221
timestamp: Date.now(),
214222
};
223+
// console.log("Saving consent:", consent);
215224
this.saveConsent(consent);
216225
this.applyConsent(consent);
217226
this.hideBanner();
218227
});
219228
}
220229

221-
// Reject non-essential cookies (advertising remains enabled)
230+
// Reject non-essential cookies (advertising and functional remain enabled)
222231
const rejectBtn = document.getElementById("reject-cookies");
223232
if (rejectBtn) {
224233
rejectBtn.addEventListener("click", () => {
225234
const consent = {
226235
analytics: false,
227236
advertising: true, // Always true for AdSense (mandatory)
228-
functional: false,
237+
functional: true, // Always true for functionality (mandatory)
229238
personalization: false,
230239
timestamp: Date.now(),
231240
};
@@ -243,6 +252,16 @@
243252
const isHidden = this.optionsPanel.classList.contains("hidden");
244253
const textSpan = customizeBtn.querySelector("span");
245254
if (isHidden) {
255+
// Pre-check all customizable checkboxes when opening
256+
const analyticsCheckbox = document.getElementById(
257+
"analytics-cookies"
258+
) as HTMLInputElement;
259+
const personalizationCheckbox = document.getElementById(
260+
"personalization-cookies"
261+
) as HTMLInputElement;
262+
if (analyticsCheckbox) analyticsCheckbox.checked = true;
263+
if (personalizationCheckbox) personalizationCheckbox.checked = true;
264+
246265
this.optionsPanel.classList.remove("hidden");
247266
if (textSpan) {
248267
textSpan.innerHTML =
@@ -294,10 +313,11 @@
294313
const consent = {
295314
analytics: analyticsCheckbox?.checked ?? false,
296315
advertising: true, // Always true for AdSense (mandatory)
297-
functional: functionalCheckbox?.checked ?? false,
316+
functional: true, // Always true for functionality (mandatory)
298317
personalization: personalizationCheckbox?.checked ?? false,
299318
timestamp: Date.now(),
300319
};
320+
// console.log("Saving customized consent:", consent);
301321
this.saveConsent(consent);
302322
this.applyConsent(consent);
303323
this.hideBanner();
@@ -315,29 +335,59 @@
315335
}
316336

317337
applyConsent(consent: any) {
318-
// Handle advertising consent (always enabled for AdSense)
319-
this.enableAdSense();
338+
// console.log("Applying consent:", consent);
339+
// console.log("Current gtag availability:", typeof gtag);
340+
341+
// Wait for gtag to be available with longer timeout
342+
let retryCount = 0;
343+
const maxRetries = 20; // Try for up to 10 seconds
344+
345+
const applyWithDelay = () => {
346+
retryCount++;
347+
// console.log(
348+
// `Retry attempt ${retryCount}/${maxRetries}, gtag available:`,
349+
// typeof gtag !== "undefined"
350+
// );
351+
352+
if (typeof gtag !== "undefined") {
353+
// console.log("gtag is available, applying consent updates");
354+
355+
// Update all consent states at once
356+
gtag("consent", "update", {
357+
ad_storage: "granted", // Always granted for AdSense
358+
ad_user_data: "granted", // Always granted for AdSense
359+
ad_personalization: "granted", // Always granted for AdSense
360+
analytics_storage: consent.analytics ? "granted" : "denied",
361+
functionality_storage: "granted", // Always granted for functionality
362+
security_storage: "granted", // Always granted for functionality
363+
personalization_storage: consent.personalization
364+
? "granted"
365+
: "denied",
366+
});
367+
368+
// Force a pageview event to ensure tracking starts
369+
if (consent.analytics) {
370+
// console.log("Sending pageview event to ensure tracking");
371+
gtag("event", "page_view", {
372+
page_title: document.title,
373+
page_location: window.location.href,
374+
});
375+
}
320376

321-
// Handle analytics consent
322-
if (consent.analytics) {
323-
this.enableAnalytics();
324-
} else {
325-
this.disableAnalytics();
326-
}
377+
// Update Google Consent Mode defaults for future page loads
378+
this.updateConsentDefaults(consent);
327379

328-
// Handle functional consent
329-
if (consent.functional) {
330-
this.enableFunctional();
331-
} else {
332-
this.disableFunctional();
333-
}
380+
// console.log("Consent applied successfully");
381+
} else if (retryCount < maxRetries) {
382+
// console.log("gtag not available, retrying...");
383+
// Retry after a longer delay
384+
setTimeout(applyWithDelay, 500);
385+
} else {
386+
// console.error("gtag failed to load after maximum retries");
387+
}
388+
};
334389

335-
// Handle personalization consent
336-
if (consent.personalization) {
337-
this.enablePersonalization();
338-
} else {
339-
this.disablePersonalization();
340-
}
390+
applyWithDelay();
341391
}
342392

343393
enableAdSense() {
@@ -365,18 +415,24 @@
365415
enableAnalytics() {
366416
// Enable analytics tracking
367417
if (typeof gtag !== "undefined") {
418+
// console.log("Enabling analytics");
368419
gtag("consent", "update", {
369420
analytics_storage: "granted",
370421
});
422+
} else {
423+
console.warn("gtag not available for analytics enable");
371424
}
372425
}
373426

374427
disableAnalytics() {
375428
// Disable analytics tracking
376429
if (typeof gtag !== "undefined") {
430+
// console.log("Disabling analytics");
377431
gtag("consent", "update", {
378432
analytics_storage: "denied",
379433
});
434+
} else {
435+
console.warn("gtag not available for analytics disable");
380436
}
381437
}
382438

@@ -403,18 +459,41 @@
403459
enablePersonalization() {
404460
// Enable personalization cookies
405461
if (typeof gtag !== "undefined") {
462+
// console.log("Enabling personalization");
406463
gtag("consent", "update", {
407464
personalization_storage: "granted",
408465
});
466+
} else {
467+
console.warn("gtag not available for personalization enable");
409468
}
410469
}
411470

412471
disablePersonalization() {
413472
// Disable personalization cookies
414473
if (typeof gtag !== "undefined") {
474+
// console.log("Disabling personalization");
415475
gtag("consent", "update", {
416476
personalization_storage: "denied",
417477
});
478+
} else {
479+
console.warn("gtag not available for personalization disable");
480+
}
481+
}
482+
483+
updateConsentDefaults(consent: any) {
484+
// Update default consent for future page loads
485+
if (typeof gtag !== "undefined") {
486+
gtag("consent", "default", {
487+
ad_storage: "granted", // Always granted for AdSense
488+
ad_user_data: "granted", // Always granted for AdSense
489+
ad_personalization: "granted", // Always granted for AdSense
490+
analytics_storage: consent.analytics ? "granted" : "denied",
491+
functionality_storage: "granted", // Always granted for functionality
492+
security_storage: "granted", // Always granted for functionality
493+
personalization_storage: consent.personalization
494+
? "granted"
495+
: "denied",
496+
});
418497
}
419498
}
420499
}

0 commit comments

Comments
 (0)