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

meta-pixel-react

v0.1.0

Published

React bindings for meta-pixel: an SSR-safe provider and hooks for Meta's Pixel.

Readme

meta-pixel-react

npm version npm downloads License

React bindings for meta-pixel: an SSR-safe provider and a typed hook for Meta's Pixel. Client-side only.

Why

Most React pixel wrappers expose a singleton you must init yourself in an effect, drop window is undefined errors during SSR, and accept untyped track(event, data) calls. This package gives you:

  • <MetaPixelProvider> — loads fbevents.js and initializes your pixels on the client only (safe to render during SSR/RSC).
  • useMetaPixel() — a typed hook; Purchase requires currency/value, advanced-matching fields are strings, etc. (all inherited from meta-pixel).
  • Automatic PageView on route change, glob-matched per pixel.
  • GDPR consent helper for the revoke-then-grant flow.

Installation

npm i meta-pixel-react
# react >= 18 is a peer dependency

Usage

Wrap your app once. Pass the current pathname from your router to get automatic page views:

import { MetaPixelProvider } from 'meta-pixel-react'

const pixelOptions = {
  consent: 'revoke', // optional: hold delivery until the user opts in
  pixels: {
    main: { id: '1234567890', pageView: '**' },
  },
} as const

export function App({ children }: { children: React.ReactNode }) {
  const pathname = usePathname() // next/navigation, react-router, etc.
  return (
    <MetaPixelProvider options={pixelOptions} pathname={pathname}>
      {children}
    </MetaPixelProvider>
  )
}

Define options outside the component (or useMemo it) — it is read once on mount.

Next.js (App Router)

<MetaPixelProvider> is a Client Component ('use client'), so wrap it in your own client providers file and mount it in the root layout. usePathname() drives the automatic PageView:

// app/providers.tsx
'use client'

import { usePathname } from 'next/navigation'
import { MetaPixelProvider } from 'meta-pixel-react'

const pixelOptions = {
  pixels: { main: { id: '1234567890' } },
} as const

export function Providers({ children }: { children: React.ReactNode }) {
  const pathname = usePathname()
  return (
    <MetaPixelProvider options={pixelOptions} pathname={pathname}>
      {children}
    </MetaPixelProvider>
  )
}
// app/layout.tsx (Server Component — no 'use client')
import { Providers } from './providers'

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

The provider is safe to render on the server: fbevents.js is only injected in a client effect, so SSR/RSC never touches window/document.

Pages Router: pass pathname={router.asPath.split('?')[0]} from next/router's useRouter() instead of usePathname().

Track events

Track events anywhere below the provider:

import { useMetaPixel } from 'meta-pixel-react'

function CheckoutButton() {
  const { $fbq } = useMetaPixel()
  return (
    <button onClick={() => $fbq('track', 'Purchase', { value: 9.99, currency: 'EUR' })}>
      Buy
    </button>
  )
}

GDPR consent

Configure consent: 'revoke' to hold delivery, then grant once the user accepts:

const { consent } = useMetaPixel()
// in your cookie-banner accept handler:
consent('grant')

Manual page views

Omit pathname to disable automatic page views and fire them yourself:

const { pageView } = useMetaPixel()
pageView()           // every pixel
pageView('1234567890') // a single pixel (uses trackSingle)

API

<MetaPixelProvider options pathname?>

| Prop | Description | | --- | --- | | options | { enabled?, consent?, pixels }. Read once on mount. | | pathname | Current route path. When it changes, pixels whose pageView glob matches fire a PageView. Omit to disable auto page views. |

options.pixels is keyed by an arbitrary name; each pixel is { id, autoConfig?, advancedMatching?, pageView? }. enabled: false loads and sends nothing while keeping useMetaPixel() working (no-op $fbq).

useMetaPixel()

Returns { $fbq, consent, pageView }. $fbq is the fully-typed query function from meta-pixel — use it for track, trackCustom, trackSingle, and CAPI deduplication via the 4th eventID arg: $fbq('track', 'Purchase', {...}, { eventID }).

pageView globs

Patterns use meta-pixel's in-house matcher: **, *, ?, and a leading !. No brace/char-class expansion.

Resources

  • Core library: https://npmjs.com/package/meta-pixel
  • Pixel reference: https://developers.facebook.com/docs/meta-pixel/reference
  • Advanced matching: https://developers.facebook.com/docs/meta-pixel/advanced/advanced-matching