2023-07-05 15:11:16 +02:00
---
title: Next Script for Google Analytics
---
2021-08-16 20:55:07 +02:00
2022-06-14 04:17:42 +02:00
> Prefer `next/script` component when using the inline script for Google Analytics.
2023-07-05 15:11:16 +02:00
## Why This Error Occurred
2021-08-16 20:55:07 +02:00
An inline script was used for Google analytics which might impact your webpage's performance.
2023-07-05 15:11:16 +02:00
## Possible Ways to Fix It
2021-08-16 20:55:07 +02:00
2023-06-23 22:54:52 +02:00
#### Using gtag.js (recommended)
2021-08-16 20:55:07 +02:00
If you are using the [gtag.js](https://developers.google.com/analytics/devguides/collection/gtagjs) script to add analytics, use the `next/script` component with the right loading strategy to defer loading of the script until necessary.
2023-07-05 15:11:16 +02:00
```jsx filename="pages/index.js"
2021-08-16 20:55:07 +02:00
import Script from 'next/script'
2022-02-04 16:18:35 +01:00
function Home() {
2021-08-16 20:55:07 +02:00
return (
2022-05-25 01:15:06 +02:00
<div className="container">
2023-06-23 22:54:52 +02:00
<Script src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID" />
<Script id="google-analytics">
2021-08-16 20:55:07 +02:00
{`
window.dataLayer = window.dataLayer || [];
2023-06-23 22:54:52 +02:00
function gtag(){dataLayer.push(arguments);}
2021-08-16 20:55:07 +02:00
gtag('js', new Date());
gtag('config', 'GA_MEASUREMENT_ID');
`}
</Script>
</div>
)
}
export default Home
```
2023-06-23 22:54:52 +02:00
#### Using analytics.js (legacy)
2021-08-16 20:55:07 +02:00
If you are using the [analytics.js](https://developers.google.com/analytics/devguides/collection/analyticsjs) script to add analytics:
2023-07-05 15:11:16 +02:00
```jsx filename="pages/index.js"
2021-08-16 20:55:07 +02:00
import Script from 'next/script'
2022-02-04 16:18:35 +01:00
function Home() {
2021-08-16 20:55:07 +02:00
return (
2022-05-25 01:15:06 +02:00
<div className="container">
2023-06-23 22:54:52 +02:00
<Script id="google-analytics">
2021-08-16 20:55:07 +02:00
{`
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
`}
</Script>
</div>
)
}
export default Home
```
If you are using the [alternative async variant](https://developers.google.com/analytics/devguides/collection/analyticsjs#alternative_async_tag):
2023-07-05 15:11:16 +02:00
```jsx filename="pages/index.js"
2021-08-16 20:55:07 +02:00
import Script from 'next/script'
2022-02-04 16:18:35 +01:00
function Home() {
2021-08-16 20:55:07 +02:00
return (
2022-05-25 01:15:06 +02:00
<div className="container">
2023-06-23 22:54:52 +02:00
<Script id="google-analytics">
2021-08-16 20:55:07 +02:00
{`
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
2023-06-23 22:54:52 +02:00
ga('create', 'UA-XXXXX-Y', 'auto');
2021-08-16 20:55:07 +02:00
ga('send', 'pageview');
`}
</Script>
2023-06-23 22:54:52 +02:00
<Script src="https://www.google-analytics.com/analytics.js" />
2021-08-16 20:55:07 +02:00
</div>
)
}
export default Home
```
2023-07-05 15:11:16 +02:00
## Useful Links
2021-08-16 20:55:07 +02:00
- [Add analytics.js to Your Site](https://developers.google.com/analytics/devguides/collection/analyticsjs)
- [Efficiently load third-party JavaScript](https://web.dev/efficiently-load-third-party-javascript/)
2023-07-05 15:11:16 +02:00
- [next/script Documentation](/docs/pages/building-your-application/optimizing/scripts)