diff --git a/src/App.tsx b/src/App.tsx index 82a83ea..6bb2ad6 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,12 +1,23 @@ -import { BrowserRouter } from "react-router-dom"; +import { useEffect } from "react"; +import { BrowserRouter, useLocation } from "react-router-dom"; import { MainLayout } from "./layout/main.layout"; import { AppRoutes } from "./routes"; +import { trackPageView } from "./services/analytics/initAnalytics"; import "./App.css"; +function RouteTracker() { + const { pathname } = useLocation(); + useEffect(() => { + trackPageView(pathname); + }, [pathname]); + return null; +} + function App() { return ( + diff --git a/src/services/analytics/initAnalytics.ts b/src/services/analytics/initAnalytics.ts index c74038f..e3dfde7 100644 --- a/src/services/analytics/initAnalytics.ts +++ b/src/services/analytics/initAnalytics.ts @@ -50,6 +50,15 @@ async function startAnalytics(): Promise { } } +export function trackPageView(path: string): void { + if (typeof window.gtag === "function") { + window.gtag("event", "page_view", { + page_path: path, + page_location: window.location.href, + }); + } +} + export function initAnalytics(): void { if (!import.meta.env.PROD) { return; diff --git a/src/test-setup.ts b/src/test-setup.ts index d0de870..bc445ad 100644 --- a/src/test-setup.ts +++ b/src/test-setup.ts @@ -1 +1,26 @@ import "@testing-library/jest-dom"; + +const localStorageMock = (() => { + let store: Record = {}; + return { + getItem: (key: string) => store[key] ?? null, + setItem: (key: string, value: string) => { + store[key] = value; + }, + removeItem: (key: string) => { + delete store[key]; + }, + clear: () => { + store = {}; + }, + get length() { + return Object.keys(store).length; + }, + key: (index: number) => Object.keys(store)[index] ?? null, + }; +})(); + +Object.defineProperty(window, "localStorage", { + value: localStorageMock, + writable: true, +});