---
title: "Discussion on \"Track User Behaviour On Your Website With Google Analytics & Next.JS\" | Hashnode"
description: "Discussion on \"Track User Behaviour On Your Website With Google Analytics & Next.JS\". How To Set Up Google Analytics With Next.JS Script Component"
url: "https://hashnode.com/posts/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs/60e3b98ae9aeb8301a6a41fb"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Track User Behaviour On Your Website With Google Analytics & Next.JS](https://blog.jarrodwatts.com/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs?utm_source=hashnode&utm_medium=feed)

How To Set Up Google Analytics With Next.JS Script Component

Jul 6, 2021 · 5 min read · blog.jarrodwatts.com  
Read the article

-   [#nextjs](https://hashnode.com/tag/nextjs)
-   [#analytics](https://hashnode.com/tag/analytics)
-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#javascript](https://hashnode.com/tag/javascript)
-   [#data-analysis](https://hashnode.com/tag/data-analysis)

## Written by

[Jarrod Watts](https://hashnode.com/@JarrodWatts)

@JarrodWatts · Sydney, Australia

  
[View profile](https://hashnode.com/@JarrodWatts)

Developer Relations Engineer @ Polygon Labs

A software engineer turned developer relations engineer in the web3 space, from Australia 🇦🇺.

## Responses 7

Markdown supported · ⌘↵ to post

[offline](https://hashnode.com/@anthonyjdella)

[Feb 6, 2022](https://hashnode.com/posts/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs/60e3b98ae9aeb8301a6a41fb/comment/62000120295d276006774c08)

Hey, I was getting a compilation error with this because `Script` requires an id. So after giving it an id, everything worked. Putting this here, if that helps anyone.

But even doing so, I get undefined when typing `dataLayer` in Dev tools. So I'm kinda stuck ATM.

edit: FIXED the issue. If anyone is having similar issues, follow these instructions:

1.  Give the `Script` any id string value.
2.  Set the strategy to `afterInteractive` instead of `lazyOnLoad`

[Avi R](https://hashnode.com/@avithe1)

[Oct 13, 2021](https://hashnode.com/posts/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs/60e3b98ae9aeb8301a6a41fb/comment/616682ff53401f641ba14b97)

For some reason I got the following error :

> Unhandled Runtime Error SyntaxError: Invalid or unexpected token

I had to change the code to :

```
<Script strategy="lazyOnload">{`
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

   gtag('config','${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}');
    `}
</Script>
```

to fix the error. I could have said that your code has an issue but then your video did not show any issues.

[Hesam Alavi](https://hashnode.com/@Hesam)

[Oct 11, 2021](https://hashnode.com/posts/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs/60e3b98ae9aeb8301a6a41fb/comment/6163ee8453401f641ba118c9)

Hey [Jarrod Watts](https://hashnode.com/@JarrodWatts)thanks very much for this. A question, is this the same as setting up google tag manager or that is a different thing?

[Erisan Olasheni](https://hashnode.com/@olasheni)

[Oct 2, 2021](https://hashnode.com/posts/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs/60e3b98ae9aeb8301a6a41fb/comment/615842aa70a8165c11f6397d)

Nice article [Jarrod Watts](https://hashnode.com/@JarrodWatts), thanks. **Quick Note:** I think the `XXXXXXXXXX` in the `gtag('config', ...` line also needs to be prefixed with `G-` making it `G-XXXXXXXXXX`. Thanks.

[Thys](https://hashnode.com/@Thys)

[Jul 9, 2021](https://hashnode.com/posts/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs/60e3b98ae9aeb8301a6a41fb/comment/60e7fcde8aaa3f019846bd9d)

How would you track router changes with this solution?

[Jarrod Watts](https://hashnode.com/@JarrodWatts)

[Jul 9, 2021](https://hashnode.com/posts/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs/60e3b98ae9aeb8301a6a41fb/comment/60e7fcde8aaa3f019846bd9d#reply-60e865f36d962b01bdecacc5)

This tag gets applied on every page since we're using it in the \_app.js file - hopefully that answers your question

[Andrew Baisden](https://hashnode.com/@andrewbaisden)

[Jul 7, 2021](https://hashnode.com/posts/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs/60e3b98ae9aeb8301a6a41fb/comment/60e59a19e9aeb8301a6a986f)

Great article you reminded me that I have not checked the analytics for my website in a very long time. I imagine I have a lot more traffic now that I am more active in the community.

[Jarrod Watts](https://hashnode.com/@JarrodWatts)

[Jul 8, 2021](https://hashnode.com/posts/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs/60e3b98ae9aeb8301a6a41fb/comment/60e59a19e9aeb8301a6a986f#reply-60e65845d534931b99a4f82b)

Thanks Andrew! For sure, I should definitely check my own analytics more too.

I've got GA setup for my Hashnode blog.jarrodwatts.com domain, which lets me know how much time people are spending on average on each of my posts which is a great feature.

I find that quite a meaningful measure of how much people are engaged with the content.

Glad you enjoyed the article!

[Kieran Roberts](https://hashnode.com/@kieran6roberts)

[Jul 6, 2021](https://hashnode.com/posts/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs/60e3b98ae9aeb8301a6a41fb/comment/60e496c8e9aeb8301a6a68f2)

Really good article [Jarrod Watts](https://hashnode.com/@JarrodWatts), we all need our fix of analytics!

[Jarrod Watts](https://hashnode.com/@JarrodWatts)

[Jul 6, 2021](https://hashnode.com/posts/track-user-behaviour-on-your-website-with-google-analytics-and-nextjs/60e3b98ae9aeb8301a6a41fb/comment/60e496c8e9aeb8301a6a68f2#reply-60e4e9f94aacd630482445e6)

Thanks Kieran! Haha, it can be scary but it's worth the small time required :)
