diff --git a/app/[locale]/analytics.tsx b/app/[locale]/analytics.tsx new file mode 100644 index 000000000..5bf3619dd --- /dev/null +++ b/app/[locale]/analytics.tsx @@ -0,0 +1,47 @@ +'use client'; + +import { GTM_ID, pageview } from 'lib/gtm'; +import { usePathname, useSearchParams } from 'next/navigation'; +import Script from 'next/script'; +import { useEffect } from 'react'; + +export default function Analytics() { + const pathname = usePathname(); + const searchParams = useSearchParams(); + + useEffect(() => { + if (pathname) { + pageview(pathname); + } + }, [pathname, searchParams]); + + if (process.env.NEXT_PUBLIC_VERCEL_ENV !== 'production') { + return null; + } + + return ( + <> + + + > + ); +} diff --git a/app/[locale]/layout.tsx b/app/[locale]/layout.tsx index 75f03f241..13e6a7067 100644 --- a/app/[locale]/layout.tsx +++ b/app/[locale]/layout.tsx @@ -2,10 +2,10 @@ import { Lato, Noto_Serif_JP } from 'next/font/google'; import localFont from 'next/font/local'; import { ReactNode, Suspense } from 'react'; -import { GoogleProvider } from 'app/context/google-provider'; import { SupportedLocale } from 'components/layout/navbar/language-control'; import { NextIntlClientProvider } from 'next-intl'; import { notFound } from 'next/navigation'; +import Analytics from './analytics'; import './globals.css'; const { TWITTER_CREATOR, TWITTER_SITE, SITE_NAME } = process.env; @@ -88,8 +88,6 @@ export default async function RootLayout({ notFound(); } - const shouldTrack = process.env.NODE_ENV === 'production'; - return (