icon

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.

thumbnail image
4 min
Posted by :Sujal
Published on
Updated on

Adding Google Analytics 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's official Google Analytics component from the @next/third-parties library and your Google Analytics ID.

Let’s get started!


Google Analytics Account Creation

Step 1: Create a Google Analytics Account

  1. 2.
    Click the “Get Started Today” button.
  2. 3.
    Sign in with your Google account. If you don’t have one, create a new account.
  3. 4.
    On the welcome page, click “Start Measuring.”

Step 2: Set Up Your Account

google analytics account creation page
google analytics account creation page
  1. 1.
    Fill in the Account Name (e.g., your business or website name).
    ⚠️

    Note: The account name is different from your Google Analytics ID. You’ll create a property under this account to track your website.

  2. 2.
    Scroll down and click Next.


Step 3: Create a Property

google analytics property creation page
google analytics property creation page
  1. 1.
    Enter a Property Name (e.g., your website domain name).
  2. 2.
    Adjust the Time Zone and Currency to match your location.
  3. 3.
    Click Next.


Step 4: Describe Your Business

google analytics business details page
google analytics business details page
  1. 1.
    Select your Industry Category (e.g., Computers & Electronics).
  2. 2.
    Choose your Business Size (e.g., Small for 1–10 employees).
  3. 3.
    Click Next.


Step 5: Choose Your Business Objectives

google analytics business objective page
google analytics business objective page
  1. 1.
    Select “Other Business Objectives” to enable all features.
  2. 2.
    Click Create.
  3. 3.
    Accept the Terms of Service and click I Accept.


Step 6: Set Up a Data Stream

  1. 1.
    On the “Start Collecting Data” page, select Web.
    google analytics data collection page
    google analytics data collection page

  2. 2.
    Enter your website URL (without https:// or trailing slashes /).
    google analytics set up data stream page
    google analytics set up data stream page
  3. 3.
    Add a Stream Name (e.g., your website name).
  4. 4.
    Click Create and Continue.
  5. 5.
    Wait a moment—you’ll see the “Set up Google tag” page. Close it using the cross icon at the top.


Step 7: Copy Your Measurement ID

google analytics web stream derails page where you will find measurement Id
google analytics web stream derails page where you will find measurement Id
  1. 1.
    On the Web Stream Details page, locate your Measurement ID (starts with G-).
  2. 2.
    Copy this ID—you’ll need it for the Next.js setup.
  3. 3.
    Close the setup page. Click “Next” then click “Go to Home” to open your Google Analytics dashboard.


Adding Google Analytics to Your Next.js Website

Step 1: Install Google Analytics in Next.js 15

To integrate Google Analytics into your Next.js website, install the official @next/third-parties library. This simplifies adding third-party scripts like Google Analytics.

Open your project folder in VS Code and run one of the following commands:

For npm

Shell
Copy
npm install @next/third-parties@latest next@latest

For pnpm

Shell
Copy
pnpm install @next/third-parties@latest next@latest

For more details read the official documentation ⤵️

Optimizing: 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-analytics
Thumbnail

Step 2: Add Google Analytics to Your Main Layout File

Add the GoogleAnalytics component to your app/layout.jsx, or if you're using TypeScript, to app/layout.tsx.

For JavaScript use this:

JavaScript
Copy
import { GoogleAnalytics } 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_ANALYTICS_ID &&
        process.env.NODE_ENV === "production" && (
          <GoogleAnalytics gaId={process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID} />
        )}
    </html>
  );
}
src/app/layout.jsx

For TypeScript use this:

TypeScript
Copy
import { GoogleAnalytics } 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_ANALYTICS_ID &&
        process.env.NODE_ENV === "production" && (
          <GoogleAnalyticsgaId={process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID!}
          />
        )}
    </html>
  );
}
src/app/layout.tsx

Key Points:

  • The GoogleAnalytics component is only loaded in production mode (process.env.NODE_ENV === "production").
  • Your Google Analytics Measurement ID is stored in the NEXT_PUBLIC_GOOGLE_ANALYTICS_ID environment variable.


Step 3: Add Your Measurement ID to Environment Variables

  1. 1.
    Create a .env.local file in the root of your project (if it doesn’t already exist).
  2. 2.
    Add the following line:
    Plain Text
    Copy
    NEXT_PUBLIC_GOOGLE_ANALYTICS_ID=G-XXXXXXX

    Replace G-XXXXXXX with your actual Measurement ID.

Why use environment variables?

  • Makes it easy to update the ID without changing your code.
  • Keeps sensitive data out of your source files.


Step 4: Verify Your Setup

  1. 1.
    Deploy Your Next.js Project:
    • Make sure to update the environment variable (NEXT_PUBLIC_GOOGLE_ANALYTICS_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
      Copy
      npm run build

      after build is completed run the build using this command

      Bash
      Copy
      npm run start
  2. 2.
    Check Real-Time Tracking:
    • 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.
    Troubleshooting:
    • If no data appears, double-check the following:
      • Ensure the NEXT_PUBLIC_GOOGLE_ANALYTICS_ID environment variable is correctly set.
      • Verify that the GoogleAnalytics 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.


Conclusion

That’s it! You’ve successfully added Google Analytics to your Next.js website using the official @next/third-parties library. It’s a simple and reliable method to track your website traffic without extra complexity.



⚙️ See Handy Tools