feat(components): add MetaPixel component with event tracking#5
feat(components): add MetaPixel component with event tracking#5
Conversation
- Add MetaPixel component for easy Meta (Facebook) Pixel integration - Parameterized pixelId prop for different sites - Include trackMetaEvent() and trackMetaCustomEvent() helpers - Support all standard Meta Pixel events (Lead, Purchase, etc.) - Follow same patterns as GoogleAnalytics component - TypeScript support with proper Window interface extension
There was a problem hiding this comment.
Pull request overview
This PR adds a Meta Pixel (Facebook Pixel) component for Next.js applications, following the same patterns as the existing GoogleAnalytics component. It provides a declarative way to integrate Facebook Pixel tracking with proper TypeScript support and event tracking helpers.
Changes:
- Added
MetaPixelcomponent with configurable pixel ID, load strategy, and PageView tracking options - Exported tracking helper function
trackMetaEvent()for programmatic event tracking - Extended Window interface with TypeScript definitions for the
fbqfunction
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| packages/core/src/components/index.ts | Exports the new MetaPixel component |
| packages/core/src/components/MetaPixel.tsx | Implements the MetaPixel component with initialization script, noscript fallback, event tracking helper, and TypeScript types |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| fbq: ( | ||
| action: string, | ||
| eventName: string, | ||
| params?: Record<string, unknown> | ||
| ) => void |
There was a problem hiding this comment.
The TypeScript Window interface extension for fbq is incomplete. The Facebook Pixel fbq function supports multiple actions ('init', 'track', 'trackCustom', etc.), but the current type definition only supports 'track'. This will cause TypeScript errors when using fbq('init', pixelId) as seen on line 40. The function signature should be updated to support multiple overloads or use a more flexible type that accommodates all fbq actions.
| fbq: ( | |
| action: string, | |
| eventName: string, | |
| params?: Record<string, unknown> | |
| ) => void | |
| fbq( | |
| action: 'init', | |
| pixelId: string, | |
| options?: Record<string, unknown> | |
| ): void | |
| fbq( | |
| action: 'track' | 'trackCustom', | |
| eventName: string, | |
| params?: Record<string, unknown> | |
| ): void | |
| fbq(action: string, ...args: unknown[]): void |
| export function trackMetaEvent( | ||
| eventName: string, | ||
| params?: Record<string, unknown> | ||
| ) { | ||
| if (typeof window !== 'undefined' && typeof window.fbq === 'function') { | ||
| if (params) { | ||
| window.fbq('track', eventName, params) | ||
| } else { | ||
| window.fbq('track', eventName) | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The PR description mentions a trackMetaCustomEvent() helper function, but this function does not exist in the code. Only trackMetaEvent() is implemented. Either the PR description should be updated to remove this reference, or the function should be added to match the documentation. Note that Facebook Pixel distinguishes between standard events (using fbq('track', eventName)) and custom events (using fbq('trackCustom', eventName)).
| /** | ||
| * Track a Meta Pixel event | ||
| * Call this function to track custom events after the pixel is loaded | ||
| * | ||
| * @example | ||
| * // Standard event | ||
| * trackMetaEvent('Lead') | ||
| * trackMetaEvent('Purchase', { value: 99.99, currency: 'USD' }) | ||
| * | ||
| * // Custom event | ||
| * trackMetaEvent('CustomEvent', { custom_param: 'value' }) | ||
| */ |
There was a problem hiding this comment.
The documentation comment states this function can be used for custom events (line 66-67), but the implementation only uses fbq('track', ...) which is for standard events. Custom events in Facebook Pixel should use fbq('trackCustom', eventName, params). This could lead to confusion or incorrect event tracking. Consider either updating the implementation to handle custom events differently or clarifying in the documentation that this function is for standard events only.
| if (params) { | ||
| window.fbq('track', eventName, params) | ||
| } else { | ||
| window.fbq('track', eventName) | ||
| } |
There was a problem hiding this comment.
The conditional logic to check if params exists is unnecessary. Facebook Pixel's fbq function accepts an optional third parameter, so window.fbq('track', eventName, params) can be called even when params is undefined. This simplifies the code and removes the redundant if-else branch.
| if (params) { | |
| window.fbq('track', eventName, params) | |
| } else { | |
| window.fbq('track', eventName) | |
| } | |
| window.fbq('track', eventName, params) |
| <noscript> | ||
| <img | ||
| height="1" | ||
| width="1" | ||
| style={{ display: 'none' }} | ||
| src={`https://www.facebook.com/tr?id=${pixelId}&ev=PageView&noscript=1`} | ||
| alt="" | ||
| /> | ||
| </noscript> |
There was a problem hiding this comment.
When disablePageView is true, the JavaScript code skips PageView tracking, but the noscript fallback image still includes 'ev=PageView' in the URL. This creates inconsistent behavior between JavaScript and non-JavaScript environments. The noscript fallback should respect the disablePageView prop and conditionally render based on its value, or omit the 'ev' parameter when PageView tracking is disabled.
| <noscript> | |
| <img | |
| height="1" | |
| width="1" | |
| style={{ display: 'none' }} | |
| src={`https://www.facebook.com/tr?id=${pixelId}&ev=PageView&noscript=1`} | |
| alt="" | |
| /> | |
| </noscript> | |
| {!disablePageView && ( | |
| <noscript> | |
| <img | |
| height="1" | |
| width="1" | |
| style={{ display: 'none' }} | |
| src={`https://www.facebook.com/tr?id=${pixelId}&ev=PageView&noscript=1`} | |
| alt="" | |
| /> | |
| </noscript> | |
| )} |
- Add trackMetaCustomEvent() for custom events (uses fbq('trackCustom'))
- Fix TypeScript Window.fbq types with proper overloads
- Simplify trackMetaEvent() - remove unnecessary conditional
- Respect disablePageView in noscript fallback
- Update JSDoc to clarify standard vs custom events
Summary
Adds a reusable Meta Pixel (Facebook Pixel) component for Next.js sites.
Features
MetaPixelcomponent with parameterizedpixelIdproptrackMetaEvent()helper for standard events (Lead, Purchase, PageView, etc.)trackMetaCustomEvent()helper for custom eventsGoogleAnalyticscomponentUsage