Google Analytics 4 for Next.js
npm install next-ga4
or
yarn add next-ga4
Add <NextGA />
to _app.tsx
.
import type { AppProps } from 'next/app'
import { NextGA } from 'next-ga4'
const App = ({ Component, pageProps }: AppProps) => {
return (
<>
<NextGA measurementId="G-XXXXXXXXXX" />
<Component {...pageProps} />
</>
)
}
export default App
Property | Description | Type | Required | Default |
---|---|---|---|---|
measurementId |
Measurement ID. | string |
Yes | |
initialConsentParams |
Parameters for consent on initial execute. | object |
No | undefined |
initialSetParams |
Parameters for set on initial execute. | object |
No | undefined |
initialConfigParams |
Parameters for config on initial execute. | object |
No | undefined |
isListenRouterEvents |
Whether to detect pushState , popState , and replaceState and send page_view If true , disable Page changes based on browser history events in Enhanced measurement . |
boolean |
No | true |
- Add Google Analytics Debugger to Google Chrome and enable it
- Click Configure -> DebugView in the left navigation
- Reload the website you want to debug
More details: [GA4] Monitor events in debug mode
MIT