Quarto
Shiny
Best practices

Google Analytics with cookie consent on a Quarto site

Measure visitor traffic responsibly without firing trackers before reader opt-in

Configure Google Analytics 4 and cookie consent in Quarto profiles so dev previews stay clean and tracking scripts wait for reader opt-in.
Published

2026-09-26

Modified

2026-09-27

The problem

Adding analytics to a documentation site or technical blog seems trivial until you consider privacy rules and development noise:

  1. Unconditional tracking breaches privacy standards. Injecting a standard tracking script causes the browser to fetch third-party scripts and set tracking cookies (_ga, _ga_*) the moment a page opens. Under EU GDPR/ePrivacy rules, non-essential cookies such as analytics generally need prior opt-in consent.
  2. Preview noise pollutes production metrics. If your analytics tag is enabled in the default site configuration, every local build, automated CI render, and pull request preview logs page views into your production dashboard.
  3. Confusion between public IDs and secrets. Developers often treat Google Analytics Measurement IDs (G-XXXXXXXXXX) as confidential secrets and attempt to hide them behind environment variables. In reality, client-side tracking IDs are embedded in plain text within page source by design.

The goal is to enable Google Analytics 4 (GA4) with a clear opt-in consent mechanism, while ensuring that local previews and automated builds generate no tracking requests.

What we observed

We tested this configuration across local builds, pull requests, and live dev hosting on 2026-09-26:

  • Local and PR builds are sterile. Inspecting the rendered HTML of a PR preview showed no occurrences of googletagmanager or G-. No tracking code was present in the deployment.
  • Opt-in operates as designed. On the live deployment, visiting the site presented the consent banner. Inspection of browser storage confirmed that cookie_consent_level was initialised with tracking:false. No _ga cookies existed in the browser before consent was clicked.
  • A technical nuance on loader fetching. While Quarto blocks execution of the gtag configuration snippet, the external loader (https://www.googletagmanager.com/gtag/js?id=...) is still fetched over the network on initial page load. While no persistent cookies are set before consent, the HTTP request itself reaches Google’s servers. For teams with strict no-external-traffic requirements prior to consent, the loader tag itself must be gated. We now gate it (see Hold back the loader too).
  • The gated loader, checked on 2026-09-27. We served a live render through the Firebase Hosting emulator, with the Measurement ID swapped for a test ID so no data reached the real stream. Before consent the browser made no request to googletagmanager.com, gtag was undefined and there were no _ga cookies. After clicking I agree, gtag.js was requested and the _ga cookies appeared. After I decline (in a fresh cookie store), the next page made no request and set no _ga cookies.
  • Banner copy wording. Quarto’s default banner text contains boilerplate mentioning third-party advertising cookies. Because an engineering site runs no advertising networks, this wording is inaccurate. The wording cannot be changed through Quarto’s cookie-consent options (only prefs-text for the preferences link is configurable), and we are tracking a fix (a custom include or another consent tool).
  • Measurement IDs are public. As shown in the HTML snippet above, the Measurement ID is visible to anyone inspecting page source. It does not require storage in a secret vault (see secrets management).

The R way: Shiny and pkgdown

The same principles of environment separation and consent gating apply to the wider R ecosystem.

Shiny applications

Shiny applications often track user sessions or tab navigation by inserting gtag() calls via tags$script(). Firing gtag("event", ...) as soon as server.R runs places tracking cookies on the user’s system without permission.

To implement equivalent express consent in Shiny: 1. Store a user consent state in a reactive value or browser cookie. 2. Render the analytics script conditionally inside an observeEvent() only after the user accepts a consent modal or banner. 3. Check Sys.getenv("R_CONFIG_ACTIVE") or configuration settings so local development runs never initialise the tracking client.

pkgdown documentation sites

R package authors frequently include analytics to measure documentation usage. In pkgdown, custom scripts are added to _pkgdown.yml using template.includes:

template:
  includes:
    in_header: |
      <!-- Google tag (gtag.js) -->
      <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
      <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'G-XXXXXXXXXX');
      </script>

Unlike Quarto, pkgdown does not offer an out-of-the-box cookie-consent engine. Adding raw gtag.js in in_header fires tracking immediately for every reader. If your package documentation must respect cookie consent, you cannot rely on a single YAML switch. You must either embed a third-party consent banner script in in_header or manually structure the tracking snippet to read a consent flag before invoking gtag('config').

Pitfalls we hit

  • Expecting analytics in default local renders. Running quarto preview or quarto render without specifying --profile live renders the base configuration. The banner will not appear. To test consent styling locally, run QUARTO_PROFILE=live quarto preview.
  • Assuming the loader is completely blocked. As noted in our observations, Quarto’s built-in express consent delays the config script but still loads the external gtag.js script tag on initial render. Verify whether your organisation’s interpretation of privacy guidelines allows this external script fetch.
  • Default banner mentions non-existent ad trackers. Leaving default Quarto banner copy intact can suggest that technical documentation shares data with advertising partners. Because Quarto’s options do not expose banner text configuration, addressing this requires a custom include or alternative consent integration.
  • Hiding Measurement IDs in CI secrets. Storing a public tracking ID in GitHub Secrets adds maintenance burden without reducing risk. Keep identifiers in repository variables or profile configuration files.
NoteUpdates
  • 2026-09-26: Removed the end-of-post checklist.
  • 2026-09-27: The gtag.js loader is now held back until consent too (a custom include instead of Quarto’s google-analytics: option), with the browser check that shows it.