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

tanstack-router-ga4

v0.5.2

Published

Google Analytics (GA4) integration for TanStack Router and TanStack Start.

Readme

tanstack-router-ga4

NPM Package NPM Downloads Tests Coverage Demo Site

Google Analytics (GA4) integration for TanStack Router and TanStack Start, built for modern React apps that need reliable analytics with minimal setup.

Live demo: tanstack-router-ga4.benhouston3d.com

Features

  • Automatically records page_view events on route changes
  • Full TypeScript support for common GA4 events (sign_up, generate_lead, etc.) and custom events
  • Supports all Google Analytics configuration options (for example debug_mode and user_id)
  • Supports supports advanced features like user-session assocation, consents and queries (get.)
  • Designed for TanStack Router and TanStack Start with correct behavior across client-side navigation and SSR
  • Full test suite with unit/integration coverage and browser-based E2E tests
  • Ultra small bundles (minified/gzipped) to less than 1KB.

Install

pnpm add tanstack-router-ga4

Peer dependencies: react (>=18), @tanstack/react-router (>=1).

Usage

Mount the component in your app shell (for example a RootDocument or root layout) so every route gets automatic page views, and use the hook as a typed wrapper over the core gtag commands.

import { HeadContent, Scripts } from "@tanstack/react-router";
import type { ReactNode } from "react";
import { GoogleAnalytics, useGoogleAnalytics } from "tanstack-router-ga4";

// In your root document — automatic page_view on each route change
function RootDocument({ children }: { children: ReactNode }) {
  return (
    <html lang="en">
      <head>
        <HeadContent />
      </head>
      <body>
        <GoogleAnalytics measurementId="G-XXXXXXXXXX" />
        {children}
        <Scripts />
      </body>
    </html>
  );
}

// In any component — track custom events
function SignupForm() {
  const ga = useGoogleAnalytics();

  const handleSubmit = () => {
    const user = await registerUser({ email: "[email protected]", password: "secure-password" });

    // associate user id with session
    ga.set({
      user_id: user.id,
      user_properties: {
        email: user.email,
        username: user.username,
      },
    });

    // send standard GA event for signups
    ga.event("sign_up", { method: "email" });
  };

  return <button onClick={handleSubmit}>Sign up</button>;
}

Hook API

useGoogleAnalytics() exposes typed wrappers around the core gtag commands:

  • set(config) for global config
  • config(measurementId, config?) for per-stream config
  • event(name, params?) for recommended and custom events
  • get(measurementId, fieldName, callback?) for callback-based reads like client_id
  • consent('default' | 'update', params) for consent mode updates

Dvelopment

This repo contains the library and a TanStack Start demo:

  • Library: packages/tanstack-router-ga4
  • Demo: packages/example-website
pnpm install
pnpm build      # build library and example
pnpm dev        # library watch + example app (port 3000)
pnpm make-release   # publish package (builds, copies dist + root README, npm publish)

Publishing runs from the repo root and uses the root README as the package README.


Testing

pnpm exec playwright install chromium
pnpm test       # run Vitest tests

License

MIT

Author

Created by Ben Houston and sponsored by Land of Assets.