feat: add GDPR consent banner gating GA4#82
Merged
Conversation
## What Add a hand-rolled GDPR cookie/analytics consent banner that gates GA4 loading on explicit user action. New `_includes/consent_banner.html` ships the dialog markup (hidden by default via the `hidden` attribute). New `assets/js/consent.js` owns show/hide logic, keyboard accessibility, localStorage persistence, and dynamic injection of `gtag.js` only after the user clicks Accept. `assets/css/custom.css` gets the banner styling appended (themed via Bulma CSS variables so it adapts to dark mode automatically). `_includes/footer.html` is rewritten to emit `window.GA_MEASUREMENT_ID` via Liquid's `jsonify` filter, include the banner markup, load consent.js, and add a "Cookie preferences" footer link that re-opens the banner after a choice has been made. ## Why PR #81 added the GA4 measurement tag but loaded gtag.js unconditionally on every production page view — not GDPR-compliant for EU visitors. This commit gates the GA4 script injection on an explicit "Accept all" click; "Decline non-essential" records the choice and never loads any third-party tracking. Escape dismisses without recording, so the banner re-appears on the next visit (consent must be explicit, not implicit). The implementation is hand-rolled to match the codebase's existing posture (theme.js follows the same pattern); if more third-party scripts are added later, switching to a library with category-based consent (cookieconsent v3, Klaro) would scale better. ## Notes - The banner is non-modal (no `aria-modal`): page scrolling is not blocked. `role="dialog"` is declared, Escape closes, focus moves to the Decline button on show and restores on close. Focus trap is intentionally not implemented since the banner is non-modal. - GA4 ID is interpolated via `{{ site.google_analytics_id | jsonify }}` rather than bare string concat, so future `_config.yml` values containing quotes or `</script>` cannot escape the JS string context. - A `window.ANALYTICS_DRY_RUN` flag is set in non-production builds. In that mode, consent.js logs `[consent] dry run: ...` to console instead of injecting gtag.js — local testing of the banner UI does not pollute production analytics. - A three-model security review (opus/sonnet/haiku) flagged three additional findings that were **considered and intentionally not addressed in this PR**: - **localStorage tampering**: any same-origin script can set `localStorage['analytics-consent'] = 'accepted'` and bypass the banner on the next load. Accepted risk — no first-party untrusted scripts run on the site, and HMAC-signing the consent value is overkill for a personal blog. - **Inline scripts block strict CSP**: `_includes/footer.html` and `_includes/head.html` both have inline `<script>` blocks. A future `script-src 'self'` CSP would require nonces or moving the data to meta tags. Deferred — CSP is not currently on the roadmap. - **`JEKYLL_ENV=production` not asserted at build time**: if a future deploy mechanism forgets to set the env var, `ANALYTICS_DRY_RUN` would silently ship to production and analytics would never fire. GitHub Pages currently sets this automatically; deferred. Signed-off-by: jmeridth <jmeridth@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Add a hand-rolled GDPR cookie/analytics consent banner that gates GA4 loading on explicit user action. New `_includes/consent_banner.html` ships the dialog markup (hidden by default via the `hidden` attribute). New `assets/js/consent.js` owns show/hide logic, keyboard accessibility, localStorage persistence, and dynamic injection of `gtag.js` only after the user clicks Accept. `assets/css/custom.css` gets the banner styling appended (themed via Bulma CSS variables so it adapts to dark mode automatically). `_includes/footer.html` is rewritten to emit `window.GA_MEASUREMENT_ID` via Liquid's `jsonify` filter, include the banner markup, load `consent.js`, and add a "Cookie preferences" footer link that re-opens the banner after a choice has been made.
Why
PR #81 added the GA4 measurement tag but loaded `gtag.js` unconditionally on every production page view — not GDPR-compliant for EU visitors. This PR gates the GA4 script injection on an explicit "Accept all" click; "Decline non-essential" records the choice and never loads any third-party tracking. Escape dismisses without recording, so the banner re-appears on the next visit (consent must be explicit, not implicit). The implementation is hand-rolled to match the codebase's existing posture (theme.js follows the same pattern); if more third-party scripts are added later, switching to a library with category-based consent (`cookieconsent` v3, Klaro) would scale better.
Notes
Testing
Verified locally with cache wiped (`rm -rf _site .jekyll-cache`) and `bundle exec jekyll serve --livereload --future`: