How to Install Meta Pixel in Next.js

By Nico
December 4, 2025·4 min read

Installing Meta Pixel in Next.js takes about 2 minutes: install the package, add the component to your layout, done. PageView tracking on route changes is automatic.

Prerequisites

Installation

npm install @adkit.so/meta-pixel-next

Setup

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>
    );
}

That's it! PageView is now tracked automatically on every route change.

Using Environment Variables

Instead of hardcoding your Pixel ID:

.env.local
NEXT_PUBLIC_META_PIXEL_ID=123456789012345
app/layout.tsx
<MetaPixel>{children}</MetaPixel> // Uses env var automatically

Tracking Events

Use the useMetaPixel hook in any client component:

components/checkout-button.tsx
'use client';

import { useMetaPixel } from '@adkit.so/meta-pixel-next';

export function CheckoutButton() {
    const meta = useMetaPixel();

    function handleClick() {
        meta.track('InitiateCheckout', { value: 99, currency: 'USD' });
    }

    return <button onClick={handleClick}>Checkout</button>;
}

Common Events

// Purchase completed
meta.track('Purchase', { value: 99.99, currency: 'USD' });

// Lead captured
meta.track('Lead');

// Content viewed
meta.track('ViewContent', { content_id: 'pricing-page' });

// Custom events
meta.trackCustom('VideoWatched', { video_id: 'intro' });

What Events to Track in Your SaaS

For a typical SaaS funnel, track these events:

EventWhen to Trigger
ViewContentOn page load of blogs, pricing or features page
LeadOn form submit (newsletter, lead magnet, waitlist)
CompleteRegistrationAfter successful account creation
StartTrialWhen user activates free trial
Subscribe / PurchaseOn subscription success page (use eventID for deduplication)

Focus on conversion events (Lead, StartTrial, Subscribe) — these are what you'll optimize your ads for.

Testing

Enable localhost tracking and debug mode:

app/layout.tsx
<MetaPixel pixelId="YOUR_PIXEL_ID" enableLocalhost={true} debug={true}>
    {children}
</MetaPixel>

Verify with the Meta Pixel Helper Chrome extension:

Meta Pixel Helper extension confirmation

Configuration Options

PropTypeDefaultDescription
pixelIdstring | string[]-Your Pixel ID(s) or use env var
debugbooleanfalseEnable console logging
enableLocalhostbooleanfalseEnable tracking on localhost
trackPageViewsbooleantrueAuto track PageView on route changes

Common Issues

Next Steps

  1. Set up conversion events in Events Manager
  2. Create custom audiences based on pixel data
  3. Use pixel events to optimize your ads

Resources

Share on:
Photo of Nico

Article by

Nico Jeannen

Hey! I'm the founder of AdKit. I've been doing ads for almost 10 years. I grew and sold my 2 previous startup using ads. Then I created AdKit to make ads accessible to everyone.