Repository navigation
feat(GA4): Sunset GA4 in place of PA. - #519
Merged
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
🔵 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
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.

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 startyou 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.