-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
PR #750 introduced the ability to opt out of Fathom analytics via the DISABLE_ANALYTICS flag. Unfortunately, while this PR provides a mechanism to disable analytics, it does not fully work in practice because the logic is applied only in a browser context that can't access it.
I run the project using:
npm install
npm run devThen I set the following in .env (or by passing it explicitly as an env variable):
VITE_DISABLE_ANALYTICS=true
However, analytics is still loaded.
The logic for disabling analytics is currently inside the index.html:
Lines 19 to 35 in 2bd9ca2
| (function () { | |
| const disableAnalytics = | |
| (window.env && window.env.DISABLE_ANALYTICS === 'true') || | |
| (typeof process !== 'undefined' && | |
| process.env && | |
| process.env.VITE_DISABLE_ANALYTICS === 'true'); | |
| if (!disableAnalytics) { | |
| const script = document.createElement('script'); | |
| script.src = 'https://cdn.usefathom.com/script.js'; | |
| script.setAttribute('data-site', 'PRHIVBNN'); | |
| script.setAttribute('data-canonical', 'false'); | |
| script.setAttribute('data-spa', 'auto'); | |
| script.defer = true; | |
| document.head.appendChild(script); | |
| } | |
| })(); |
Problem:
process.envdoes not exist in the browser.import.meta.envcannot be used inside plain HTML, only in JS/TS modules processed by Vite.- As a result, setting
VITE_DISABLE_ANALYTICSin.envhas no effect. - This is not a
.envissue since it’s caused by the logic running in the browser context.
Expected behavior:
Setting VITE_DISABLE_ANALYTICS=true in .env should disable analytics when running npm run dev (and in production builds).
Workaround:
I can disable analytics manually by changing config.js with:
window.env = { DISABLE_ANALYTICS: 'true' };