React Facebook Pixel

1.0.4 · abandoned · verified Sun Apr 19

React Facebook Pixel is a wrapper library for integrating Facebook's Pixel (now Meta Pixel) into React applications. It provides a straightforward API for initializing the pixel, tracking page views, and logging standard or custom events. The current stable version is 1.0.4, but the package has not seen any new releases to npm in over five years, with its last publication in December 2020. This indicates an abandoned maintenance cadence, suggesting potential compatibility issues with newer React versions, updated Facebook Pixel APIs, or modern web development practices like server-side rendering (SSR). Differentiators from actively maintained alternatives (like `react-facebook` or `react-use-facebook-pixel`) include its simplified, direct API for basic pixel functionalities, though it lacks modern React features such as hooks or built-in SSR safety measures.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Facebook Pixel, track a page view on component mount, and trigger a custom 'Purchase' event. It also includes the crucial `typeof window !== 'undefined'` check for SSR compatibility and shows how to use the GDPR consent features.

import ReactPixel from 'react-facebook-pixel';
import { useEffect } from 'react';

const advancedMatching = { em: 'some@email.com' }; // Optional for advanced matching
const options = {
  autoConfig: true, // Enables pixel's autoConfig
  debug: false, // Disables logs
};

function App() {
  useEffect(() => {
    // Ensure window is defined before initializing (for SSR compatibility)
    if (typeof window !== 'undefined') {
      ReactPixel.init('yourPixelIdGoesHere', advancedMatching, options);
      ReactPixel.pageView(); // Track initial page view
    }
  }, []);

  const handleProductPurchase = (productId, price) => {
    if (typeof window !== 'undefined') {
      ReactPixel.track('Purchase', { value: price, currency: 'USD', content_ids: [productId], content_type: 'product' });
    }
  };

  return (
    <div>
      <h1>My React App</h1>
      <p>Welcome to the homepage. We track page views.</p>
      <button onClick={() => handleProductPurchase('product123', 99.99)}>
        Buy Product 123
      </button>
      <p>Please remember to handle GDPR consent explicitly.</p>
      <button onClick={ReactPixel.grantConsent}>Accept Cookies</button>
    </div>
  );
}

export default App;

view raw JSON →