From ccdd732bbd2bb7b9310e44bcb57c49052103e6d5 Mon Sep 17 00:00:00 2001 From: Adam Gross <634414+adamgross42@users.noreply.github.com> Date: Tue, 30 Dec 2025 15:58:55 -0800 Subject: [PATCH] Build configuration option to remove Google Analytics (#236) --- README.md | 18 ++++++++++++++++++ index.html | 3 ++- src/util/analytics_noop.ts | 2 ++ vite.config.mts | 26 +++++++++++++++++++++++++- 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/util/analytics_noop.ts diff --git a/README.md b/README.md index 5fc1b40..2ff4868 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,24 @@ set VITE_STATIC_URL=https://example.org/sample.ged && npm run build The `dist/` folder will contain files that can be hosted on a Web server. +### Build without Google Analytics + +Set `VITE_GOOGLE_ANALYTICS=false` to exclude Google Analytics from the build output. This will remove the external JavaScript dependency. + +```shell +VITE_GOOGLE_ANALYTICS=false npm run build +``` + +
+For Windows CMD: + +```cmd +set VITE_GOOGLE_ANALYTICS=false && npm run build +``` +
+ +This may be combined with the other build environment variables described above. + ### Alternative build The [topola-webpack](https://github.com/develancer/topola-webpack) tool can build a Topola Genealogy Viewer package bundled together with a GEDCOM file. diff --git a/index.html b/index.html index dad208d..05cfe90 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@ - + + diff --git a/src/util/analytics_noop.ts b/src/util/analytics_noop.ts new file mode 100644 index 0000000..7e24950 --- /dev/null +++ b/src/util/analytics_noop.ts @@ -0,0 +1,2 @@ +/** No-op function for analytics. */ +export function analyticsEvent(action: string, data?: any) { } diff --git a/vite.config.mts b/vite.config.mts index fe544f1..df9f2cd 100644 --- a/vite.config.mts +++ b/vite.config.mts @@ -1,11 +1,35 @@ import {defineConfig} from 'vite'; +import {resolve} from 'path'; import react from '@vitejs/plugin-react'; import viteTsconfigPaths from 'vite-tsconfig-paths'; export default defineConfig({ // depending on your application, base can also be "/" base: '', - plugins: [react(), viteTsconfigPaths()], + plugins: [ + react(), + viteTsconfigPaths(), + { + name: 'transform-index-plugin', + transformIndexHtml(html: string) { + // Remove Google Analytics code if VITE_GOOGLE_ANALYTICS is set to 'false' + if (process.env.VITE_GOOGLE_ANALYTICS?.trim() === 'false') { + return html.replace(/[\s\S]*?/, ''); + } + }, + }, + ], + resolve: { + alias: [ + { + // Remove Google Analytics code if VITE_GOOGLE_ANALYTICS is set to 'false' + // Handles both formats of import statements used in this project + find: /\.?\.\/util\/analytics/, replacement: process.env.VITE_GOOGLE_ANALYTICS?.trim() === 'false' + ? resolve(__dirname, 'src/util/analytics_noop.ts') + : resolve(__dirname, 'src/util/analytics.ts') + }, + ], + }, server: { // this ensures that the browser opens upon server start open: true,