-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
6f0483b
commit 7b87d67
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
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,43 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
import ReactGA from 'react-ga4'; | ||
|
||
/** | ||
* 구글 애널리틱스 사용하기 위함. | ||
* uri 변경 추적 컴포넌트 | ||
* uri가 변경될 때마다 pageview 이벤트 전송 | ||
*/ | ||
const RouteChangeTracker: React.FC = () => { | ||
const location = useLocation(); | ||
const [initialized, setInitialized] = useState(false); | ||
|
||
// localhost는 기록하지 않음 | ||
// useEffect(() => { | ||
// if (!window.location.href.includes('localhost')) { | ||
// process.env.REACT_APP_GOOGLE_ANALYTICS_TRAKING_ID && | ||
// ReactGA.initialize(process.env.REACT_APP_GOOGLE_ANALYTICS_TRAKING_ID); | ||
// setInitialized(true); | ||
// } | ||
// }, []); | ||
|
||
// location 변경 감지시 pageview 이벤트 전송 | ||
useEffect(() => { | ||
if (initialized) { | ||
ReactGA.set({ page: location.pathname }); | ||
ReactGA.send('pageview'); | ||
} | ||
}, [initialized, location]); | ||
|
||
// 개발용 | ||
useEffect(() => { | ||
process.env.REACT_APP_GOOGLE_ANALYTICS_TRAKING_ID && | ||
ReactGA.initialize(process.env.REACT_APP_GOOGLE_ANALYTICS_TRAKING_ID); | ||
ReactGA.set({ page: location.pathname }); | ||
ReactGA.send('pageview'); | ||
}, [location]); | ||
|
||
// JSX에서 사용하기 위해 null 반환 | ||
return null; | ||
}; | ||
|
||
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