Activating debug mode for GA4 tracking in Next.js 14 application

I’m working with a Next.js 14.2 project and I’ve integrated Google Analytics 4 using the GoogleAnalytics component from the @next/third-parties/google package. The problem I’m facing is that the standard <GoogleAnalytics gaId="G-MY-TRACKING-ID" /> component doesn’t seem to have any built-in way to activate debug mode for development purposes.

I need to enable the DebugView feature so I can properly test and monitor events during development, but I can’t find any debug parameter or configuration option. Does anyone know how to turn on debug mode with this particular setup?

Here’s my current implementation in the root layout:

import { GoogleAnalytics } from '@next/third-parties/google'
 
export default function MainLayout({ children }) {
  return (
    <html lang="en">
      <body>
        {children}
        <GoogleAnalytics gaId="G-ABC123" />
      </body>
    </html>
  )
}

Any suggestions on how to enable debugging with this configuration would be really helpful.

Hit this same problem last month on a client project. The @next/third-parties/google component just doesn’t cut it for dev work. Here’s what actually worked: ditch the GoogleAnalytics component in development and use a custom Script component instead. Check if you’re in dev mode with process.env.NODE_ENV === 'development' and render a Script that loads gtag with debug_mode: true and send_page_view: false. You’ll get full control over debug settings and it plays nice with GA4’s DebugView. Switch back to the standard GoogleAnalytics component for production. I’ve used this setup on several Next.js 14 projects without issues.

Had this exact issue recently. Found a cleaner fix that doesn’t require scrapping the whole component. Keep your GoogleAnalytics component and just add a separate Script tag to modify the gtag config after it loads. Drop this right after your GoogleAnalytics component: <Script id="ga-debug" strategy="afterInteractive">{window.gtag && gtag(‘config’, ‘${gaId}’, {debug_mode: true, send_page_view: false})}</Script>. This overrides the config for debug mode while keeping all the convenience of the third-parties package. Wrap it in a dev check so it only runs locally. DebugView should start showing events right away.

u can enable debug mode by manually adding debug_mode: true to your gtag config. Just use the Script component to inject gtag with those debug params. for example: gtag('config', 'G-ABC123', { debug_mode: true }) for dev. hope this helps!