Check out new NextJs Third-Parties Component

After watching the NextJS Conf2023, they released @next/third-parties because of people reporting in the past months after they followed the Google Analytics Documentation, it made bad perfomance issues on Lighhouse.

After following NextJs documentation guide, I was able to fully setup Google Analytics on NextJs 14.

1. To load Google Tag Manager for all routes, include the component directly in your root layout:

app/layout.tsx

import { GoogleTagManager } from '@next/third-parties/google'

export default function RootLayout({children}: {children: React.ReactNode}) {
    return (
        <html lang="en">
            <body>{children}</body>
            <GoogleTagManager gtmId="GTM-XYZ" />
        </html>
     )
}

2. To load Google Tag Manager for a single route, include the component in your page file:

app/page.js

import { GoogleTagManager } from '@next/third-parties/google'

export default function Page() {
    return <GoogleTagManager gtmId="GTM-XYZ" />
}

Note: This is the Official NextJs Documentation.

Answer from Anda Hanise on Stack Overflow
🌐
GitHub
gist.github.com › pradeepdotco › 5ad04109add5f9a0306a
Google Tag Manager Sample Code · GitHub
Google Tag Manager Sample Code · Raw · Google-Tag-Manager-Sample-Code.php · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Google
developers.google.com › tag platform › gtag.js
Set up the Google tag with gtag.js | Tag Platform | Google for Developers
To use the Google tag, copy your tag ID from a Google product (e.g., Google Ads, Analytics), paste it into the provided code snippet, and place the snippet on every page you want to track. Verify your Google tag implementation using Tag Assistant to ensure it's sending data correctly by checking for your tag ID and consulting the Tag Manager help center for troubleshooting.
Discussions

How to add Google Tag Manager to a NextJS website
In your _document.js file which is located in /pages directory add below code. Also if there is no any _document.js file then please create it. Now add the functionality of Google Tag Manager as : More on stackoverflow.com
🌐 stackoverflow.com
Anyone Explain Google Tag Manager to me Please in Simple
Imagine your website is a physical store. Google Tag Manager (GTM) is like an advanced "camera system" that observes visitors when they enter your store and notifies your "partners" (such as Google Analytics, Google Ads, or Meta) about what those visitors do. For example, GTM can simply notify when a visitor arrives or provide more detailed insights, like tracking the areas they visit, the products they view, the items in their cart, or whether they complete a purchase or submit a form. However, it’s important to note that GTM itself does not analyze the data. Instead, it enables the tools you connect to it—like Google Analytics or Facebook Ads Manager—to perform the analysis and give you insights. Think of GTM as the messenger, not the decision-maker. The great part? Instead of installing a separate system for each of your partners, you install one GTM system and configure it to notify all your tools as needed. How GTM Works: Tags: Tags are the connections between GTM and your partners. Each tag sends specific information (like clicks, purchases, or video plays) to tools like Google Analytics or Meta Ads. Triggers: Triggers are the actions you want to monitor, such as a visitor clicking a button, submitting a form, or scrolling down a page. Variables: Variables are like reusable templates or placeholders for information. For example, you can store your Google Analytics ID in a variable and reference it across multiple tags. If the ID changes, you only need to update the variable once, and all related tags will update automatically. To set it up, you install a JavaScript tag on your website. Then, by configuring triggers, tags, and variables, you can track visitor behavior and share that data with the tools you use to analyze it. More on reddit.com
🌐 r/GoogleTagManager
26
12
January 19, 2025
Reddit code has a Google Tag Manager "jail"? : GoogleTagManager
I left the Chrome developer tools open and went back to Reddit. I wasn't sure what to make of this, but there appears to be a "jail" written... More on old.reddit.com
🌐 r/GoogleTagManager
Google Tag Manager / Google Analytics - No Code?

Yes, you can utilise those native DOM events assuming:

  • You can readily identify the elements in question in the page DOM. ie: are there ID attributes on the buttons you want to track?

  • You only want to track these 'simple' events, and not 'custom' events.

  • You do not need to expose any backend data to GTM/GA.

At that level of complexity GTM can more or less be a point and click kind of set-up.

More on reddit.com
🌐 r/GoogleTagManager
5
2
December 9, 2020
People also ask

What is Google Tag Manager?
Google Tag Manager is a free tool created by Google that allows you to control your tracking scripts in one place.
🌐
analyticsmania.com
analyticsmania.com › post › google-tag-manager-tutorial-for-beginners
Google Tag Manager Tutorial for Beginners (2025) + video
Does Google Tag Manager replace Google Analytics?
No. Google Analytics should be used together with Google Tag Manager. It is highly recommended to use GTM in order to control when certain Google Analytics tracking scripts should be activated.
🌐
analyticsmania.com
analyticsmania.com › post › google-tag-manager-tutorial-for-beginners
Google Tag Manager Tutorial for Beginners (2025) + video
Why should I start using Google Tag Manager?
There are multiple reasons. To name a few: 1. It allows you to implement tracking codes faster and in many cases, you don't have to wait for your IT department. 2. All your tracking scripts are controlled in one place. 3. Ready-made tag templates.
🌐
analyticsmania.com
analyticsmania.com › post › google-tag-manager-tutorial-for-beginners
Google Tag Manager Tutorial for Beginners (2025) + video
🌐
Analytics Mania
analyticsmania.com › post › google-tag-manager-tutorial-for-beginners
Google Tag Manager Tutorial for Beginners (2025) + video
October 15, 2025 - It can be a tracking code, some piece of code that changes the text or a particular website element, or even code that changes the color of the browser’s address bar, you name it. When you create a tag, you basically instruct Tag Manager to “do this”, “do that”, “track page views of this visitor”, “track this click and send to Google Analytics”, etc. A trigger is a condition when a tag must fire. Should a tag fire on all page views? Or maybe on certain clicks? How about successful form submissions? All of these examples are triggers.
🌐
MeasureSchool
measureschool.com › home › the google tag manager tutorial for beginners
Google Tag Manager Tutorial for Beginners (2025)
It has the objective, the link to Google’s quickstart guide, who to email for support and code examples. Since this is a template/guide, you must change a few details, like the email for support and parts of the code. Those are the steps for installing Google Tag Manager.
Published   January 9, 2025
🌐
Google
marketingplatform.google.com › about › tag-manager
Website Tag Management Tools & Solutions - Google Tag Manager
Tag Manager works seamlessly with Google and third-party tags, so you can change tags on the fly – saving time and increasing efficiency. Conversion tracking, basic remarketing, and dynamic remarketing are fully supported with Google Ads. Templates for third-party vendor tags simplify code ...
Top answer
1 of 12
48

Check out new NextJs Third-Parties Component

After watching the NextJS Conf2023, they released @next/third-parties because of people reporting in the past months after they followed the Google Analytics Documentation, it made bad perfomance issues on Lighhouse.

After following NextJs documentation guide, I was able to fully setup Google Analytics on NextJs 14.

1. To load Google Tag Manager for all routes, include the component directly in your root layout:

app/layout.tsx

import { GoogleTagManager } from '@next/third-parties/google'

export default function RootLayout({children}: {children: React.ReactNode}) {
    return (
        <html lang="en">
            <body>{children}</body>
            <GoogleTagManager gtmId="GTM-XYZ" />
        </html>
     )
}

2. To load Google Tag Manager for a single route, include the component in your page file:

app/page.js

import { GoogleTagManager } from '@next/third-parties/google'

export default function Page() {
    return <GoogleTagManager gtmId="GTM-XYZ" />
}

Note: This is the Official NextJs Documentation.

2 of 12
11

In your _document.js file which is located in /pages directory add below code. Also if there is no any _document.js file then please create it.

Now add the functionality of Google Tag Manager as :

import Document, { Html, Head, Main, NextScript } from "next/document";

function MyDocument() {
  return (
    <Html>
      <Head>
        <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=%27+i+dl;f.parentNode.insertBefore(j,f);
              })(window,document,'script','dataLayer','GOOGLE_TAG_MANAGER_ID');
            `,
          }}
        />
      </Head>
      <body>
        <noscript
          dangerouslySetInnerHTML={{
            __html: `
              <iframe src="https://www.googletagmanager.com/ns.html?id=GTM-xxxx"
              height="0" width="0" style="display:none;visibility:hidden"></iframe>
            `,
          }}
        />
        <Main />
        <NextScript />
      </body>
    </Html>
  );
}

export default MyDocument;

Now you can check the functionality is working or not and please let me know if it not works. I will surely help.

Find elsewhere
🌐
Google Support
support.google.com › tagmanager › answer › 15756616
Set up your Google tag in Google Tag Manager - Tag Manager Help
To send all events to a Tag Manager server container instead of Google Analytics, you need to configure the following parameter: Open the Configuration settings menu. Set up the server container URL by adding a new configuration parameter: ... Set up user properties: User properties are attributes that describe groups of your user base, such as their language preferences or geographic locations. You can use user properties to define audiences. For example, you can set a user property called favorite_food, which you can use to record each user's favorite food.
🌐
Analytics Mania
analyticsmania.com › post › how-to-install-google-tag-manager
How to Install Google Tag Manager on your Website
October 13, 2025 - However, based on my experiments, the performance is better than adding tracking codes directly to the source code of the website. Here’s an example of the website’s <head> where you could add the Google Tag Manager <script> code.
🌐
Simo Ahava
simoahava.com › analytics › fun-with-google-tag-manager-part-1
Fun With Google Tag Manager: Part 1 | Simo Ahava's blog
June 4, 2014 - So if the copy action takes place over multiple paragraphs, headers, or spans, for example, you’ll only be able to observe the properties of the first element targeted by the action. Google Tag Manager has this cool thing called the JavaScript Error Listener.
Top answer
1 of 5
8
Imagine your website is a physical store. Google Tag Manager (GTM) is like an advanced "camera system" that observes visitors when they enter your store and notifies your "partners" (such as Google Analytics, Google Ads, or Meta) about what those visitors do. For example, GTM can simply notify when a visitor arrives or provide more detailed insights, like tracking the areas they visit, the products they view, the items in their cart, or whether they complete a purchase or submit a form. However, it’s important to note that GTM itself does not analyze the data. Instead, it enables the tools you connect to it—like Google Analytics or Facebook Ads Manager—to perform the analysis and give you insights. Think of GTM as the messenger, not the decision-maker. The great part? Instead of installing a separate system for each of your partners, you install one GTM system and configure it to notify all your tools as needed. How GTM Works: Tags: Tags are the connections between GTM and your partners. Each tag sends specific information (like clicks, purchases, or video plays) to tools like Google Analytics or Meta Ads. Triggers: Triggers are the actions you want to monitor, such as a visitor clicking a button, submitting a form, or scrolling down a page. Variables: Variables are like reusable templates or placeholders for information. For example, you can store your Google Analytics ID in a variable and reference it across multiple tags. If the ID changes, you only need to update the variable once, and all related tags will update automatically. To set it up, you install a JavaScript tag on your website. Then, by configuring triggers, tags, and variables, you can track visitor behavior and share that data with the tools you use to analyze it.
2 of 5
7
GTM is an external tool installed in a website or app that allows you to insert user behavior tracking elements without having to modify the website itself or use a developer to code. It is especially beneficial to non-technical marketing people who can use the pre-defined tags and triggers to monitor, report, and analyze specific events that occur, such as a form submission, add to cart, or a puchase. These events can then be attributed to the traffic source for the website owners to understand how customers find their site and what is generating the leads or purchases.
🌐
Analytics Mania
analyticsmania.com › post › ultimate-google-tag-manager-data-layer-tutorial
Google Tag Manager Data Layer Tutorial (2025) with Examples
October 20, 2025 - The first method is useful if you want to push custom data right when the page loads, for example, product data when the product page loads. You might want to send that product data from the Data Layer to the Meta Pixel (with View Content event). In this case, your developers must add a Data Layer snippet above the Google Tag Manager tracking container with parameters like product ID, product title, etc.
🌐
Google
developers.google.com › tag platform › tag manager › the data layer
The data layer | Tag Platform | Google for Developers
The data layer is an object used by Google Tag Manager and gtag.js to pass information to tags. Events or variables can be passed via the data layer, and triggers can be set up based on the values of variables. For example, if you fire a remarketing tag when the value of purchase_total is greater than $100, or based on the specific events, e.g.
🌐
CXL
cxl.com › home › all things growth and marketing › google tag manager tutorial: simple gtm setup & workflow
Google Tag Manager Tutorial: Simple GTM Setup & Workflow
March 17, 2025 - Google Tag Manager Data Layer is a JavaScript code snippet that acts as an additional layer between your site’s HTML and your tags, triggers, and variables to make the data collection process smoother. Default tags automatically initiate the data layer, but if you want to track customer events, you’ll need to manually create a data layer variable. For example, use the data layer when passing ecommerce transaction data to Google Analytics, or when default Google Tag Manager form triggers don’t catch form submissions.
🌐
Optimize Smart
optimizesmart.com › google-tag-manager-data-layer-explained-like-never
#Google #Tag #Manager #Data #Layer Tutorial with examples
March 10, 2025 - Learn all about Google Tag Manager Data Layers with lot of examples. Create simple and complex data layers in minutes. Learn about GTM datalayer push.
🌐
Owox
owox.com › blog › use-cases › how-to-install-gtm
How to Install Google Tag Manager (GTM) in 2025
September 22, 2025 - A detailed guide on how to create an account in Google Tag Manager, install the code on your website, publish the container, and check its operation.
🌐
Tracking-garden
tracking-garden.com › ✙ knowledge ✙ › tag management › tag management systems › google tag manager › google tag manager code in detail
Google Tag Manager Code in Detail
August 26, 2021 - So it first tracks the page view and then returns an image - in our example a black PNG pixel. This means that rudimentary tracking is also possible if Javascript is not available. That is the point of the <noscript> part of the Google Tag Manager.
🌐
HubSpot
blog.hubspot.com › marketing › google-tag-manager-guide
Google Tag Manager: A Simple Tutorial
September 23, 2020 - Go to the Google Tag Manager and click the green “Sign Up for Free” button. It will ask you to input your account name (company), country, and website URL, as well as where you want to use Google Tag (web, iOS, android, AMP). When you’re finished, click the blue “Create” button. Next, you’ll be given codes and instructions to include one code high in the <head> of your page, and the other after the opening <body> tag.
🌐
Google Support
support.google.com › tagmanager › answer › 14842164
1. Create an account and container - Tag Manager Help
Note: If you're creating a container for a website, the container name should be the top-most URL for your website (for example, www.example.com). You don't need a container for each page of your website. ... Click Create and agree to the Terms of Service. If your company has more than one website or app, create another container by returning to the Accounts tab (top left) and then, next to the account name, clicking > Create Container. Once you create your container, continue to the next step to install the necessary code snippets for your website's Google Tag Manager container:
🌐
Stape
stape.io › blog › sgtm hosting › how to set up google tag manager and gtm tags in 5 minutes
How to Set Up Google Tag Manager and GTM Tags in 5 Minutes
September 19, 2025 - The <script> part of Google Tag Manager code should be placed in the <head> section of your website, and the <noscript> part should be added within the <body> section, ideally right after the opening <body> tag.