1- import { BrowserRouter as Router , Routes , Route } from "react-router-dom" ;
1+ import { useEffect } from "react" ;
2+ import {
3+ BrowserRouter as Router ,
4+ Routes ,
5+ Route ,
6+ useLocation
7+ } from "react-router-dom" ;
28import Home from "@/pages/Home" ;
39import SelectInfo from "@/pages/SelectInfo" ;
410import Chat from "@/pages/Chat" ;
@@ -12,10 +18,40 @@ import MbtiTestIntro from "@/pages/MbtiTestIntro";
1218import MbtiTestQuestions from "@/pages/MbtiTestQuestions" ;
1319import MbtiTestResult from "@/pages/MbtiTestResult" ;
1420import CenteredLayout from "@/components/CenteredLayout" ;
21+ import { initGA , trackPageView } from "@/libs/analytics" ;
22+
23+ const PageTracker = ( ) => {
24+ const location = useLocation ( ) ;
25+
26+ const trackedPaths = [
27+ { path : "/" , label : "home" } ,
28+ { path : "/login" , label : "login" } ,
29+ { path : "/contents" }
30+ ] ;
31+
32+ useEffect ( ( ) => {
33+ const { pathname } = location ;
34+
35+ trackedPaths . forEach ( ( { path, label } ) => {
36+ if ( path === "/contents" && pathname . startsWith ( path ) ) {
37+ trackPageView ( label || pathname ) ;
38+ } else if ( pathname === path ) {
39+ trackPageView ( label || pathname ) ;
40+ }
41+ } ) ;
42+ } , [ location . pathname ] ) ;
43+
44+ return null ;
45+ } ;
1546
1647const App = ( ) => {
48+ useEffect ( ( ) => {
49+ initGA ( ) ;
50+ } , [ ] ) ;
51+
1752 return (
1853 < Router >
54+ < PageTracker />
1955 < CenteredLayout >
2056 < Routes >
2157 < Route path = "/" element = { < Home /> } />
0 commit comments