Skip to content

Commit

Permalink
feat: added Google Analytics basic page tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
raczmirko committed Aug 1, 2024
1 parent edf46bd commit 5314602
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"i18next-http-backend": "^2.5.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-ga4": "^2.1.0",
"react-i18next": "^15.0.0",
"react-router-dom": "^6.24.0",
"react-scripts": "^5.0.1",
Expand Down
29 changes: 21 additions & 8 deletions src/app/App.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import { Box } from '@mui/material';
import CssBaseline from '@mui/material/CssBaseline';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import React from 'react';
import { Route, Routes } from 'react-router-dom';
import ReactGA from 'react-ga4';
import { Route, Routes, useLocation } from 'react-router-dom';
import '../assets/App.css';
import Sidebar from '../components/common/Sidebar';
import Home from './pages/Home';
import Contact from './pages/Contact';
import Academic from './pages/Academic';
import Skills from './pages/Skills';
import About from './pages/About';
import Resources from './pages/Resources';
import Academic from './pages/Academic';
import Contact from './pages/Contact';
import Home from './pages/Home';
import Projects from './pages/Projects';
import Resources from './pages/Resources';
import Skills from './pages/Skills';
import { useEffect } from 'react';

const usePageTracking = () => {
const location = useLocation();

useEffect(() => {
ReactGA.send({ hitType: 'pageview', page: location.pathname + location.search });
}, [location]);
};


const App = () => {
usePageTracking();

function App() {
return (
<ThemeProvider theme={createTheme({palette: {mode: 'dark'}})}>
<CssBaseline/>
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { HashRouter } from 'react-router-dom';
import App from './app/App';
import NotificationProvider from '../src/components/common/NotificationProvider';
import i18n from './i18n';
import initializeAnalytics from './utils/analytics';

initializeAnalytics();

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
Expand Down
7 changes: 7 additions & 0 deletions src/utils/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ReactGA from "react-ga4";

const initializeAnalytics = () => {
ReactGA.initialize("G-MDJYMJ41D2");
};

export default initializeAnalytics;

0 comments on commit 5314602

Please sign in to comment.