How can you add Google Analytics in Nextjs 13?

How can you add Google Analytics in Nextjs 13?Learn in 2 minutes

Sandipan Das
Sandipan Das |
1 min read66 views
twitterfacebooklinkedinpinterestW

Create a Google Analytics account by visiting the Google Analytics website and following the signup process.

Get your Tracking ID

You'll receive a tracking ID after creating your account. It looks like "UA-XXXXXXXXX-X". Keep this ID handy, as you'll need it to integrate Google Analytics with your Next.js project.

Implement with NEXTJS

Import your Script into your _app.tsx or _app.jsx while you want to add analytics globally

import Script from "next/script";

<Script async strategy="afterInteractive" src="https://www.googletagmanager.com/gtag/js?id=UA-1196-1" />
<Script
  id="google-analytics"
  strategy="afterInteractive"
  dangerouslySetInnerHTML={{
    __html: `
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'UA-YOUR ANALYTICS ID-1', {
      page_path: window.location.pathname,
    });
  `,
  }}
/>


Verify Tracking

Once you've added the tracking code, you can verify its implementation by visiting your website and checking your Google Analytics dashboard's "Realtime" section. It may take some time for data to start showing up.

That's it! You've successfully added Google Analytics to your Next.js 13 project. You'll now be able to track user interactions, page views, and more to gain insights into how your website is being used.

twitterfacebooklinkedinpinterestW

Join & Grow

Be Part of a Winning Community!
Let’s Connect, learn, and succeed with like-minded people. Follow now!

Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image

+100s of Happy Clients!

Thanks for checking out my portfolio!

I really enjoy programming—it's not just my job, it's something I love. I've finished lots of web projects using different tools, and I'm excited to share what I know with everyone and help out.

I know my way around ReactJS, NextJS, MongoDB, WordPress, Laravel, CodeIgniter, and making things look good (UI/UX). I'm also a quick learner, always ready to learn and grow in my field.

If you've got any questions or need some help, feel free to get in touch anytime.

Best Regards,

SANDIPAN DASFull Stack Web Developer

Let‘s collaborate
on your next project

Chat on WhatsApp
Schedule a Zoom Meeting