Add Google Analytics G-LL4RX9KM6Q with cookie consent - #47
Conversation
Integrate Google Analytics (G-LL4RX9KM6Q) into VitePress in a Vue-native manner with Consent Mode v2 default denied state. Add responsive cookie consent banner sharing mume_cookie_consent cookie across subpaths. Track SPA pageviews on VitePress route changes.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Reviewer's GuideAdds a consent-gated Google Analytics integration for the VitePress SPA, using Consent Mode v2, shared localStorage/cookie persistence, route-based pageview events, a responsive banner, and Playwright coverage for accept/decline flows. Sequence diagram for consent-gated Google Analytics and SPA pageviewssequenceDiagram
participant Visitor
participant CookieConsent
participant Storage
participant GoogleAnalytics
participant VitePressRouter
CookieConsent->>CookieConsent: onMounted()
CookieConsent->>GoogleAnalytics: initGtag()
CookieConsent->>GoogleAnalytics: gtag(consent, default, denied)
CookieConsent->>Storage: getStoredConsent()
alt savedConsent is granted
CookieConsent->>CookieConsent: setConsent(true)
CookieConsent->>GoogleAnalytics: loadGtagScript()
CookieConsent->>GoogleAnalytics: gtag(config, G-LL4RX9KM6Q)
else savedConsent is denied
CookieConsent->>CookieConsent: setConsent(false)
else no saved consent
CookieConsent-->>Visitor: show consent banner
alt Visitor accepts
Visitor->>CookieConsent: accept()
CookieConsent->>Storage: saveConsentPreference(granted)
CookieConsent->>CookieConsent: setConsent(true)
CookieConsent->>GoogleAnalytics: loadGtagScript()
CookieConsent->>GoogleAnalytics: gtag(config, G-LL4RX9KM6Q)
else Visitor declines
Visitor->>CookieConsent: decline()
CookieConsent->>Storage: saveConsentPreference(denied)
CookieConsent->>CookieConsent: setConsent(false)
end
end
VitePressRouter->>VitePressRouter: onAfterRouteChange(to)
VitePressRouter->>GoogleAnalytics: gtag(event, page_view, page_path)
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="docs/.vitepress/theme/components/CookieConsent.vue" line_range="68" />
<code_context>
+ if (ls) return ls
+ } catch {}
+ const match = document.cookie.match(new RegExp('(?:^|; )' + CONSENT_KEY + '=([^;]*)'))
+ return match ? decodeURIComponent(match[1]) : null
+ }
+
</code_context>
<issue_to_address>
**issue (bug_risk):** decodeURIComponent(match[1]) throws a URIError when the consent cookie contains malformed percent-encoding, so the component's onMounted handler aborts and the page can fail to hydrate correctly.
**Triggers:** When a malformed mume_cookie_consent cookie is present.
**Suggested fix:** Wrap cookie decoding in its own try/catch and treat invalid values as absent consent.
```suggestion
if (!match) return null
try {
return decodeURIComponent(match[1])
} catch {
return null
}
```
</issue_to_address>
### Comment 2
<location path="docs/.vitepress/theme/components/CookieConsent.vue" line_range="64-65" />
<code_context>
+
+ const getStoredConsent = (): string | null => {
+ try {
+ const ls = localStorage.getItem(CONSENT_KEY)
+ if (ls) return ls
+ } catch {}
+ const match = document.cookie.match(new RegExp('(?:^|; )' + CONSENT_KEY + '=([^;]*)'))
</code_context>
<issue_to_address>
**issue (bug_risk):** An arbitrary or stale value in localStorage shadows a valid consent cookie because any non-empty localStorage value is returned without validation, causing the banner to remain hidden while neither granted nor denied consent is applied.
**Triggers:** When localStorage contains an invalid non-empty mume_cookie_consent value and the cookie contains a valid preference.
**Suggested fix:** Accept only 'granted' or 'denied' from localStorage; otherwise fall back to the cookie and show the banner if neither source is valid.
```suggestion
const ls = localStorage.getItem(CONSENT_KEY)
if (ls === 'granted' || ls === 'denied') return ls
```
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and if the consent gating is wrong, page-view data could be sent to Google without the user's valid consent, and that exposure is not undone by reverting the change. Reverting stops future collection but cannot retract data already transmitted.
Blocking findings: docs/.vitepress/theme/components/CookieConsent.vue:68, docs/.vitepress/theme/components/CookieConsent.vue:65
Strictly validate localStorage values for granted/denied consent and wrap cookie decodeURIComponent in try/catch to handle malformed values.
Adds Google Analytics (G-LL4RX9KM6Q) with Google Consent Mode v2 and a responsive cookie consent banner. Shared cookie consent identifier (
mume_cookie_consent) stored via localStorage and cookie (path=/). Implements Vue-native SPA pageview tracking on route changes.PR created automatically by Jules for task 17241253057543500964 started by @nschimme
Summary by Sourcery
Add consent-aware Google Analytics tracking and a responsive cookie consent experience to the documentation site.
New Features:
Enhancements:
Tests: