From 7a44c39cef9c56ddb0df6f3b20172bf2098e8ba9 Mon Sep 17 00:00:00 2001 From: seongminn Date: Wed, 29 Nov 2023 18:42:53 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20google=20analytics=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/layout.tsx | 36 ++++++++++++++++++++++++++++++++++++ src/types/ga/gtag.ts | 11 +++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/types/ga/gtag.ts diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 0c906b6..beecd8d 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -3,9 +3,12 @@ import './globals.css'; import { Analytics } from '@vercel/analytics/react'; import type { Metadata } from 'next'; import { Noto_Sans_KR } from 'next/font/google'; +import { useRouter } from 'next/router'; +import { useEffect } from 'react'; import Footer from '@/components/layout/Footer'; import Header from '@/components/layout/Header'; +import * as ga from '@/types/ga/gtag'; import ReactQueryProvider from './ReactQueryProvider'; @@ -27,8 +30,41 @@ export default function RootLayout({ }: { children: React.ReactNode; }) { + const router = useRouter(); + + useEffect(() => { + const handleRouteChange = (url: URL) => { + ga.pageView(url); + }; + + router.events.on('routeChangeComplete', handleRouteChange); + + return () => { + router.events.off('routeChangeComplete', handleRouteChange); + }; + }, [router.events]); + return ( + +