-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
client/src/components/_common/routeChangeTracker/RouteChangeTracker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* src/RouteChangeTracker.js */ | ||
import { PropsWithChildren, useEffect, useState } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
import ReactGA from 'react-ga4'; | ||
|
||
const RouteChangeTracker = ({ children }: PropsWithChildren) => { | ||
const location = useLocation(); | ||
const [initialized, setInitialized] = useState(false); | ||
|
||
// 구글 애널리틱스 운영서버만 적용 | ||
useEffect(() => { | ||
if (process.env.GOOGLE_ANALYTICS) { | ||
ReactGA.initialize(process.env.GOOGLE_ANALYTICS); | ||
setInitialized(true); | ||
} | ||
}, []); | ||
|
||
// location 변경 감지시 pageview 이벤트 전송 | ||
useEffect(() => { | ||
if (initialized) { | ||
ReactGA.set({ page: location.pathname }); | ||
ReactGA.send('pageview'); | ||
} | ||
}, [initialized, location]); | ||
|
||
return <>{children}</>; | ||
}; | ||
|
||
export default RouteChangeTracker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters