-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: modify GA script && preconnect assets
- Loading branch information
Showing
6 changed files
with
49 additions
and
71 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,19 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useEffect } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
import ReactGA from 'react-ga4'; | ||
|
||
const useTrackRoute = () => { | ||
const location = useLocation(); | ||
const [initialized, setInitialized] = useState(false); | ||
const { NODE_ENV } = import.meta.env; | ||
const isPro = NODE_ENV === 'production'; | ||
|
||
useEffect(() => { | ||
if (import.meta.env.VITE_GA) { | ||
ReactGA.initialize(import.meta.env.VITE_GA); | ||
setInitialized(true); | ||
} | ||
}, []); | ||
|
||
// location 변경 감지시 pageview 이벤트 전송 | ||
useEffect(() => { | ||
if (initialized) { | ||
ReactGA.set({ page: location.pathname }); | ||
ReactGA.send('pageview'); | ||
} | ||
}, [initialized, location]); | ||
if (!isPro) return; | ||
window.gtag('event', 'pageview', { | ||
page_path: location.pathname + location.search + location.hash, | ||
page_search: location.search, | ||
page_hash: location.hash | ||
}); | ||
}, [location]); | ||
}; | ||
|
||
export default useTrackRoute; |
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