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

react-fathom

v0.1.4

Published

Easily compose Fathom Analytics into your React/Next.js apps with automatic pageview tracking and full TypeScript support.

Readme

😻 react-fathom

npm NPM npm npm bundle size GitHub stars GitHub forks GitHub issues GitHub pull requests Coveralls github codecov CircleCI Known Vulnerabilities TypeScript GitHub last commit Twitter Follow

Easily compose Fathom Analytics into your React/Next.js apps with automatic pageview tracking and full TypeScript support.

Features

  • 🚀 Zero-config Fathom Analytics integration for React
  • 📦 Tree-shakeable - Only bundle what you use
  • 🔄 Automatic pageview tracking for Next.js (Pages Router & App Router)
  • 💪 Full TypeScript support with type definitions
  • 🎯 Flexible - Works with any React app or Next.js
  • Lightweight - Minimal bundle size impact

Install

Via npm

npm install react-fathom fathom-client

Via Yarn

yarn add react-fathom fathom-client

Peer Dependencies

  • react >= 16.8
  • react-dom >= 16.8
  • fathom-client >= 3.0.0
  • next >= 10.0.0 (only if using Next.js providers)

Usage

Basic React Setup

Wrap your app with FathomProvider:

import { FathomProvider } from 'react-fathom'

function App() {
  return <FathomProvider siteId="YOUR_SITE_ID">{/* Your app */}</FathomProvider>
}

Using the Hook

Access Fathom methods via the useFathom hook:

import { useFathom } from 'react-fathom'

function MyComponent() {
  const { trackPageview, trackEvent, trackGoal, load } = useFathom()

  const handleClick = () => {
    trackEvent?.('button-click', { id: 'signup-button' })
  }

  const handlePurchase = () => {
    trackGoal?.('purchase', 2999) // $29.99 in cents
  }

  return (
    <>
      <button onClick={handleClick}>Sign Up</button>
      <button onClick={handlePurchase}>Buy Now</button>
    </>
  )
}

Convenience Hooks

Track events and pageviews with convenience hooks:

import {
  useTrackOnMount,
  useTrackOnClick,
  useTrackOnVisible,
} from 'react-fathom'

function MyComponent() {
  // Track pageview on mount
  useTrackOnMount({ url: '/custom-page' })

  // Track event on click
  const handleClick = useTrackOnClick({
    eventName: 'button-click',
    id: 'signup-button',
    callback: (e) => {
      console.log('Tracked click!', e)
    },
  })

  // Track event when element becomes visible
  const ref = useTrackOnVisible({
    eventName: 'section-viewed',
    section: 'hero',
    callback: (entry) => {
      console.log('Element is visible!', entry)
    },
  })

  return (
    <>
      <button onClick={handleClick}>Sign Up</button>
      <div ref={ref}>This will be tracked when visible</div>
    </>
  )
}

Declarative Components

Use declarative components for tracking:

import { TrackPageview, TrackClick, TrackVisible } from 'react-fathom'

function MyPage() {
  return (
    <>
      {/* Track pageview on mount */}
      <TrackPageview url="/custom-page">
        <div>Page content</div>
      </TrackPageview>

      {/* Track click events */}
      <TrackClick eventName="button-click" id="signup-button">
        <button>Sign Up</button>
      </TrackClick>

      {/* Track when element becomes visible */}
      <TrackVisible eventName="section-viewed" section="hero">
        <div>Hero section</div>
      </TrackVisible>
    </>
  )
}

Next.js App Router

Use FathomProvider with NextFathomTrackViewApp for automatic route tracking:

// app/layout.tsx
import { FathomProvider } from 'react-fathom'
import { NextFathomTrackViewApp } from 'react-fathom/next'

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        <FathomProvider siteId="YOUR_SITE_ID">
          <NextFathomTrackViewApp />
          {children}
        </FathomProvider>
      </body>
    </html>
  )
}

Next.js Pages Router

Use FathomProvider with NextFathomTrackViewPages for automatic route tracking:

// pages/_app.tsx
import { FathomProvider } from 'react-fathom'
import { NextFathomTrackViewPages } from 'react-fathom/next'

function MyApp({ Component, pageProps }) {
  return (
    <FathomProvider siteId="YOUR_SITE_ID">
      <NextFathomTrackViewPages />
      <Component {...pageProps} />
    </FathomProvider>
  )
}

export default MyApp

API

FathomProvider

Main provider component for React apps. Supports composable nesting - nested providers can override client, defaultPageviewOptions, or defaultEventOptions.

Props:

  • siteId (string, optional): Your Fathom Analytics site ID
  • client (FathomClient, optional): Custom Fathom client instance
  • clientOptions (LoadOptions, optional): Options passed to fathom-client
  • defaultPageviewOptions (PageViewOptions, optional): Default options merged into all trackPageview calls
  • defaultEventOptions (EventOptions, optional): Default options merged into all trackEvent calls

Example:

<FathomProvider
  siteId="YOUR_SITE_ID"
  defaultPageviewOptions={{ referrer: 'https://example.com' }}
  defaultEventOptions={{ id: 'global-id' }}
>
  {/* Your app */}
</FathomProvider>

NextFathomTrackViewApp

Component that tracks pageviews for Next.js App Router. Must be used within a FathomProvider.

Props:

  • disableAutoTrack (boolean, optional): Disable automatic pageview tracking on route changes (defaults to false)

Example:

<FathomProvider siteId="YOUR_SITE_ID">
  <NextFathomTrackViewApp />
  {/* Your app */}
</FathomProvider>

NextFathomTrackViewPages

Component that tracks pageviews for Next.js Pages Router. Must be used within a FathomProvider.

Props:

  • disableAutoTrack (boolean, optional): Disable automatic pageview tracking on route changes (defaults to false)

Example:

<FathomProvider siteId="YOUR_SITE_ID">
  <NextFathomTrackViewPages />
  {/* Your app */}
</FathomProvider>

useFathom()

Hook to access Fathom methods and context.

Returns:

  • trackPageview(options?): Track a pageview (automatically merges defaultPageviewOptions)
  • trackEvent(eventName, options?): Track a custom event (automatically merges defaultEventOptions)
  • trackGoal(code, cents): Track a goal conversion
  • load(siteId, options?): Load Fathom with a site ID
  • setSite(siteId): Change the site ID
  • blockTrackingForMe(): Block tracking for current user
  • enableTrackingForMe(): Enable tracking for current user
  • isTrackingEnabled(): Check if tracking is enabled
  • client: The Fathom client instance
  • defaultPageviewOptions: Current default pageview options
  • defaultEventOptions: Current default event options

useTrackOnMount(options?)

Hook to track a pageview when a component mounts.

Options:

  • url (string, optional): URL to track
  • referrer (string, optional): Referrer URL
  • All other PageViewOptions from fathom-client

useTrackOnClick(options)

Hook that returns a click handler function to track events.

Options:

  • eventName (string, required): Event name to track
  • preventDefault (boolean, optional): Whether to prevent default behavior (defaults to false)
  • callback ((e?: MouseEvent) => void, optional): Callback function to run after tracking
  • All other EventOptions from fathom-client

useTrackOnVisible(options)

Hook that returns a ref to attach to an element. Tracks an event when the element becomes visible.

Options:

  • eventName (string, required): Event name to track
  • callback ((entry: IntersectionObserverEntry) => void, optional): Callback function to run after tracking
  • threshold (number, optional): IntersectionObserver threshold (defaults to 0.1)
  • rootMargin (string, optional): IntersectionObserver rootMargin
  • All other EventOptions from fathom-client

TrackPageview

Component that tracks a pageview when it mounts.

Props:

  • url (string, optional): URL to track
  • referrer (string, optional): Referrer URL
  • children (ReactNode, optional): Child elements to render
  • All other PageViewOptions from fathom-client

TrackClick

Component that tracks an event when clicked.

Props:

  • eventName (string, required): Event name to track
  • preventDefault (boolean, optional): Whether to prevent default behavior (defaults to false)
  • children (ReactNode, required): Child element(s) to render
  • All other EventOptions from fathom-client

TrackVisible

Component that tracks an event when it becomes visible.

Props:

  • eventName (string, required): Event name to track
  • threshold (number, optional): IntersectionObserver threshold (defaults to 0.1)
  • rootMargin (string, optional): IntersectionObserver rootMargin
  • children (ReactNode, required): Child element(s) to render
  • as (string, optional): HTML element type to render (defaults to 'div')
  • All other EventOptions from fathom-client

Tree-shaking

This library is optimized for tree-shaking. When you import only what you need:

import { useFathom } from 'react-fathom'

Bundlers will automatically exclude unused code, keeping your bundle size minimal.

TypeScript

Full TypeScript support is included. Types are automatically generated and exported.

License

MIT © Ryan Hefner