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

next-meta-pixel

v0.1.0

Published

Facebook Pixel + Conversions API for Next.js App Router. Dual client/server tracking with automatic deduplication.

Readme

next-meta-pixel

Facebook Pixel + Conversions API (CAPI) for Next.js App Router.

Dual client/server event tracking with automatic deduplication, PII hashing, and TypeScript support.

Features

  • Pixel + CAPI — Send events to both browser and server for maximum attribution
  • Auto-deduplication — Shared event IDs prevent double-counting
  • PII hashing — SHA256 hashing of emails, phones, names before sending to Meta
  • App Router — Built for Next.js 13+ App Router with "use client" components
  • TypeScript — Full type safety with exported interfaces
  • Dev mode — Mock responses and fallback cookies in development
  • Zero dependencies — Only next and react as peer deps

Quick Start

1. Install

npm install next-meta-pixel

2. Add environment variables

# .env.local
NEXT_PUBLIC_FB_PIXEL_ID=123456789       # Your Pixel ID
FB_PIXEL_ACCESS_TOKEN=EAAx...           # Access token (for CAPI, server-side only)

3. Add to your layout

// app/layout.tsx
import { FacebookPixel, PixelPageView } from "next-meta-pixel";

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

4. Create the API route (for CAPI)

// app/api/fb-events/route.ts
import { fbEventsHandler } from "next-meta-pixel/handlers";
export const POST = fbEventsHandler;

5. Track events

import { fbEvent } from "next-meta-pixel";

// Track a lead
fbEvent({ eventName: "Lead" });

// Track with data + PII for better CAPI matching
fbEvent({
  eventName: "Lead",
  data: { content_name: "signup-form" },
  emails: ["[email protected]"],
  phones: ["+972501234567"],
});

API Reference

Components

<FacebookPixel />

Loads the Facebook Pixel script. Add once in your root layout.

<PixelPageView />

Tracks PageView on every route change. Add alongside <FacebookPixel />.

Client Functions

fbEvent(options)

Track an event on both Pixel (client) and CAPI (server).

fbEvent({
  eventName: "Purchase",          // Required — standard or custom event name
  data: {                         // Optional — event parameters
    value: 29.99,
    currency: "USD",
  },
  emails: ["[email protected]"],   // Optional — hashed and sent via CAPI
  phones: ["+1234567890"],        // Optional — hashed and sent via CAPI
  firstName: "John",              // Optional
  lastName: "Doe",                // Optional
  apiRoute: "/api/fb-events",     // Optional — default: "/api/fb-events"
});

Facebook standard events: Lead, Purchase, AddToCart, InitiateCheckout, ViewContent, CompleteRegistration, Subscribe, Search, etc.

usePixel()

React hook wrapper for fbEvent.

const { track } = usePixel();
track({ eventName: "AddToCart", data: { value: 19.99 } });

trackStandardEvent(name, options?, eventID?)

Low-level: fires fbq('track', ...) only (no CAPI).

trackCustomEvent(name, options, eventID)

Low-level: fires fbq('trackCustom', ...) only (no CAPI).

Server Functions

sendServerEvent(eventData)

Send an event directly to Facebook's Conversions API. Use this for server-side events (e.g., in API routes or Server Actions).

import { sendServerEvent } from "next-meta-pixel/server";

await sendServerEvent({
  eventName: "Lead",
  eventId: "unique-uuid",
  emails: ["[email protected]"],
  phones: ["+972501234567"],
  sourceUrl: "https://example.com/form",
});

Handler

fbEventsHandler

Pre-built Next.js POST handler for the CAPI API route.

// app/api/fb-events/route.ts
import { fbEventsHandler } from "next-meta-pixel/handlers";
export const POST = fbEventsHandler;

Environment Variables

| Variable | Required | Side | Description | |---|---|---|---| | NEXT_PUBLIC_FB_PIXEL_ID | Yes | Client + Server | Your Facebook Pixel ID | | FB_PIXEL_ACCESS_TOKEN | For CAPI | Server only | System User access token | | FB_TEST_EVENT_CODE | No | Server only | Test event code for development |

How Deduplication Works

When you call fbEvent():

  1. A unique eventId (UUID v4) is generated
  2. The same eventId is sent to both:
    • Client: fbq('track', 'Lead', { eventID: '...' })
    • Server: POST /api/fb-events → Facebook CAPI with event_id: '...'
  3. Facebook matches the two events by event_id and counts them once

This ensures you get the best of both worlds — immediate client-side tracking plus reliable server-side attribution.

Cookie Consent

This package does not enforce cookie consent. If you need consent gating, conditionally render the components:

function Layout({ children }) {
  const hasConsent = useCookieConsent(); // your consent hook

  return (
    <>
      {children}
      {hasConsent && <FacebookPixel />}
      {hasConsent && <PixelPageView />}
    </>
  );
}

The fbEvent() function will silently no-op if the pixel script hasn't been loaded.

CSP (Content Security Policy)

If you use CSP headers, add these domains:

script-src: https://connect.facebook.net
connect-src: https://connect.facebook.net https://www.facebook.com
img-src: https://www.facebook.com

Development Mode

In development (NODE_ENV=development):

  • The API route handler returns mock responses (no real Facebook API calls)
  • Missing _fbp/_fbc cookies are replaced with realistic fallbacks
  • All events are logged to the console with [next-meta-pixel] prefix
  • Set FB_TEST_EVENT_CODE to test with Facebook's Test Events tool

License

MIT