Skip to content

Commit

Permalink
Added handling for vite.config for non defined Google Analytics ID
Browse files Browse the repository at this point in the history
  • Loading branch information
JBergVincit committed Jul 15, 2024
1 parent ac02431 commit a5475d4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions frontend/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const App = () => {
if (GETAROOM_ENV().VITE_CLARITY_ID != null) {
// Start seeing data on the Clarity dashboard with your id
clarity.init(GETAROOM_ENV().VITE_CLARITY_ID as string);

clarity.consent();

// Check if Clarity has been initialized before calling its methods
if (clarity.hasStarted()) {
clarity.identify('USER_ID', { userProperty: 'value' });
Expand Down
30 changes: 22 additions & 8 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ import { VitePluginRadar } from 'vite-plugin-radar';
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');

return {
const googleAnalyticsId = env.VITE_GOOGLE_ANALYTICS_ID;

const googleAnalyticsPlugin =
googleAnalyticsId !== null && googleAnalyticsId !== ''
? VitePluginRadar({
enableDev: true,
// Google Analytics tag injection
analytics: {
id: env.VITE_GOOGLE_ANALYTICS_ID as string
}
})
: null;

let config = {
// depending on your application, base can also be "/"
base: './',
plugins: [
Expand All @@ -23,13 +36,6 @@ export default defineConfig(({ mode }) => {
svgr({
svgrOptions: { exportType: 'named' },
include: '/**/*.svg'
}),
VitePluginRadar({
enableDev: true,
// Google Analytics tag injection
analytics: {
id: env.VITE_GOOGLE_ANALYTICS_ID as string
}
})
],
optimizeDeps: {
Expand Down Expand Up @@ -57,4 +63,12 @@ export default defineConfig(({ mode }) => {
}
}
};

if (googleAnalyticsPlugin) {
config.plugins.push(googleAnalyticsPlugin);
} else {
console.log('Google Analytics ID not defined!');
}

return config;
});

0 comments on commit a5475d4

Please sign in to comment.