Adding Google Analytics to Next.js (w/ Script Component)

Опубликовано: 01 Март 2026
на канале: DiGiorGiu
6,163
142

In this tutorial I will show you how to integrate Google Analytics into a Next.js web application exploiting the power of the Next.js Script Component.

Would you like to virtually invite me for an Italian Espresso Coffee?! ☕
Buy me a Coffee 👉 https://www.buymeacoffee.com/theItali...

We all know how most of the third-party scripts can negatively impact on the performance, and so the user experience, of a website.

⛔️ Is important to remember that loading speed affect SEO too.

Thanks to the Next.js Script component we can choose the right load strategy and defer the load of a the Google Analytics script until necessary 🔥

You can read my blog post on how to add Google Analytics to Next.js here

➡ https://theitaliandev.hashnode.dev/ho...

Useful resources 📒

➡ https://nextjs.org/docs/basic-feature...
➡ https://nextjs.org/docs/messages/next...

#nextjsgoogleanalytics #nextjs #googleanalytics

Chapters
0:00 Intro
0:55 Github repository
3:30 Vercel deployment
4:08 Google Analytics Data Stream
6:10 Environment variables
7:19 Google Analytics script added via Next.js Script component
13:40 Custom function to track pageviews and subscription to router.events
18:45 Final tests and re-deploy
21:36 GTmetrix