From a777303ca53b12682ad7b362324d3aa4e6573d26 Mon Sep 17 00:00:00 2001 From: NPJigaK Date: Thu, 26 Oct 2023 02:40:53 +0900 Subject: [PATCH] Using Google Tag Manager --- lib/gtm.ts | 17 ++++++++++++++ package.json | 1 - pages/_app.tsx | 54 +++++++++++++++++++-------------------------- pages/_document.tsx | 49 ++++++++-------------------------------- yarn.lock | 5 ----- 5 files changed, 49 insertions(+), 77 deletions(-) create mode 100644 lib/gtm.ts diff --git a/lib/gtm.ts b/lib/gtm.ts new file mode 100644 index 0000000..14695a3 --- /dev/null +++ b/lib/gtm.ts @@ -0,0 +1,17 @@ +import { debugLogger } from "./debugLogger"; + +declare global { + interface Window { + dataLayer: Record[]; + } +} + +export const GTM_ID = "GTM-NDPCVWBM"; + +export const pageview = (url: string): void => { + debugLogger(`Logging pageview for ${url}`); + window.dataLayer.push({ + event: "pageview", + page: url, + }); +}; diff --git a/package.json b/package.json index d331c6d..87a6521 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "nextra-theme-docs": "3.0.0-alpha.8", "react": "^18.2.0", "react-dom": "^18.2.0", - "react-ga4": "^2.1.0", "react-social-login-buttons": "3.9.1" }, "devDependencies": { diff --git a/pages/_app.tsx b/pages/_app.tsx index 69cbb85..4c1f261 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -3,45 +3,37 @@ import type { AppProps } from "next/app"; import type { ReactElement } from "react"; import { useEffect } from "react"; import { useRouter } from "next/router"; -import ReactGA from "react-ga4"; -import { debugLogger } from "@/lib/debugLogger"; - -export const logPageView = () => { - debugLogger(`Logging pageview for ${window.location.pathname}`); - ReactGA.set({ page: window.location.pathname }); - ReactGA.send({ hitType: "pageview", page: window.location.pathname }); -}; - -export const initGA = () => { - debugLogger("init GA"); - ReactGA.initialize("G-EPBVRTSKFD"); -}; +import Script from "next/script"; +import { GTM_ID, pageview } from "@/lib/gtm"; export default function App({ Component, pageProps }: AppProps): ReactElement { const router = useRouter(); - useEffect(() => { - if (window.location.hostname === "localhost") { - debugLogger("Running on localhost. Skip initGA."); - return; - } - initGA(); - // `routeChangeComplete` won't run for the first page load unless the query string is - // hydrated later on, so here we log a page view if this is the first render and - // there's no query string - if (!router.asPath.includes("?")) { - logPageView(); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); - useEffect(() => { // Listen for page changes after a navigation or when the query changes - router.events.on("routeChangeComplete", logPageView); + router.events.on("routeChangeComplete", pageview); return () => { - router.events.off("routeChangeComplete", logPageView); + router.events.off("routeChangeComplete", pageview); }; }, [router.events]); - return ; + return ( + <> + {/* Google Tag Manager - Global base code */} +