npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

inx-next-analytics-tracker

v1.0.0

Published

Google Analytics 4 tracker component for Next.js App Router with automatic page views and button click tracking

Readme

📊 next-analytics-tracker

Lightweight Google Analytics 4 (GA4) tracking for Next.js App Router Automatically tracks page views + button clicks with zero boilerplate.


👤 Author

[email protected]


🚀 Overview

next-analytics-tracker is a plug-and-play analytics solution designed specifically for Next.js App Router applications.

It removes the complexity of:

  • Manual page tracking
  • Repetitive click event tracking
  • Handling client-side route changes

✨ Features

  • 📄 Automatic Page View Tracking
  • 🖱️ Automatic Click Tracking
  • ⚡ Built for Next.js App Router
  • 🎯 Customizable click selectors
  • 🧠 Lightweight & optimized
  • 🔌 No external dependencies

📦 Installation

npm install next-analytics-tracker

or

yarn add next-analytics-tracker

or

pnpm add next-analytics-tracker

⚙️ Prerequisites

Before using this package, make sure:

  • ✅ You are using Next.js 13+ (App Router)
  • ✅ You have a Google Analytics 4 (GA4) Measurement ID
  • gtag.js is added to your project

🛠️ Setup Guide

1️⃣ Add Google Analytics (GA4)

Add this inside your root layout (layout.tsx):

import Script from 'next/script';

<Script
  src="https://www.googletagmanager.com/gtag/js?id=YOUR_GA_ID"
  strategy="afterInteractive"
/>

<Script id="google-analytics" strategy="afterInteractive">
  {`
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'YOUR_GA_ID');
  `}
</Script>

2️⃣ Add Analytics Tracker

import AnalyticsTracker from "next-analytics-tracker";

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <AnalyticsTracker
          gaId="G-XXXXXXXXXX"
          clickableSelectors={[
            "button",
            "a",
            '[role="button"]',
            "[data-analytics]",
            'input[type="submit"]',
            ".track-click",
            "[data-track]",
          ]}
        />
        {children}
      </body>
    </html>
  );
}

🎯 Usage

Track Button Clicks Automatically

By default, it tracks:

  • <button>
  • <a>
  • [role="button"]

You can extend tracking:

<AnalyticsTracker
  clickableSelectors={["button", ".cta-button", "[data-track]"]}
/>

Custom Tracking Example

<button data-analytics="signup">Sign Up</button>

📊 Events Sent to GA4

Page View

{
  "event": "page_view",
  "page_path": "/home"
}

Click Event

{
  "event": "click",
  "element_text": "Submit",
  "element_type": "button"
}

🧠 How It Works

📄 Page Tracking

  • Listens to route changes in Next.js App Router
  • Sends page_view event automatically

🖱️ Click Tracking

  • Uses event delegation
  • Captures clicks using provided selectors
  • Sends structured event data to GA4

💡 Why This Package?

While working on multiple Next.js projects, I found that:

  • GA4 integration required repetitive setup
  • Page tracking didn’t work correctly with App Router
  • Click tracking had to be manually added everywhere

So I built this package to:

  • ✅ Simplify analytics setup
  • ✅ Reduce boilerplate
  • ✅ Provide plug-and-play tracking

🧪 Use Cases

  • SaaS dashboards
  • Landing pages
  • Marketing funnels
  • Admin panels
  • Portfolio websites

⚠️ Best Practices

  • Use specific selectors:

    • .cta-button
    • [data-analytics="signup"]
  • Avoid tracking too many generic elements

  • Verify events in GA4 Realtime Dashboard


🐛 Troubleshooting

Events not showing?

  • Check your GA4 ID
  • Ensure gtag.js is loaded
  • Check browser console
  • Verify in GA4 → Realtime tab

🔗 Links


📄 License

MIT License


🙌 Support

If you find this helpful:

  • ⭐ Star the repo
  • 🐛 Report issues
  • 💡 Suggest features