Meta Pixel Capi: Install, Source and Security | FunnelSlayer

Meta Pixel Capi

Published by aammo1 in sk

No known issues18 installs

What this skill does

Implement Meta (Facebook) Pixel tracking with Conversions API (CAPI) for dual-channel event deduplication. Covers browser pixel setup, server-side CAPI integration, eventID deduplication architecture, standard event mapping, PII hashing, and debugging. Use when the user is adding Meta Pixel tracking, setting up CAPI, implementing event tracking for ads, debugging pixel/CAPI issues, or mentions Meta Pixel, Facebook Pixel, Conversions API, fbq, pixel tracking, or ad conversion tracking.

Add Meta Pixel Capi to your agent

Review the source and files first. When you are ready, copy the prompt instruction or use the CLI command supported by your environment.

Install with a prompt

Paste this into a compatible coding agent:

add this skill "meta-pixel-capi" from https://github.com/aammo1/sk

Install with the CLI

Run this command in a controlled environment after reviewing the repository:

npx skills add https://github.com/aammo1/sk --skill meta-pixel-capi

Skill instructions

Meta Pixel + CAPI Implementation

When to Use

Use this skill when the user:

  • Is implementing Meta Pixel tracking on a website (any framework)
  • Is setting up server-side Conversions API (CAPI) for deduplication
  • Is debugging Pixel or CAPI issues (events not firing, duplicates, missing data)
  • Mentions Meta Pixel, Facebook Pixel, fbq, CAPI, or conversion tracking
  • Needs to add standard events (PageView, ViewContent, AddToCart, Purchase, etc.)
  • Is implementing e-commerce tracking with Meta

Architecture: Dual Channel Deduplication

Every tracked event fires through both channels with the same eventID:

User action
  ├─ Browser: fbq('track', 'EventName', {params}, {eventID: 'uuid'})
  └─ Server:  POST /api/pixel → Meta Graph API /events {event_id: 'uuid', ...}
                                         ↑
              Meta matches by (event_name + event_id) → deduplicates

The eventID must be:

  • A unique UUID (use crypto.randomUUID() on frontend)
  • Identical on both browser and server channels for the same event
  • Passed as 4th argument to fbq() (NOT inside params — see Bug 2)
  • Sent as event_id in the CAPI payload

Critical Bugs to Avoid

Bug 1: CAPI endpoint URL mismatch

Frontend calls ${BASE_URL}/pixel but backend route is at /api/pixel. Always verify the full constructed URL matches the backend route. Check how the base URL is used across the app before constructing API URLs.

Rule: Open DevTools → Network tab and verify the exact URL the frontend requests. Match it to the backend route definition.

Bug 2: eventID in wrong position for fbq()

Meta ignores eventID when it's inside the params object. It MUST be the 4th argument:

// CORRECT — eventID as 4th argument
fbq('track', 'Purchase', { value: 100, currency: 'USD' }, { eventID: 'uuid' })

// WRONG — eventID inside params, Meta ignores it
fbq('track', 'Purchase', { value: 100, currency: 'USD', eventID: 'uuid' })

Bug 3: Access token not scoped to the pixel

Tokens generated from Business Settings → System Users do NOT have pixel-level permissions. Error: Object with ID 'xxx' does not exist.

Fix: Generate the token from Events Manager → select pixel → Settings → Conversions API → Generate Access Token. This ensures the token is scoped to the exact pixel with events endpoint permission.

Bug 4: Missing user data in CAPI events

Error: error_subcode: 2804050 — "You haven't added sufficient customer information parameter data."

Every CAPI event MUST include at minimum:

  • client_ip_address
  • client_user_agent

Purchase events should additionally include hashed ph (phone), fn (first name), ln (last name), ct (city) for better match rates.

Bug 5: Duplicate PageView from inline script

Adding fbq('track', 'PageView') after fbq('init') in the inline script fires a PageView without eventID. If your app also fires PageView via useEffect (with eventID), you get duplicate PageViews where the inline one cannot be deduplicated.

Fix: Only call fbq('init') in the inline script. Let your app's trackPageView() function handle all PageViews with eventID. The fbq stub correctly queues and replays all 4 arguments including {eventID} — timing is not an issue.

Note: Facebook's Test Events panel often does NOT display event_id for browser PageView events, even when correctly sent. This is a known platform quirk. Verify by checking DevTools → Network → filter tr? → inspect the eid parameter in the request payload.

Bug 6: Using trackCustom for standard events

fbq('trackCustom', 'PageView', ...) sends PageView as a custom event — Meta handles standard and custom events differently. Custom events don't appear in the standard Events dashboard, lose eid parameter intermittently, and deduplication with CAPI fails.

// CORRECT
fbq('track', 'PageView', params, { eventID })         // Standard event → 'track'
fbq('track', 'ViewContent', params, { eventID })       // Standard event → 'track'
fbq('trackCustom', 'SignUpCompleted', params, { eventID }) // Custom event → 'trackCustom'

// WRONG
fbq('trackCustom', 'PageView', params, { eventID })    // Standard event with trackCustom!

Rule: Use 'track' for all Meta standard events. Only use 'trackCustom' for events NOT in Meta's standard event list.

Bug 7: Over-engineering PageView timing

Using waitForFbq() polling, onLoad callbacks, or useState(pixelReady) to delay PageView until the pixel script loads causes PageView to not fire at all or fire too late. onLoad doesn't work with dangerouslySetInnerHTML.

Fix: Use the simplest approach — useRef(firstRender) with useEffect([pathname]). The fbq stub queues calls and correctly replays them with all 4 arguments. Do NOT wait for the script to load.

const firstRender = useRef(true);
const previousPathname = useRef(pathname);

useEffect(() => {
  trackPageView(url, title);  // fbq stub queues this, replays when real fbq loads
  if (firstRender.current) {
    firstRender.current = false;
    return;
  }
  if (previousPathname.current !== pathname) {
    previousPathname.current = pathname;
    trackPageView(url, title);
  }
}, [pathname]);

Bug 8: Redundant fbc/fbp in trackPurchase

Don't pass fbc/fbp as userData to sendCapiEvent() — it already fetches them internally. If the caller passes them via spread and the cookies don't exist, undefined overwrites the internally-fetched values, removing them from the payload. fbc/fbp improve match quality but are not required.

fbq() Call Format

Argument positions (MUST follow exactly)

fbq('track', 'EventName', { params }, { eventID: 'uuid' })
//   arg1       arg2        arg3         arg4 (dedup info object)

Standard vs Custom

Event typeCommandExamples
Standard'track'PageView, ViewContent, AddToCart, InitiateCheckout, Purchase, Lead, Search, Contact, SubmitApplication
Custom'trackCustom'Any event name NOT in Meta's standard list

Using 'trackCustom' for standard events causes: eventID/eid lost intermittently, events missing from standard dashboard, CAPI deduplication breaks.

How the fbq queue works

The Meta pixel snippet creates a stub that queues calls before the real script loads. This queue correctly preserves all 4 arguments including {eventID}. When the real fbq loads, it replays the queue. The only thing that breaks eventID is putting it in the wrong position (Bug 2), not timing.

CAPI Event Requirements

Minimum required fields (every event)

FieldSourceRequired
event_nameFixed stringYes
event_timeUnix timestamp (seconds)Yes
event_idUUID (same as browser event)Yes
action_source"website"Yes
event_source_urlCurrent page URLRecommended
client_ip_addressExtracted from request headersYes (or CAPI fails)
client_user_agentExtracted from request headersYes (or CAPI fails)

Additional fields for Purchase events (improves match rate)

FieldSourceHashing
phCustomer phoneSHA256, lowercase, trimmed
fnCustomer first nameSHA256, lowercase, trimmed
lnCustomer last nameSHA256, lowercase, trimmed
ctCustomer citySHA256, lowercase, trimmed
external_idPhone number (unique ID)SHA256, lowercase, trimmed
fbc_fbc cookie (click ID)Raw, no hashing
fbp_fbp cookie (browser ID)Raw, no hashing

Hashing rules

All PII must be SHA256-hashed before sending to Meta:

import hashlib

def sha256(value: str) -> str:
    return hashlib.sha256(value.strip().lower().encode("utf-8")).hexdigest()
async function sha256(value: string): Promise<string> {
  const msgBuffer = new TextEncoder().encode(value.trim().toLowerCase());
  const hashBuffer = await crypto.subtle.digest("SHA-256", msgBuffer);
  return Array.from(new Uint8Array(hashBuffer))
    .map((b) => b.toString(16).padStart(2, "0"))
    .join("");
}
  • Lowercase + trim BEFORE hashing
  • fbc and fbp are NOT hashed (already Meta-generated IDs)

Standard Events Reference

EventBrowserCAPIParametersTrigger
PageViewYesYespage_title, page_locationEvery page navigation
ViewContentYesYescontent_ids, content_name, content_type, currency, valueProduct/details page
AddToCartYesYescontent_ids, content_type, currency, value, num_itemsAdd to cart
InitiateCheckoutYesYescontent_ids, contents, content_type, currency, value, num_itemsCheckout start
PurchaseYesYescontent_ids, contents, content_type, currency, value, num_itemsOrder confirmation

Parameter format

content_ids: ['product-slug-1']    // Array of strings (product IDs/slugs)
content_type: 'product'            // Always 'product' for e-commerce
currency: 'USD'                    // ISO 4217
value: 29.99                       // Number (total price)
num_items: 2                       // Number
contents: [{ id: 'variant-1', quantity: 1, item_price: 29.99 }]  // Array for Purchase

Environment Variables

Frontend

VariableNotes
NEXT_PUBLIC_FACEBOOK_PIXEL_IDMust be in BOTH build args and runtime env vars for Next.js

For Next.js Docker deployments, declare in Dockerfile:

ARG NEXT_PUBLIC_FACEBOOK_PIXEL_ID
ENV NEXT_PUBLIC_FACEBOOK_PIXEL_ID=$NEXT_PUBLIC_FACEBOOK_PIXEL_ID

Backend

VariableNotes
FACEBOOK_PIXEL_IDSame pixel ID as frontend
FACEBOOK_ACCESS_TOKENFrom Events Manager → Settings → Conversions API (NOT Business Settings)
FACEBOOK_TEST_EVENT_CODESet during testing, remove in production
FACEBOOK_API_VERSIONDefaults to v22.0

Testing Flow

1. Set test event code

Add FACEBOOK_TEST_EVENT_CODE=TEST12345 to backend env vars. Events go to the Test Events tab (not production data).

2. Hit the test endpoint

Create a test endpoint that sends a CAPI event and returns the full response. Expected:

{
  "pixel_id": "YOUR_PIXEL_ID",
  "api_version": "v22.0",
  "result": { "success": true, "data": { "events_received": 1 } }
}

3. Check Meta Test Events tab

Events Manager → pixel → Test Events. You should see:

  • Browser events (connection: "Browser") — should have eventID
  • Server events (connection: "Server") — should have event_id
  • Both with matching IDs for deduplication

4. Verify eventID in browser events

DevTools → Network → filter tr? → check payload for eid parameter. It must match the server event's event_id. If eid is missing: check that eventID is the 4th argument to fbq() (Bug 2) and that you're using 'track' not 'trackCustom' for standard events (Bug 6).

5. Remove test code for production

Delete FACEBOOK_TEST_EVENT_CODE or set to empty string when done.

CAPI Error Codes

CodeSubcodeMeaningFix
10033Object doesn't exist / missing permissionsWrong pixel ID or token not scoped. Regenerate from Events Manager → Settings → Conversions API
1002804050Insufficient customer infoAdd client_ip_address + client_user_agent at minimum
100—Invalid parameterCheck parameter format (arrays, numbers, etc.)
190—Invalid access tokenToken expired/wrong. Regenerate from Events Manager

Debugging Checklist

  • NEXT_PUBLIC_FACEBOOK_PIXEL_ID set in both build args AND runtime env vars
  • FACEBOOK_PIXEL_ID and FACEBOOK_ACCESS_TOKEN set in backend env vars
  • Access token from Events Manager → Settings → Conversions API (NOT Business Settings → System Users)
  • CAPI endpoint URL matches backend route exactly (verify in DevTools Network tab)
  • eventID is 4th argument to fbq(): fbq('track', 'Event', params, {eventID})
  • Using 'track' not 'trackCustom' for all standard events (PageView, ViewContent, etc.)
  • event_id in CAPI payload matches browser event's eventID
  • client_ip_address and client_user_agent included in every CAPI event
  • PII fields (ph, fn, ln, ct) are SHA256-hashed, trimmed, lowercased
  • fbc/fbp cookies sent from browser to backend (optional but improves matching)
  • Test endpoint returns events_received: 1
  • DevTools Network tab → filter tr? → eid parameter present in browser events
  • FACEBOOK_TEST_EVENT_CODE removed for production
  • CORS allows frontend domain for the CAPI endpoint
  • Inline pixel script has ONLY fbq('init') — no fbq('track', 'PageView') after it (Bug 5)
  • PageView uses simple useRef(firstRender) pattern, NOT waitForFbq or useState(pixelReady) (Bug 7)
  • undefined values stripped from params before sending to fbq() and CAPI
  • PageView eventID verified via DevTools Network → tr? → eid (NOT Test Events panel, which hides it sometimes)

Files included

  • SKILL.md

More skills