react-fathomv0.3.0
On this page
  1. TrackPageview
  2. Props
  3. Without Children
  4. With Referrer
  5. TrackClick
  6. Props
  7. With Value
  8. With Links
  9. Multiple Children
  10. TrackVisible
  11. Props
  12. Custom Element Type
  13. With Threshold
  14. With Root Margin
  15. Implementation Notes

Components

API reference for TrackPageview, TrackClick, and TrackVisible components.

TrackPageview

Track a pageview when the component mounts.

tsx
import { TrackPageview } from 'react-fathom'

function Dashboard() {
  return (
    <TrackPageview url="/dashboard">
      <div>Dashboard content</div>
    </TrackPageview>
  )
}

Props

PropTypeRequiredDescription
urlstringNoURL to track
referrerstringNoReferrer URL
childrenReactNodeNoChild elements to render

Without Children

tsx
<>
  <TrackPageview url="/landing" />
  <LandingPageContent />
</>

With Referrer

tsx
<TrackPageview url="/campaign" referrer="https://twitter.com">
  <CampaignPage />
</TrackPageview>

TrackClick

Track an event when a child element is clicked.

tsx
import { TrackClick } from 'react-fathom'

function SignUpSection() {
  return (
    <TrackClick eventName="signup-click">
      <button>Sign Up Free</button>
    </TrackClick>
  )
}

Props

PropTypeRequiredDescription
eventNamestringYesEvent name to track
_valuenumberNoValue in cents
preventDefaultbooleanNoPrevent default click behavior
childrenReactNodeYesClickable child element

With Value

tsx
<TrackClick eventName="plan-selected" _value={999}>
  <button>Choose Pro Plan - $9.99/mo</button>
</TrackClick>
tsx
<TrackClick eventName="docs-link-click">
  <a href="/docs">View Documentation</a>
</TrackClick>

Multiple Children

The click handler is attached to a wrapper div:

tsx
<TrackClick eventName="card-click">
  <div className="card">
    <h3>Feature</h3>
    <p>Description</p>
  </div>
</TrackClick>

TrackVisible

Track an event when an element becomes visible in the viewport.

tsx
import { TrackVisible } from 'react-fathom'

function PricingSection() {
  return (
    <TrackVisible eventName="pricing-viewed">
      <section>
        <h2>Pricing</h2>
        {/* pricing content */}
      </section>
    </TrackVisible>
  )
}

Props

PropTypeRequiredDefaultDescription
eventNamestringYes-Event name to track
_valuenumberNo-Value in cents
thresholdnumberNo0.1Visibility threshold (0-1)
rootMarginstringNo-IntersectionObserver root margin
asstringNo'div'HTML element to render
childrenReactNodeYes-Child elements

Custom Element Type

tsx
<TrackVisible eventName="footer-viewed" as="footer">
  <p>Footer content</p>
</TrackVisible>
tsx
<TrackVisible eventName="section-viewed" as="section" className="my-section">
  <h2>Section Title</h2>
</TrackVisible>

With Threshold

tsx
// Track when 50% of the element is visible
<TrackVisible eventName="hero-viewed" threshold={0.5}>
  <div className="hero">
    <h1>Welcome</h1>
  </div>
</TrackVisible>

With Root Margin

tsx
// Track 100px before the element enters the viewport
<TrackVisible eventName="upcoming-section" rootMargin="100px">
  <section>Content</section>
</TrackVisible>

Implementation Notes

  • Uses IntersectionObserver internally
  • Only tracks once per mount
  • Passes through all additional props to the wrapper element
  • Falls back gracefully if IntersectionObserver is unavailable

Last updated: October 8, 2026

By

Commune Software