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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@nik0di3m/utm-tracker-hook

v1.3.0

Published

Framework-agnostic React Hook for tracking and storing UTM parameters in cookies. Works with Next.js, Vite, CRA, and any React application.

Readme

utm-tracker-hook

🔍 Framework-agnostic React Hook for UTM tracking

📌 Automatically saves UTM, GCLID, FBCLID parameters in cookies for 30 days

✨ Works with Next.js, Vite, Create React App, and any React application

📦 Installation

Copy

npm install utm-tracker-hook

🚀 How to use?

Option 1: Using UtmTrackerProvider (Recommended)

Add the provider to your root component to automatically track UTM parameters across your entire app.

Next.js (App Router)

// app/layout.tsx
import { UtmTrackerProvider } from "@nik0di3m/utm-tracker-hook";

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        <UtmTrackerProvider>{children}</UtmTrackerProvider>
      </body>
    </html>
  );
}

Next.js (Pages Router)

// pages/_app.tsx
import { UtmTrackerProvider } from "@nik0di3m/utm-tracker-hook";
import type { AppProps } from "next/app";

export default function App({ Component, pageProps }: AppProps) {
  return (
    <UtmTrackerProvider>
      <Component {...pageProps} />
    </UtmTrackerProvider>
  );
}

Vite / Create React App

// src/App.tsx
import { UtmTrackerProvider } from "@nik0di3m/utm-tracker-hook";

function App() {
  return (
    <UtmTrackerProvider>
      <YourAppContent />
    </UtmTrackerProvider>
  );
}

Custom cookie expiry

<UtmTrackerProvider expiryDays={60}>{children}</UtmTrackerProvider>

Track custom parameters

You can track additional custom URL parameters beyond the standard UTM parameters:

<UtmTrackerProvider
  expiryDays={30}
  customParams={["ref", "campaign_id", "affiliate_id", "promo_code"]}
>
  {children}
</UtmTrackerProvider>

Allow override existing cookies

By default, the hook implements first-touch attribution (doesn't overwrite existing data). To enable last-touch attribution:

<UtmTrackerProvider allowOverride={true}>
  {children}
</UtmTrackerProvider>

Custom cookie name

Avoid conflicts by using a custom cookie name:

<UtmTrackerProvider cookieName="my_app_utm_data">
  {children}
</UtmTrackerProvider>

Full configuration example

<UtmTrackerProvider
  expiryDays={60}
  customParams={["ref", "affiliate_id", "promo_code"]}
  allowOverride={true}
  cookieName="my_utm_tracking"
>
  {children}
</UtmTrackerProvider>

For URL:

https://yoursite.com?utm_source=google&ref=homepage&promo_code=SUMMER2024

The hook will capture:

  • Standard: utm_source, utm_medium, utm_campaign, utm_term, utm_content, gclid, fbclid
  • Custom: ref, affiliate_id, promo_code

Option 2: Using the hook directly

Basic usage

import { useUtmTracker } from "@nik0di3m/utm-tracker-hook";

export default function MyComponent() {
  const utmData = useUtmTracker();

  // utmData contains: utm_source, utm_medium, utm_campaign, utm_term, utm_content, gclid, fbclid
  return <pre>{JSON.stringify(utmData, null, 2)}</pre>;
}

With configuration

import { useUtmTracker } from "@nik0di3m/utm-tracker-hook";

export default function MyComponent() {
  const utmData = useUtmTracker({
    expiryDays: 60,
    customParams: ["ref", "affiliate_id"],
    allowOverride: true,
    cookieName: "my_utm_data"
  });

  return <pre>{JSON.stringify(utmData, null, 2)}</pre>;
}

Backward compatible (legacy)

// Still works for backward compatibility
const utmData = useUtmTracker(60); // expiryDays only

📝 Configuration Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | expiryDays | number | 30 | Cookie expiration time in days | | customParams | string[] | [] | Additional URL parameters to track | | allowOverride | boolean | false | Allow overwriting existing cookie data (enables last-touch attribution) | | cookieName | string | "utm_tracking_data" | Custom cookie name to avoid conflicts |

✨ Key Features

  • 🎯 Framework-agnostic - Works with Next.js, Vite, CRA, and any React app
  • 🍪 Automatic cookie storage - UTM parameters stored for 30 days (configurable)
  • 📊 Comprehensive tracking - Captures UTM parameters, GCLID, and FBCLID
  • 🎨 Custom parameters - Track any additional URL parameters you need
  • 🔒 SSR-safe - Works seamlessly with server-side rendering
  • 📦 Lightweight - Minimal dependencies (only js-cookie)
  • 💪 TypeScript support - Fully typed with TypeScript
  • Easy integration - Drop-in provider component or use the hook directly
  • 🔄 Backward compatible - Legacy API still supported

📋 Tracked Parameters

The hook automatically captures and stores the following parameters:

  • utm_source - Campaign source
  • utm_medium - Campaign medium
  • utm_campaign - Campaign name
  • utm_term - Campaign term
  • utm_content - Campaign content
  • gclid - Google Click ID
  • fbclid - Facebook Click ID