Skip to content

Commit c3037ce

Browse files
Address PR review feedback on consent manager
- Remove dead GOOGLE_ANALYTICS_ID option (gtag config is in _Layout.cshtml) - Remove dead consentManagerReady and consentUpdated event dispatches - Delete malformed consent cookie so banner re-shows on parse error - Fix clearTrackingCookies() to handle multi-part TLDs by iterating all parent domain suffixes instead of assuming a 2-label registrable domain
1 parent 918f65b commit c3037ce

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

EssentialCSharp.Web/wwwroot/js/consent-manager.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class ConsentManager {
99
this.COOKIE_NAME = 'essential-csharp-consent';
1010
this.COOKIE_DURATION = 365; // days
1111
this.CONSENT_VERSION = '2'; // Bump this to re-prompt all users when consent terms change
12-
this.GOOGLE_ANALYTICS_ID = options.googleAnalyticsId || 'G-761B4BMK2R';
1312
this.consentState = {
1413
analytics_storage: 'denied',
1514
ad_storage: 'denied',
@@ -43,13 +42,12 @@ class ConsentManager {
4342
}
4443

4544
dispatchInitializationEvent() {
46-
const event = new CustomEvent('consentManagerReady', {
45+
document.dispatchEvent(new CustomEvent('consentManagerReady', {
4746
detail: {
4847
hasAnalyticsConsent: this.hasAnalyticsConsent(),
4948
hasAdvertisingConsent: this.hasAdvertisingConsent()
5049
}
51-
});
52-
document.dispatchEvent(event);
50+
}));
5351
}
5452

5553
initGoogleConsentMode() {
@@ -90,7 +88,9 @@ class ConsentManager {
9088
this.consentState = { ...this.consentState, ...validatedPreferences };
9189
this.updateConsentMode();
9290
} catch (e) {
91+
// Malformed cookie — delete it so the banner is shown again
9392
console.warn('Failed to parse consent preferences', e);
93+
this.deleteCookie(this.COOKIE_NAME);
9494
}
9595
}
9696
}
@@ -262,14 +262,6 @@ class ConsentManager {
262262

263263
// Remove banner
264264
this.removeConsentBanner();
265-
266-
// Notify layout scripts so they can fire gtag('config') on interactive consent
267-
document.dispatchEvent(new CustomEvent('consentUpdated', {
268-
detail: {
269-
hasAnalyticsConsent: this.hasAnalyticsConsent(),
270-
hasAdvertisingConsent: this.hasAdvertisingConsent()
271-
}
272-
}));
273265
}
274266

275267
updateConsentMode() {
@@ -461,14 +453,19 @@ class ConsentManager {
461453
// Clear common tracking cookies (Google Analytics and Microsoft Clarity)
462454
const trackingCookies = ['_ga', '_gid', '_gat', '_clck', '_clsk', 'CLID', 'ANONCHK', 'MR', 'MUID', 'SM'];
463455
const expired = 'expires=Thu, 01 Jan 1970 00:00:00 GMT';
464-
// GA and Clarity cookies are often set on the root domain, so attempt deletion on both the
465-
// exact hostname and the parent domain (e.g. .essentialcsharp.com)
466456
const hostname = window.location.hostname;
467-
const rootDomain = '.' + hostname.split('.').slice(-2).join('.');
457+
// Build candidate domains: exact host plus progressively shorter parent domains.
458+
// This handles multi-part TLDs (e.g. .co.uk) by trying all suffixes.
459+
const parts = hostname.split('.');
460+
const domains = [hostname];
461+
for (let i = 1; i < parts.length - 1; i++) {
462+
domains.push('.' + parts.slice(i).join('.'));
463+
}
468464
trackingCookies.forEach(cookieName => {
469465
document.cookie = `${cookieName}=;${expired};path=/`;
470-
document.cookie = `${cookieName}=;${expired};path=/;domain=${hostname}`;
471-
document.cookie = `${cookieName}=;${expired};path=/;domain=${rootDomain}`;
466+
domains.forEach(domain => {
467+
document.cookie = `${cookieName}=;${expired};path=/;domain=${domain}`;
468+
});
472469
});
473470
}
474471
}

0 commit comments

Comments
 (0)