React Facebook Pixel
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
-
ReferenceError: window is not defined
cause The library directly references the global `window` object, which is not available in Node.js environments (e.g., server-side rendering in Next.js, Gatsby, or during build processes in CI).fixWrap `ReactPixel` calls in `if (typeof window !== 'undefined') { ... }` blocks. For Next.js, dynamic imports with `ssr: false` are often the most robust solution. When using CommonJS `require()`, use `require('react-facebook-pixel').default`. -
TypeError: src_default(...).track is not a function
cause This error typically occurs when using CommonJS `require('react-facebook-pixel')` without explicitly accessing the `.default` export, especially in bundled environments or when the module's export structure is misunderstood.fixEnsure you are using `const ReactPixel = require('react-facebook-pixel').default;` when importing via CommonJS. If using ES Modules, `import ReactPixel from 'react-facebook-pixel';` is correct. -
Property 'fbq' does not exist on type 'Window'
cause When directly interacting with `window.fbq` in TypeScript without proper global type declarations, the TypeScript compiler will report this error.fixAdd a global type declaration for `window.fbq` in your project (e.g., in a `global.d.ts` file) or use the `react-facebook-pixel` library's provided API which should have types. Example: `declare global { interface Window { fbq?: FacebookPixelFunction; } }`.
Warnings
- gotcha The library directly accesses the `window` object, which causes `ReferenceError: window is not defined` errors during server-side rendering (SSR) or in Node.js environments like Next.js or during CI builds.
- gotcha To ensure GDPR compliance, explicit consent management is required. The library provides `revokeConsent()` and `grantConsent()` methods, which developers must integrate into their UI/UX flow.
- deprecated The `react-facebook-pixel` package has been effectively abandoned, with its last npm publication over five years ago (December 2020). This means it is unlikely to receive updates for new Facebook Pixel API changes, security vulnerabilities, or compatibility fixes for newer React versions.
Install
-
npm install react-facebook-pixel -
yarn add react-facebook-pixel -
pnpm add react-facebook-pixel
Imports
- ReactPixel
import ReactPixel from 'react-facebook-pixel';
- ReactPixel
const ReactPixel = require('react-facebook-pixel');const ReactPixel = require('react-facebook-pixel').default; - typeof ReactPixel
import type ReactPixelType from 'react-facebook-pixel';
Quickstart
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;