icon

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.

https://res.cloudinary.com/drzcgtzx8/image/upload/v1745064578/portfolio/blog/integrate-google-tag-manager-nextjs-15/integrate-google-tag-manager-nextjs-15_jghfkx.png
Posted by@Sujal Vanjare
Published on @
Updated on @

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. 2.b.ii.
    Log in using your Google account (Gmail or Workspace).
  2. 3.c.iii.
    Once logged in, youโ€™ll either see your existing GTM accounts or an empty dashboard.
  3. 4.d.iv.
    Click the โ€œCreate Accountโ€ button (highlighted in sky blue).
  4. 5.e.v.
    Youโ€™ll now go through two steps:
    • Set up your Account
    • Set up your Container
google tag manager account create filled form
google tag manager account create filled form

๐Ÿง  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

straight to the point YouTube video to create google tag manager account.


๐Ÿ“‹ Step 2: Copy Your Google Tag Manager ID

  1. 1.a.i.
    Go to the Google Tag Manager dashboard.
  2. 2.b.ii.
    Click your account, then select your container.
  3. 3.c.iii.
    In the top-right, copy your GTM ID (starts with GTM-XXXXXX).
where you will find GTM id
where you will find GTM id


๐Ÿ›  Add Google Tag Manager to Next.js 15 App Router

๐Ÿ“ฆ Step 1: Install the next js third parties Library

Use your terminal:

For npm

Bash
npm install @next/third-parties@latest next@latest

For pnpm

Bash
pnpm install @next/third-parties@latest next@latest

For more details read the official documentation โคต๏ธ

Guides: Third Party Libraries | Next.js

Optimize the performance of third-party libraries in your application with the `@next/third-parties` package.

Logo
https://nextjs.org/docs/app/building-your-application/optimizing/third-party-libraries#google-tag-manager
Thumbnail


๐Ÿงฉ 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 :

JavaScript
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>
  );
}
src/app/layout.jsx

For TypeScript use this :

TypeScript
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>
  );
}
src/app/layout.tsx

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. 1.a.i.
    Create aย .env.localย file in the root of your project (if it doesnโ€™t already exist).
  2. 2.b.ii.
    Add the following line to the file:
Plain Text
NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID=GTM-XXXXXXX
.env.local file

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. 1.a.i.
    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 running
      Bash
      npm run build

      after build is completed run the build using this command

      Bash
      npm run start
  2. 2.b.ii.
    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. 3.c.iii.
    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.


Want to add your first Google Analytics tag in Tag Manager? Check out this blogโฌ‡๏ธ

Just want to add Google Analytics ? Check this blogโฌ‡๏ธ


โš™๏ธ See Handy Tools