-
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.
- Loading branch information
Showing
6 changed files
with
94 additions
and
3 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
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,45 @@ | ||
import { useEffect } from "react"; | ||
import { useLocation } from "react-router-dom"; | ||
import ReactGA from "react-ga4"; | ||
import { Cookies } from "react-cookie-consent"; | ||
|
||
/** | ||
* Initializes Google Analytics | ||
* Should only be called once and called once the user has accepted the cookie policy | ||
*/ | ||
const initializeReactGA = () => { | ||
ReactGA.initialize("G-MWMM6L80KH"); | ||
ReactGA.set({ anonymizeIp: true }); | ||
}; | ||
|
||
/** | ||
* Handles the user accepting the cookie policy | ||
*/ | ||
const handleAcceptCookie = () => { | ||
initializeReactGA(); | ||
_logPageView(); | ||
}; | ||
|
||
/** | ||
* Logs a page view with the current URL | ||
*/ | ||
const _logPageView = () => { | ||
ReactGA.send({ | ||
hitType: "pageview", | ||
page: window.location.pathname + window.location.search, | ||
}); | ||
}; | ||
|
||
/** | ||
* Component to track page views if and only if the user has accepted the cookie policy. | ||
*/ | ||
function AnalyticsTracker() { | ||
const location = useLocation(); | ||
useEffect(() => { | ||
if (Cookies.get("CookieConsent")) { | ||
_logPageView(); | ||
} | ||
}, [location]); | ||
return null; | ||
} | ||
export { initializeReactGA, handleAcceptCookie, AnalyticsTracker }; |