From 212ba239dd8ae4935751bce0ed7d35a8f5561eac Mon Sep 17 00:00:00 2001 From: zainabfatima097 Date: Wed, 20 May 2026 09:32:20 +0530 Subject: [PATCH] setup google analytics for the project --- .gitignore | 6 ++++++ package-lock.json | 9 ++++++++- package.json | 3 ++- src/main.jsx | 14 +++++++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index bf5e8bc..6222fbe 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,9 @@ lerna-debug.log* *.sln *.sw? .DS_Store + +.env +.env.local +.DS_Store +node_modules/ +dist/ \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index a51bf8b..dfbccca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "0.1.0", "dependencies": { "react": "^18.2.0", - "react-dom": "^18.2.0" + "react-dom": "^18.2.0", + "react-ga4": "^3.0.1" }, "devDependencies": { "@types/react": "^18.2.0", @@ -4162,6 +4163,12 @@ "react": "^18.3.1" } }, + "node_modules/react-ga4": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/react-ga4/-/react-ga4-3.0.1.tgz", + "integrity": "sha512-GyCc01bSheWXjzGDyHsXMOqk/SP5Cf/JrcJTg4hcpKx4eeSwaJKpJUc+ipF4ffLTZkmabmf3ZGBv4OKHTXNXyA==", + "license": "MIT" + }, "node_modules/react-is": { "version": "16.13.1", "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", diff --git a/package.json b/package.json index efdbbee..05517d0 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ }, "dependencies": { "react": "^18.2.0", - "react-dom": "^18.2.0" + "react-dom": "^18.2.0", + "react-ga4": "^3.0.1" }, "devDependencies": { "@types/react": "^18.2.0", diff --git a/src/main.jsx b/src/main.jsx index 3885300..259f327 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -1,9 +1,21 @@ import React from 'react' import ReactDOM from 'react-dom/client' import App from './App' +import ReactGA from 'react-ga4' + +// Initialize Google Analytics +const GA_MEASUREMENT_ID = import.meta.env.VITE_GA_MEASUREMENT_ID + +if (GA_MEASUREMENT_ID) { + ReactGA.initialize(GA_MEASUREMENT_ID) + ReactGA.send('pageview') + console.log('✅ Google Analytics initialized with ID:', GA_MEASUREMENT_ID) +} else { + console.log('⚠️ Google Analytics not initialized — VITE_GA_MEASUREMENT_ID not found in .env') +} ReactDOM.createRoot(document.getElementById('root')).render( -) +) \ No newline at end of file