-
Notifications
You must be signed in to change notification settings - Fork 6
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
4 changed files
with
40 additions
and
0 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
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,30 @@ | ||
import { useEffect, useState } from 'react'; | ||
import ReactGA from 'react-ga4'; | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
/** | ||
* uri 변경 추적 컴포넌트 | ||
* uri가 변경될 때마다 pageview 이벤트 전송 | ||
*/ | ||
const RouteChangeTracker = () => { | ||
const location = useLocation(); | ||
const [initialized, setInitialized] = useState(false); | ||
|
||
// 구글 애널리틱스 운영서버만 적용 | ||
useEffect(() => { | ||
if (import.meta.env.REACT_APP_GOOGLE_ANALYTICS) { | ||
ReactGA.initialize(import.meta.env.REACT_APP_GOOGLE_ANALYTICS); | ||
setInitialized(true); | ||
} | ||
}, []); | ||
|
||
// location 변경 감지시 pageview 이벤트 전송 | ||
useEffect(() => { | ||
if (initialized) { | ||
ReactGA.set({ page: location.pathname }); | ||
ReactGA.send('pageview'); | ||
} | ||
}, [initialized, location]); | ||
}; | ||
|
||
export default RouteChangeTracker; |