Facebook Conversion API for Next.js
npm install @tapstack/facebook-conversion-api-nextjsFacebook / Meta Conversion API (CAPI) for Next.js
> Next.js wrapper for Facebook's Conversion API
Tested with Next.js 14, 15 and the latest canary.
It includes an API route handler for sending server-side events to Facebook and client-side functions to trigger the events.
Facebook recommends sending events with Facebook Pixel and the Conversion API with the same event id to match duplicated events.
Therefore, we have added the option to enable standard Facebook Pixel events in this package, where we handle duplicated events out of the box.
Support Next.js API routes on both Vercel and AWS Amplify.
NPM
``bash`
npm install @tapstack/facebook-conversion-api-nextjsalso install its peer dependency for API calls
npm install @tapstack/facebook-conversion-api
Yarn
`bash`
yarn add @tapstack/facebook-conversion-api-nextjs
jsx
import { fbEventsHandler } from '@tapstack/facebook-conversion-api-nextjs/handlers';export default fbEventsHandler;
`$3
.env
`dotenv
FB_ACCESS_TOKEN=accessToken
NEXT_PUBLIC_FB_PIXEL_ID=pixelId
NEXT_PUBLIC_FB_DEBUG=true # optional
`Read more here on how you can get your access token and pixel id.
2. Load Facebook Pixel (Optional)
This is only needed if you want to fire standard Pixel Events.$3
pages/_app.js
`jsx
import { FBPixelScript, FBPixelProvider } from '@tapstack/facebook-conversion-api-nextjs/components';...
<>
>
...
`3. Start Sending Events
Trigger the events you need. For example, add to cart or purchase events.
`jsx
import { fbEvent } from '@tapstack/facebook-conversion-api-nextjs';useEffect(() => {
fbEvent({
eventName: 'ViewContent', // ViewContent, AddToCart, InitiateCheckout, Purchase etc.
eventId: 'eventId', // optional, unique event id's will be generated by default
emails: ['email1', 'email2'], // optional
phones: ['phone1', 'phone2'], // optional
firstName: 'firstName', // optional
lastName: 'lastName', // optional
country: 'country', // optional
city: 'city', // optional
zipCode: 'zipCode', // optional
products: [{ // optional
sku: 'product123',
quantity: 1,
}],
value: 1000, // optional
currency: 'USD', // optional
enableStandardPixel: false // default false (Require Facebook Pixel to be loaded, see step 2)
});
}, []);
``