Skip to content

useNextScriptForGoogleAnalytics (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useNextScriptForGoogleAnalytics": "error"
}
}
}
}

Enforce the use of the @next/third-parties/google components to load Google Analytics and Google Tag Manager.

Adding the Google Analytics or Google Tag Manager script with a plain <script> element can slow down your page. The GoogleAnalytics and GoogleTagManager components from @next/third-parties/google load these scripts after the page becomes interactive.

This rule reports <script> elements that load one of these scripts, either through the src attribute or through inline code in dangerouslySetInnerHTML.

<script async src="https://www.googletagmanager.com/gtag/js?id=G-XYZ" />
code-block.jsx:1:1 lint/nursery/useNextScriptForGoogleAnalytics ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This script element loads Google Analytics.

> 1 │ <script async src="https://www.googletagmanager.com/gtag/js?id=G-XYZ" />
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ Loading Google Analytics with a plain script element can slow down your page.

ℹ Use the GoogleAnalytics component from @next/third-parties/google instead, which loads the script after the page becomes interactive. See the Next.js docs for more details.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

<script
dangerouslySetInnerHTML={{
__html: `
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XYZ');
`,
}}
/>
code-block.jsx:1:1 lint/nursery/useNextScriptForGoogleAnalytics ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This script element loads Google Tag Manager.

> 1 │ <script
│ ^^^^^^^
> 2 │ dangerouslySetInnerHTML={{
> 3 │ __html: `
> 4 │ (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
...
> 9 │ `,
> 10 │ }}
> 11 │ />
│ ^^
12 │

ℹ Loading Google Tag Manager with a plain script element can slow down your page.

ℹ Use the GoogleTagManager component from @next/third-parties/google instead, which loads the script after the page becomes interactive. See the Next.js docs for more details.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

import { GoogleAnalytics, GoogleTagManager } from "@next/third-parties/google";
export default function RootLayout({ children }) {
return (
<html lang="en">
<GoogleTagManager gtmId="GTM-XYZ" />
<body>{children}</body>
<GoogleAnalytics gaId="G-XYZ" />
</html>
);
}