Skip to content

Add Google Analytics G-LL4RX9KM6Q with cookie consent - #47

Merged
nschimme merged 2 commits into
mainfrom
feature/add-google-analytics-17241253057543500964
Sep 8, 2026
Merged

Add Google Analytics G-LL4RX9KM6Q with cookie consent#47
nschimme merged 2 commits into
mainfrom
feature/add-google-analytics-17241253057543500964

Conversation

@nschimme

@nschimme nschimme commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

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:

  • Add Google Analytics tracking with Consent Mode v2 and SPA route-change pageview events.
  • Add a responsive cookie consent banner with accept and decline options and persisted consent preferences.

Enhancements:

  • Respect users' consent preferences across sessions using a shared localStorage and cookie identifier.

Tests:

  • Add Playwright coverage for desktop and mobile cookie consent interactions.

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.
@google-labs-jules

Copy link
Copy Markdown

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai

sourcery-ai Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds 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 pageviews

sequenceDiagram
    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)
Loading

File-Level Changes

Change Details Files
Added consent-gated Google Analytics initialization using Google Consent Mode v2.
  • Initializes the shared dataLayer and gtag shim client-side.
  • Defaults analytics and advertising consent signals to denied.
  • Loads the Google Analytics script and configures measurement only after acceptance.
  • Persists granted or denied preference in localStorage and a root-scoped SameSite cookie, with cookie fallback lookup.
docs/.vitepress/env.d.ts
docs/.vitepress/theme/components/CookieConsent.vue
Integrated a responsive cookie consent banner into the shared VitePress layout.
  • Renders the banner at the bottom of every layout.
  • Provides accept and decline actions with responsive desktop/mobile styling and transition effects.
  • Registers the consent component with the theme app.
docs/.vitepress/theme/Layout.vue
docs/.vitepress/theme/components/CookieConsent.vue
docs/.vitepress/theme/index.ts
Implemented SPA pageview tracking for VitePress route changes.
  • Hooks router after-route-change handling.
  • Sends page_view events with title, URL, path, and measurement ID after Vue updates.
  • Sends events only when gtag has been initialized.
docs/.vitepress/theme/index.ts
docs/.vitepress/env.d.ts
Added end-to-end coverage for consent behavior across viewport sizes.
  • Verifies the banner appears and accepting stores granted consent.
  • Verifies the mobile layout and declining stores denied consent.
tests/consent.spec.ts
Updated dependency lock metadata for the analytics and test integration.
  • Regenerated package lockfile entries.
package-lock.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread docs/.vitepress/theme/components/CookieConsent.vue Outdated
Comment thread docs/.vitepress/theme/components/CookieConsent.vue Outdated
Strictly validate localStorage values for granted/denied consent and wrap
cookie decodeURIComponent in try/catch to handle malformed values.
@nschimme
nschimme merged commit 21ac8fc into main Sep 8, 2026
2 checks passed
@nschimme
nschimme deleted the feature/add-google-analytics-17241253057543500964 branch September 8, 2026 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant