fix(analytics): make product analytics opt-in and host-scoped

The OpenPanel endpoint and client id were hardcoded in dashboard/index.html, so
every clone, fork and self-hosted deployment inherited them and reported into
the hosted instance's dashboard. src/lib/analytics.js already documents the
intended behaviour ('analytics can never leak into the open-source experience');
this makes that true.

- apiUrl and clientId now come from VITE_OPENPANEL_API_URL /
  VITE_OPENPANEL_CLIENT_ID, substituted by Vite at build time and unset by
  default, so npm run dev and a plain npm run build initialise nothing.
- The third-party script is only fetched once analytics is actually enabled: a
  default build makes no external analytics request at all.
- Reporting is additionally gated on a hostname allowlist, so a dist/ built with
  credentials stays inert if it is served anywhere else.

Verified with a headless browser against real production builds:
  credentials + non-allowlisted host -> no requests
  credentials + allowlisted host     -> initialises and reports
  no credentials + allowlisted host  -> no requests

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
victorcavero14 2026-07-28 09:56:49 +02:00
parent 8f873296a1
commit 4fcc1cd5ed
4 changed files with 51 additions and 10 deletions

View file

@ -41,3 +41,10 @@ AWS_S3_PUBLIC_BUCKET=your-public-bucket-name
# Video encoder for all ffmpeg encodes: x264 (default) | nvenc | auto. # Video encoder for all ffmpeg encodes: x264 (default) | nvenc | auto.
# auto probes h264_nvenc once at startup and falls back to x264 if unusable. # auto probes h264_nvenc once at startup and falls back to x264 if unusable.
# FFMPEG_ENCODER=x264 # FFMPEG_ENCODER=x264
# Product analytics (optional, off by default). Both are read by the dashboard
# at BUILD time. Leave them unset and the frontend initialises no analytics and
# loads no third-party script. Set them to your own OpenPanel instance — and add
# your hostname to ANALYTICS_HOSTS in dashboard/index.html — to collect your own.
# VITE_OPENPANEL_API_URL=https://api.openpanel.example.com
# VITE_OPENPANEL_CLIENT_ID=your_openpanel_client_id

View file

@ -112,6 +112,7 @@ Async job queue with semaphore-based concurrency control. Configure via `MAX_CON
- `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_REGION`, `AWS_S3_BUCKET` - For S3 backup - `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_REGION`, `AWS_S3_BUCKET` - For S3 backup
- `MAX_CONCURRENT_JOBS` - Concurrent processing limit (default: 5) - `MAX_CONCURRENT_JOBS` - Concurrent processing limit (default: 5)
- `VITE_API_URL` - Production API URL override - `VITE_API_URL` - Production API URL override
- `VITE_OPENPANEL_API_URL`, `VITE_OPENPANEL_CLIENT_ID` - Optional product analytics, read at **build** time. Unset (the default, including every self-hosted build) means no analytics is initialised and no third-party script is loaded. `dashboard/index.html` also gates reporting on an `ANALYTICS_HOSTS` allowlist, so a build carrying credentials stays inert on any other host.
**Client-side (localStorage, encrypted):** **Client-side (localStorage, encrypted):**
- `GEMINI_API_KEY` - Google Gemini API key (required) - `GEMINI_API_KEY` - Google Gemini API key (required)

View file

@ -21,6 +21,12 @@ COPY . .
# (Coolify: add it as a build variable on the frontend service). Empty = same-origin. # (Coolify: add it as a build variable on the frontend service). Empty = same-origin.
ARG VITE_API_URL="" ARG VITE_API_URL=""
ENV VITE_API_URL=$VITE_API_URL ENV VITE_API_URL=$VITE_API_URL
# Optional product analytics (OpenPanel). Left empty, index.html initialises
# nothing and the build phones home to nobody — see the comment there.
ARG VITE_OPENPANEL_API_URL=""
ENV VITE_OPENPANEL_API_URL=$VITE_OPENPANEL_API_URL
ARG VITE_OPENPANEL_CLIENT_ID=""
ENV VITE_OPENPANEL_CLIENT_ID=$VITE_OPENPANEL_CLIENT_ID
RUN npm run build RUN npm run build
# ---- static serving (nginx) ---- # ---- static serving (nginx) ----

View file

@ -250,19 +250,46 @@
} }
</script> </script>
<!-- Analytics --> <!-- Analytics — opt-in, disabled by default.
<!-- OpenPanel analytics (self-hosted) --> A plain `npm run build` (and every local `npm run dev`) leaves the two
VITE_OPENPANEL_* variables unset, so nothing is initialised, no third
party script is fetched and no request leaves the browser. Self-hosted
instances are silent unless you deliberately turn this on.
To collect analytics for your own deployment: point
VITE_OPENPANEL_API_URL / VITE_OPENPANEL_CLIENT_ID at your own OpenPanel
instance (see .env.example) and add your hostname to ANALYTICS_HOSTS. -->
<script> <script>
window.op = window.op || function () { var n = []; return new Proxy(function () { arguments.length && n.push([].slice.call(arguments)); }, { get: function (t, r) { return "q" === r ? n : function () { n.push([r].concat([].slice.call(arguments))); }; }, has: function (t, r) { return "q" === r; } }); }(); window.op = window.op || function () { var n = []; return new Proxy(function () { arguments.length && n.push([].slice.call(arguments)); }, { get: function (t, r) { return "q" === r ? n : function () { n.push([r].concat([].slice.call(arguments))); }; }, has: function (t, r) { return "q" === r; } }); }();
window.op("init", { (function () {
apiUrl: "https://api.openpanel.fotoexamen.com", // Vite substitutes these at build time and leaves the literal
clientId: "9b7ab939-bdef-4097-87c4-bb4a2dbe034d", // "%VITE_...%" placeholder in place when the variable is undefined.
trackScreenViews: true, var apiUrl = "%VITE_OPENPANEL_API_URL%";
trackOutgoingLinks: true, var clientId = "%VITE_OPENPANEL_CLIENT_ID%";
trackAttributes: true, var unset = function (v) { return !v || v.charAt(0) === "%"; };
}); if (unset(apiUrl) || unset(clientId)) return;
// Second gate: hosts allowed to report. A dist/ built with credentials
// stays inert anywhere else (a fork, a mirror, a preview domain), so a
// deployment can never report into somebody else's dashboard.
var ANALYTICS_HOSTS = /^(www\.)?openshorts\.app$/;
if (!ANALYTICS_HOSTS.test(location.hostname)) return;
window.op("init", {
apiUrl: apiUrl,
clientId: clientId,
trackScreenViews: true,
trackOutgoingLinks: true,
trackAttributes: true,
});
var s = document.createElement("script");
s.src = "https://openpanel.dev/op1.js";
s.defer = true;
s.async = true;
document.head.appendChild(s);
})();
</script> </script>
<script src="https://openpanel.dev/op1.js" defer async></script>
</head> </head>
<body> <body>