
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
@adkit.so/meta-pixel-next
Advanced tools
Next.js integration for Meta Pixel tracking - auto PageView on route changes
The simplest Meta Pixel integration for Next.js — with automatic PageView tracking on route changes.
Built on top of @adkit.so/meta-pixel-react, this package provides a drop-in component that handles everything automatically.
<MetaPixel />NEXT_PUBLIC_META_PIXEL_ID out of the boxnpm install @adkit.so/meta-pixel-next
Add <MetaPixel /> to your root layout:
// app/layout.tsx
import { MetaPixel } from '@adkit.so/meta-pixel-next';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<MetaPixel pixelId="YOUR_PIXEL_ID">{children}</MetaPixel>
</body>
</html>
);
}
Track events in any component:
'use client';
import { useMetaPixel } from '@adkit.so/meta-pixel-next';
export function PurchaseButton() {
const meta = useMetaPixel();
function handlePurchase() {
meta.track('Purchase', { value: 99.99, currency: 'USD' });
}
return <button onClick={handlePurchase}>Buy Now</button>;
}
That's it! 🎉
| Prop | Type | Default | Description |
|---|---|---|---|
pixelId | string | string[] | - | Your Meta Pixel ID(s). Falls back to NEXT_PUBLIC_META_PIXEL_ID env var |
debug | boolean | false | Enable styled console logging |
enableLocalhost | boolean | false | Enable tracking on localhost (for testing) |
autoTrackPageView | boolean | true | Auto track PageView on initial load and route changes |
Instead of passing pixelId as a prop, use an environment variable:
# .env.local
NEXT_PUBLIC_META_PIXEL_ID=123456789012345
// app/layout.tsx
import { MetaPixel } from '@adkit.so/meta-pixel-next';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<MetaPixel debug={true} enableLocalhost={true}>
{children}
</MetaPixel>
</body>
</html>
);
}
Track to multiple pixels simultaneously:
<MetaPixel pixelId={['PIXEL_ID_1', 'PIXEL_ID_2', 'PIXEL_ID_3']}>{children}</MetaPixel>
Or via environment variable (comma-separated):
NEXT_PUBLIC_META_PIXEL_ID=PIXEL_1,PIXEL_2
Use the useMetaPixel hook in any client component:
'use client';
import { useMetaPixel } from '@adkit.so/meta-pixel-next';
export function ProductPage({ product }) {
const meta = useMetaPixel();
function handleAddToCart() {
meta.track('AddToCart', {
content_ids: [product.id],
content_name: product.name,
value: product.price,
currency: 'USD',
});
}
function handlePurchase() {
meta.track('Purchase', {
content_ids: [product.id],
value: product.price,
currency: 'USD',
num_items: 1,
});
}
return (
<div>
<h1>{product.name}</h1>
<button onClick={handleAddToCart}>Add to Cart</button>
<button onClick={handlePurchase}>Buy Now</button>
</div>
);
}
Track custom events specific to your business:
const meta = useMetaPixel();
function trackPricingView() {
meta.trackCustom('PricingViewed', {
plan: 'enterprise',
source: 'homepage_cta',
});
}
function trackVideoComplete() {
meta.trackCustom('VideoWatched', {
video_id: 'intro_2024',
watch_percentage: 100,
});
}
Prevent duplicate events using eventID:
function handleCheckout(orderId: string) {
meta.track('Purchase', { value: 299.99, currency: 'USD' }, { eventID: `order-${orderId}` });
}
All Meta Pixel standard events are supported with full TypeScript autocomplete:
| Event | Description | Common Use Cases |
|---|---|---|
PageView | Auto-tracked on route changes | - |
ViewContent | Content/product viewed | Product pages |
Search | Search performed | Site search |
AddToCart | Item added to cart | E-commerce |
AddToWishlist | Item added to wishlist | E-commerce |
InitiateCheckout | Checkout started | Cart page |
AddPaymentInfo | Payment info added | Checkout flow |
Purchase | Purchase completed | Order confirmation |
Lead | Lead form submitted | Contact forms |
CompleteRegistration | Registration completed | Sign-up flows |
Contact | Contact action | Contact page |
Schedule | Appointment scheduled | Booking systems |
StartTrial | Trial started | SaaS apps |
Subscribe | Subscription started | Newsletters |
| Parameter | Type | Description | Example |
|---|---|---|---|
value | number | Monetary value | 99.99 |
currency | string | ISO 4217 currency code | 'USD', 'EUR' |
content_ids | string[] | Product IDs or SKUs | ['SKU_123'] |
content_type | string | Type of content | 'product' |
content_name | string | Name of product/page | 'Blue T-Shirt' |
content_category | string | Category | 'Apparel' |
num_items | number | Number of items | 3 |
search_string | string | Search query | 'running shoes' |
'use client';
import { useMetaPixel } from '@adkit.so/meta-pixel-next';
import { useEffect } from 'react';
export function ProductPage({ product }) {
const meta = useMetaPixel();
useEffect(() => {
// Track product view on page load
meta.track('ViewContent', {
content_ids: [product.id],
content_type: 'product',
content_name: product.name,
content_category: product.category,
value: product.price,
currency: 'USD',
});
}, [product.id]);
function handleAddToCart() {
meta.track('AddToCart', {
content_ids: [product.id],
content_type: 'product',
content_name: product.name,
value: product.price,
currency: 'USD',
});
}
function handlePurchase(orderId: string) {
meta.track(
'Purchase',
{
content_ids: [product.id],
content_type: 'product',
value: product.price,
currency: 'USD',
num_items: 1,
},
{ eventID: orderId }, // For deduplication
);
}
return (
<div>
<h1>{product.name}</h1>
<p>${product.price}</p>
<button onClick={handleAddToCart}>Add to Cart</button>
<button onClick={() => handlePurchase('order-123')}>Buy Now</button>
</div>
);
}
If you prefer to handle PageView tracking manually:
<MetaPixel pixelId="YOUR_PIXEL_ID" autoTrackPageView={false}>
{children}
</MetaPixel>
function trackIfReady() {
if (meta.isLoaded()) {
meta.track('Purchase', { value: 99.99, currency: 'USD' });
}
}
Full TypeScript support with exported types:
import type { StandardEvent, EventData, EventMetaData } from '@adkit.so/meta-pixel-next';
function trackEvent(event: StandardEvent, data: EventData) {
meta.track(event, data);
}
Enable debug={true} to see styled console logs:
[Meta Pixel] Initializing Meta Pixel... { pixelIds: [...] }
[Meta Pixel] ✓ Meta Pixel initialized successfully
[Meta Pixel] Tracking: PageView
[Meta Pixel Next.js] Auto-tracking PageView on route change: /about
debug={true} to see logsenableLocalhost={true} for local testingMake sure <MetaPixel /> wraps your content in the root layout, not a nested layout.
Use @adkit.so/meta-pixel-react instead. Note: auto PageView on route changes requires manual setup with React Router.
For a complete step-by-step guide on installing and configuring Meta Pixel, check out our detailed tutorial:
MIT
Made with ❤️ by Adkit
If this package helped you, please consider giving it a ⭐️ on GitHub!
FAQs
Next.js integration for Meta Pixel tracking - auto PageView on route changes
We found that @adkit.so/meta-pixel-next demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.