Skip to content

feat(GA4): Sunset GA4 in place of PA. - #519

Merged
stephaniehobson merged 2 commits into
mainfrom
swap-ga4-for-plausible
Nov 24, 2026
Merged

stephaniehobson merged 2 commits into
mainfrom
swap-ga4-for-plausible

Conversation

@stephendherrera

@stephendherrera stephendherrera commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Swap Google Analytics for Plausible

Replace react-ga4 and the withTracker decorator with Plausible's per-site script, loaded from a first-party subdomain by the new initPlausible() in src/lib/measure.js. The script counts a page view on load and on every history.pushState, so the router needs no tracking code.

The module is named measure.js because ad blockers block files named plausible.js and analytics.js. index.jsx imports it dynamically after rendering, so if a blocker still catches it, the site loads and only analytics are lost.

initPlausible() loads nothing when the visitor has Global Privacy Control or Do Not Track on (checked on navigator and window separately), when the script URL is empty, or when it has already run. It only loads a per-site script on pa.firefox.com (https://pa.firefox.com/js/pa-.js), so a wrong build setting can't run another origin's code on the site. Any other URL logs a warning and loads nothing, instead of throwing out of index.jsx. Events always go to https://pa.firefox.com/api/event.

The script URL is not in .env. deploy.yml builds with VITE_PLAUSIBLE_SCRIPT_URL taken from the PLAUSIBLE_SCRIPT_URL variable of the stage or prod GitHub environment, so each environment reports to its own Plausible site, and local runs and CI load no analytics. The deploy fails if that variable is unset or doesn't match the pattern, or if the URL doesn't end up in the bundle, so a site can't ship with analytics silently off. After the upload, the site check confirms the script is served as JavaScript. Nothing automated confirms that page views are recorded; after changing the variable, visit stage and look for the visit in its Plausible site.

Tests: measure.test.jsx covers the privacy signals, the single load, the endpoint, and rejected URLs (another origin, a lookalike host, plain http, a non-per-site script, a query string). measure.spec.js checks that the script is requested, that GPC and DNT stop it, and that the site still loads when the module is blocked. The live-data Playwright project signals GPC so the post-deploy smoke test does not count as visitors. The old pageview tests in Routes.test.jsx are removed along with withTracker.

Running Playwright locally against an npm start you already have open fails measure.spec.js, because that server has no script URL. CONTRIBUTING.md explains how to start one with it.

Docs updated to match, including a note that a future CSP must allow pa.firefox.com.

This comment was marked as resolved.

Ad blockers block files named plausible.js and analytics.js. In
development Vite serves each module under its own name, and index.jsx
imported the module statically, so a blocked request stopped the whole
app from loading. The module is now lib/measure.js, and index.jsx
imports it dynamically after rendering, so if it is still blocked the
only loss is analytics. measure.spec.js blocks the module and checks
that the site still loads.

The app loaded any absolute script URL it was built with, so a wrong
PLAUSIBLE_SCRIPT_URL setting could run another origin's code on the
site. It now accepts only https://pa.firefox.com/js/pa-<id>.js, and
always sends events to pa.firefox.com.

An unset or invalid variable used to deploy a site with analytics
silently off. The build step now fails unless the variable matches
that pattern and ends up in the bundle, and the post-deploy check
confirms the script is served as JavaScript.

Also:
- Check typeof window.plausible === 'function', so a DOM element with
  id="plausible" can't silently disable analytics.
- List initPlausible among the repository's named exports.
- Note that measure.spec.js fails against a reused plain npm start.
- Trim comments down to the non-obvious constraints.

Copilot AI 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.

🔵 Needs a closer look

Human verification is needed to confirm stage/prod Plausible settings and initial and SPA-navigation pageview delivery, since recorded tests stub the tracker and deployed smoke tests disable it.

1 open issue

Review effort: Balanced

@stephaniehobson stephaniehobson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

R+

@stephaniehobson
stephaniehobson merged commit bf8ad2e into main Nov 24, 2026
8 checks passed
@stephaniehobson
stephaniehobson deleted the swap-ga4-for-plausible branch November 24, 2026 20:50
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.

3 participants