Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
107 lines
3.7 KiB
Plaintext
107 lines
3.7 KiB
Plaintext
---
|
|
title: Using Google Analytics with Next.js (through `@next/third-parties/google`)
|
|
---
|
|
|
|
> Prefer `@next/third-parties/google` when using the inline script for Google Analytics and Tag Manager.
|
|
|
|
## Why This Error Occurred
|
|
|
|
An inline script was used for Google Analytics which might impact your webpage's performance. Instead, we recommend using `next/script` through the `@next/third-parties` library.
|
|
|
|
## Possible Ways to Fix It
|
|
|
|
### Use `@next/third-parties` to add Google Analytics
|
|
|
|
**`@next/third-parties`** is a library that provides a collection of components and utilities that improve the performance and developer experience of loading popular third-party libraries in your Next.js application. It is available with Next.js 14 (install `next@latest`).
|
|
|
|
The `GoogleAnalytics` component can be used to include [Google Analytics
|
|
4](https://developers.google.com/analytics/devguides/collection/ga4) to your page via the Google tag (`gtag.js`). By default, it fetches the original scripts after hydration occurs on the page.
|
|
|
|
> **Recommendation**: If Google Tag Manager is already included in your application, you can
|
|
> configure Google Analytics directly using it, rather than including Google Analytics as a separate component. Refer to the [documentation](https://developers.google.com/analytics/devguides/collection/ga4/tag-options#what-is-gtm)
|
|
> to learn more about the differences between Tag Manager and `gtag.js`.
|
|
|
|
To load Google Analytics for all routes, include the component directly in your root layout and pass in your measurement ID:
|
|
|
|
```tsx filename="app/layout.tsx" switcher
|
|
import { GoogleAnalytics } from '@next/third-parties/google'
|
|
|
|
export default function RootLayout({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode
|
|
}) {
|
|
return (
|
|
<html lang="en">
|
|
<body>{children}</body>
|
|
<GoogleAnalytics gaId="G-XYZ" />
|
|
</html>
|
|
)
|
|
}
|
|
```
|
|
|
|
```jsx filename="app/layout.js" switcher
|
|
import { GoogleAnalytics } from '@next/third-parties/google'
|
|
|
|
export default function RootLayout({ children }) {
|
|
return (
|
|
<html lang="en">
|
|
<body>{children}</body>
|
|
<GoogleAnalytics gaId="G-XYZ" />
|
|
</html>
|
|
)
|
|
}
|
|
```
|
|
|
|
To load Google Analytics for a single route, include the component in your page file:
|
|
|
|
```jsx filename="app/page.js"
|
|
import { GoogleAnalytics } from '@next/third-parties/google'
|
|
|
|
export default function Page() {
|
|
return <GoogleAnalytics gaId="G-XYZ" />
|
|
}
|
|
```
|
|
|
|
### Use `@next/third-parties` to add Google Tag Manager
|
|
|
|
The `GoogleTagManager` component can be used to add [Google Tag Manager](https://developers.google.com/tag-manager/quickstart) to your page.
|
|
|
|
```tsx filename="app/layout.tsx" switcher
|
|
import { GoogleTagManager } from '@next/third-parties/google'
|
|
|
|
export default function RootLayout({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode
|
|
}) {
|
|
return (
|
|
<html lang="en">
|
|
<GoogleTagManager gtmId="GTM-XYZ" />
|
|
<body>{children}</body>
|
|
</html>
|
|
)
|
|
}
|
|
```
|
|
|
|
To load Google Tag Manager for a single route, include the component in your page file:
|
|
|
|
```jsx filename="app/page.js"
|
|
import { GoogleTagManager } from '@next/third-parties/google'
|
|
|
|
export default function Page() {
|
|
return <GoogleTagManager gtmId="GTM-XYZ" />
|
|
}
|
|
```
|
|
|
|
## Good to know
|
|
|
|
- If you are using the Pages Router, please refer to the [`pages/` documentation](/docs/pages/guides/third-party-libraries).
|
|
- `@next/third-parties` also supports [other third parties](/docs/app/guides/third-party-libraries#google-tag-manager).
|
|
- Using `@next/third-parties` is not required. You can also use the `next/script` component directly. Refer to the [`next/script` documentation](/docs/app/guides/scripts) to learn more.
|
|
|
|
## Useful Links
|
|
|
|
- [`@next/third-parties` Documentation](/docs/app/guides/third-party-libraries)
|
|
- [`next/script` Documentation](/docs/app/guides/scripts)
|