From 9bc8941604b93dcb797049ec111776366d69748e Mon Sep 17 00:00:00 2001 From: "plebeius.eth" <117766773+plebeius-eth@users.noreply.github.com> Date: Sat, 23 Sep 2023 20:30:14 +0200 Subject: [PATCH] Create useGoogleAnalytics.js --- src/hooks/useGoogleAnalytics.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/hooks/useGoogleAnalytics.js diff --git a/src/hooks/useGoogleAnalytics.js b/src/hooks/useGoogleAnalytics.js new file mode 100644 index 00000000..83677038 --- /dev/null +++ b/src/hooks/useGoogleAnalytics.js @@ -0,0 +1,19 @@ +import { useEffect } from 'react'; +import { useLocation } from 'react-router-dom'; + +const GA_MEASUREMENT_ID = "G-2M2VYEFL7B"; + +const useGoogleAnalytics = () => { + const location = useLocation(); + + useEffect(() => { + if (typeof window.gtag === 'function') { + const fullPath = location.pathname + location.search + location.hash; + window.gtag('config', GA_MEASUREMENT_ID, { + 'page_path': fullPath, + }); + } + }, [location]); +}; + +export default useGoogleAnalytics;