React Facebook

Pixel Setup

How to set up Facebook Pixel tracking with react-facebook using FacebookProvider or standalone FacebookPixelProvider.

Pixel Setup

Facebook Pixel lets you measure the effectiveness of your advertising by tracking visitor actions on your website. react-facebook provides two ways to set it up.

Approach 1: Via FacebookProvider

If you are already using FacebookProvider for login, social plugins, or Graph API, add the pixelId prop to enable pixel tracking alongside the SDK.

import { FacebookProvider } from 'react-facebook';

function App() {
  return (
    <FacebookProvider appId="YOUR_APP_ID" pixelId="YOUR_PIXEL_ID">
      <YourApp />
    </FacebookProvider>
  );
}

This wraps your component tree with both FacebookContext and FacebookPixelContext, so you can use any hook (useLogin, usePixel, usePageView, etc.) anywhere inside.

Approach 2: Standalone FacebookPixelProvider

If you only need pixel tracking and do not need the Facebook SDK (login, social plugins, Graph API), use FacebookPixelProvider directly for a smaller footprint.

import { FacebookPixelProvider } from 'react-facebook';

function App() {
  return (
    <FacebookPixelProvider pixelId="YOUR_PIXEL_ID" autoConfig debug={false}>
      <YourApp />
    </FacebookPixelProvider>
  );
}

Provider Props

PropTypeDefaultDescription
pixelIdstring(required)Your Facebook Pixel ID
autoConfigbooleantrueEnable automatic configuration of page-level events
debugbooleanfalseLog pixel calls to the console
advancedMatchingRecord<string, unknown>{}Advanced matching parameters (e.g. { em: 'user@example.com' })
lazybooleanfalseDelay loading the pixel script until init() is called manually

Tracking Events

Once the provider is in place, use the usePixel hook in any child component to fire events.

import { usePixel } from 'react-facebook';

function CheckoutButton() {
  const { track } = usePixel();

  return <button onClick={() => track('Purchase', { value: 29.99, currency: 'USD' })}>Complete Purchase</button>;
}

Standard Events

These are the standard pixel events recognized by Facebook. Pass the event name as the first argument to track().

Event NameWhen to use
PageViewA page is loaded (use usePageView for automatic tracking)
ViewContentA key page is viewed (product page, article, etc.)
SearchA search is performed
AddToCartAn item is added to a shopping cart
AddToWishlistAn item is added to a wishlist
InitiateCheckoutCheckout flow is started
AddPaymentInfoPayment information is entered
PurchaseA purchase is completed
LeadA lead form is submitted
CompleteRegistrationA registration form is completed

Additional standard events are also supported: Contact, CustomizeProduct, Donate, FindLocation, Schedule, StartTrial, SubmitApplication, and Subscribe.

For events not in this list, use trackCustom() instead.

The pixel script is loaded asynchronously. All tracking calls are safe to make immediately -- they will wait for the script to finish loading before firing.

Next Steps

On this page