From b519b49e0db3dd84d11ddd83f1c5ffd05b76eaf9 Mon Sep 17 00:00:00 2001 From: oskar Date: Wed, 24 Jun 2026 17:00:29 +0200 Subject: [PATCH] Add Umami analytics tag to on production builds Inject the Umami tracking script into index.html via a build-only Vite plugin (apply: 'build'), so it loads on production but never on the dev server. Website id comes from PUBLIC_UMAMI_WEBSITE_ID with a fallback to the gethumanai.pl stats id. The SPA serves PL and EN from the same index.html, so both language versions get the tag. Co-Authored-By: Claude Opus 4.8 (1M context) --- vite.config.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index 5a33944..b3e6267 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,7 +1,28 @@ -import { defineConfig } from 'vite' +import { defineConfig, loadEnv, type Plugin } from 'vite' import react from '@vitejs/plugin-react' +const UMAMI_SRC = 'https://stats.gethumanai.pl/script.js' +// Fallback website id, used when PUBLIC_UMAMI_WEBSITE_ID is not provided. +const UMAMI_FALLBACK_ID = 'f5a3fd87-5ae1-408d-85a6-7af588484d37' + +// Injects the Umami analytics tag into . `apply: 'build'` means it runs +// only for production builds (`vite build`) — never on the dev server. +function umami(websiteId: string): Plugin { + return { + name: 'inject-umami', + apply: 'build', + transformIndexHtml(html) { + const tag = `` + return html.replace('', ` ${tag}\n `) + }, + } +} + // https://vitejs.dev/config/ -export default defineConfig({ - plugins: [react()], +export default defineConfig(({ mode }) => { + const env = loadEnv(mode, process.cwd(), '') + const websiteId = env.PUBLIC_UMAMI_WEBSITE_ID || UMAMI_FALLBACK_ID + return { + plugins: [react(), umami(websiteId)], + } })