Skip to content

Setting VITE_DISABLE_ANALYTICS=true does not disable analytics #910

@rubenferreira97

Description

@rubenferreira97

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 dev

Then 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:

chartdb/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.env does not exist in the browser.
  • import.meta.env cannot be used inside plain HTML, only in JS/TS modules processed by Vite.
  • As a result, setting VITE_DISABLE_ANALYTICS in .env has no effect.
  • This is not a .env issue 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' };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions