Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions packages/core/src/components/MetaPixel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
'use client'

import Script from 'next/script'

export interface MetaPixelProps {
/** Meta (Facebook) Pixel ID (e.g., '1234567890123456') */
pixelId: string
/** Load strategy (default: 'afterInteractive') */
strategy?: 'afterInteractive' | 'lazyOnload' | 'beforeInteractive'
/** Disable automatic PageView tracking (default: false) */
disablePageView?: boolean
}

const PIXEL_ID_PATTERN = /^\d{15,16}$/

export function MetaPixel({
pixelId,
strategy = 'afterInteractive',
disablePageView = false
}: MetaPixelProps) {
// Don't render if no pixel ID or invalid format
if (!pixelId || !PIXEL_ID_PATTERN.test(pixelId)) {
return null
}

const pageViewScript = disablePageView ? '' : `fbq('track', 'PageView');`

return (
<>
<Script id="meta-pixel-init" strategy={strategy}>
{`
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '${pixelId}');
${pageViewScript}
`}
</Script>
{!disablePageView && (
<noscript>
<img
height="1"
width="1"
style={{ display: 'none' }}
src={`https://www.facebook.com/tr?id=${pixelId}&ev=PageView&noscript=1`}
alt=""
/>
</noscript>
)}
</>
)
}

/**
* Track a standard Meta Pixel event
* Use this for Facebook's predefined standard events
*
* @see https://developers.facebook.com/docs/meta-pixel/reference#standard-events
*
* @example
* trackMetaEvent('Lead')
* trackMetaEvent('Purchase', { value: 99.99, currency: 'USD' })
* trackMetaEvent('AddToCart', { content_ids: ['SKU123'], value: 29.99 })
*/
Comment on lines +59 to +69
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
export function trackMetaEvent(
eventName: string,
params?: Record<string, unknown>
) {
if (typeof window !== 'undefined' && typeof window.fbq === 'function') {
window.fbq('track', eventName, params)
}
}

/**
* Track a custom Meta Pixel event
* Use this for your own custom events (not Facebook's standard events)
*
* @see https://developers.facebook.com/docs/meta-pixel/reference#custom-events
*
* @example
* trackMetaCustomEvent('StartTrial')
* trackMetaCustomEvent('ShareContent', { content_type: 'article' })
*/
export function trackMetaCustomEvent(
eventName: string,
params?: Record<string, unknown>
) {
if (typeof window !== 'undefined' && typeof window.fbq === 'function') {
window.fbq('trackCustom', eventName, params)
}
}
Comment on lines +70 to +96
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)).

Copilot uses AI. Check for mistakes.

// Extend Window interface for TypeScript
declare global {
interface Window {
fbq: {
(action: 'init', pixelId: string, options?: Record<string, unknown>): void
(
action: 'track' | 'trackCustom',
eventName: string,
params?: Record<string, unknown>
): void
(action: string, ...args: unknown[]): void
}
}
}
1 change: 1 addition & 0 deletions packages/core/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './BasicPageLayout'
export * from './GoogleAnalytics'
export * from './MetaPixel'
export * from './ScrollToTop'