How to Add Google Tag Manager in Next.js 15 App Router (Official & Simple Method)
Learn how to integrate Google Tag Manager into your Next.js 15 App Router project using the official @next/third-parties package. This guide walks you through the latest and easiest method, no manual script tags, just clean integration.
Adding Google Tag Manager to your Next.js 15 app router is incredibly simple. You don’t need to mess with scripts or long chunks of code. Instead, we’ll use Next.js official Google Tag Manger component and your Google Tag Manager ID. Let’s get started
Google Tag Manager Setup Guide
Step 1: Create Your Google Tag Manager Account
- 1.Visit tagmanager.google.com
- 2.Log in using your Google account (Gmail or Workspace).
- 3.Once logged in, you’ll either see your existing GTM accounts or an empty dashboard.
- 4.Click the “Create Account” button (highlighted in sky blue).
- 5.You’ll now go through two steps:
- Set up your Account
- Set up your Container

What’s the Difference Between Account and Container?
- Account: Represents your business or organization.
- Container: Holds all the tags you’ll install on your website.
For example, if you're managing sites for two different businesses, create separate GTM accounts and each with its own container.
Fill Out the Setup Form
- Account Name: Use your business or company name.
- Country: Choose your location.
- Container Name: Name it after your website.
- Target Platform: Select Web.
Click Create, accept the Terms of Service, and wait a few seconds.
You’ll see a popup modal with installation scripts—just close it. We only need the GTM ID.
If you're stuck, watch this YouTube video
Step 2: Copy Your Google Tag Manager ID
- 1.Go to the Google Tag Manager dashboard.
- 2.Click your account, then select your container.
- 3.In the top-right, copy your GTM ID (starts with
GTM-XXXXXX
).

Add Google Tag Manager to Next.js 15 App Router
Step 1: Install the next js third parties Library
Use your terminal:
For npm
npm install @next/third-parties@latest next@latest
For pnpm
pnpm install @next/third-parties@latest next@latest
For more details read the official documentation
Optimize the performance of third-party libraries in your application with the `@next/third-parties` package.
Step 2: Add Google Tag Manager to your main layout file
Next, you’ll need to add the Google Tag Manager component to your app/layout.jsx
or if you are using typescript then app/layout.tsx
For JavaScript use this :
import { GoogleTagManager } from "@next/third-parties/google";
import "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
{process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID &&
process.env.NODE_ENV === "production" && (
<GoogleTagManager gtmId={process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID} />
)}
</html>
);
}
For TypeScript use this :
import { GoogleTagManager } from "@next/third-parties/google";
import "./globals.css";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
{process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID &&
process.env.NODE_ENV === "production" && (
<GoogleTagManager gtmId={process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID!} />
)}
</html>
);
}
Key Points why we added conditions here:
- The
GoogleTagManager
component is only loaded in production mode (process.env.NODE_ENV === "production"
). This ensures no data is sent during development. - The
NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID
environment variable stores your Google Tag Manager ID.
Step 3: Add Your GTM ID to Environment Variables
- 1.Create a
.env.local
file in the root of your project (if it doesn’t already exist). - 2.Add the following line to the file:
NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID=GTM-XXXXXXX
Replace GTM-XXXXXXX
with the ID you copied from your Google Tag Manager account.
Why Use Environment Variables?
- Storing your Measurement ID in an environment variable makes it easy to update without modifying your code.
- It also keeps sensitive information out of your codebase.
Step 4: Test Your Setup
- 1.Deploy Your Next.js Project:
- Make sure to update the environment variable (
NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID
) with your Measurement ID before deployment. - Deploy your project to your hosting platform (e.g., Vercel, Netlify, or any other).
- Alternatively, you can test it locally in production mode by runningBashCopy
npm run build
after build is completed run the build using this command
BashCopynpm run start
- 2.Check Real-Time Tracking (Only if you've added Google Analytics inside Google Tag Manager. If not, refer to the blog linked in the Related Blogs section.)
- Visit your website in a new browser tab or incognito window.
- Open your Google Analytics dashboard and navigate to the Real-Time section.
- Confirm that your website traffic is being tracked by checking for active users or pageviews.
- 3.Troubleshooting:
- If no data appears, double-check the following:
- Ensure the
NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID
environment variable is correctly set. - Verify that the
GoogleTagManager
component is only running in production mode (process.env.NODE_ENV === "production"
). - Clear your browser cache or try accessing the site from a different device.
Wrapping Up
You’ve now successfully integrated Google Tag Manager into your Next.js 15 app using the latest and official method. No more adding raw script tags—this way is faster, safer, and cleaner.
Related Blogs
Want to add your first Google Analytics tag in Tag Manager? Check out this blog

How to Add Google Analytics 4 to Google Tag Manager (Step-by-Step Guide with GA4 ID Setup)
Learn how to add Google Analytics 4 to your website using Google Tag Manager the right way. This step-by-step guide covers creating a GA4 property, getting the Measurement ID, and setting up tags with variables and triggers.
Just want to add Google Analytics Check this blog

How to Add Google Analytics in Next.js 15 App Router (Official & Simple Method)
Learn how to easily add Google Analytics to your Next.js 15 App Router project using the official @next/third-parties library, no manual scripts, just a simple and modern setup.