WT-1344 inject Plausible analytics for EU visitors#1502
WT-1344 inject Plausible analytics for EU visitors#1502stephendherrera wants to merge 4 commits into
Conversation
Load the Plausible script only for EU/consent countries, gated behind a `plausible` waffle switch and a configured PLAUSIBLE_DOMAIN. The gating rule lives in a single `plausible_enabled(country_code)` helper reused across the base templates. Adds the Plausible host to the script-src and connect-src CSP directives when configured, plus unit tests for the helper and the client script.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1502 +/- ##
==========================================
+ Coverage 79.38% 80.88% +1.50%
==========================================
Files 154 147 -7
Lines 10582 10145 -437
==========================================
- Hits 8400 8206 -194
+ Misses 2182 1939 -243 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| GTM_CONTAINER_ID: "GTM-MSTZDDXN" | ||
| LOG_LEVEL: INFO | ||
| LOCAL_DB_UPDATE: "True" | ||
| # PLAUSIBLE_DOMAIN: "firefox.com" |
There was a problem hiding this comment.
🚗 No a full review, just passing through, but why did you comment it out? If default is disabled, maybe consider the var being empty/null.
Configuration should never live in comments. It creates ambiguity, breaks tooling that audits env vars, and makes the default behavior opaque. The codebase already has a documented default (empty string).
There was a problem hiding this comment.
We can then also get rid of the noqa: F405.
|
Took this for a spin locally Verification
Asks before merge
Non-blocking, but worth resolving while it's cheap
Happy to take this as a follow-up if you'd rather keep PR scope tight, but it's cheap to do now and matches the upstream install pattern. |
One-line summary
Inject the Plausible analytics script for EU/consent-country visitors only, gated behind a
plausiblewaffle switch.Significant changes and points to review
plausible_enabled(country_code)helper (misc.py) is the single source of truth — it returns true only when theplausiblewaffle switch is on,PLAUSIBLE_DOMAINis configured, and the visitor is in aDATA_CONSENT_COUNTRIES(EU/EEA + CH + UK) country. Reused across all three base templates so non-EU visitors never receive the script or its data attributes.data-plausible-domain/data-plausible-srcat call time, bails on DNT/GPC, defines thewindow.plausiblequeue stub, then injects the deferred script — following Plausible's documented install.script-srcandconnect-srconly whenPLAUSIBLE_DOMAINis set. Note: assumes events post to the same host as the script (true for Plausible Cloud); a self-hosted instance ingesting on a separate host would need that added too.PLAUSIBLE_DOMAIN(default empty → disabled) andPLAUSIBLE_SCRIPT_URL(defaulthttps://plausible.io/js/script.js). Demo env var added but commented out so demos stay dark by default.Issue / Bugzilla link
WT-1344
Testing
pytestcases for the helper (EU on / non-EU off / switch off / no-domain off). Runnpm run jasmineandmake test.PLAUSIBLE_DOMAIN, create+enable theplausiblewaffle switch, then:?geo=DE→ confirm the Plausible<script>anddata-plausible-*attributes are present in the<head>.?geo=US→ confirm the script and attributes are absent.?geo=DEand DNT or GPC enabled in the browser → confirm the script does not load.