-
Notifications
You must be signed in to change notification settings - Fork 1
Install the Meta pixel #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| "use client"; | ||
|
|
||
| import Script from "next/script"; | ||
| import { usePathname, useSearchParams } from "next/navigation"; | ||
| import { Suspense, useEffect, useRef } from "react"; | ||
|
|
||
| /* Meta (Facebook) pixel. The base snippet loads fbevents.js and reports the | ||
| initial page load with fbq('track', 'PageView'); conversions are reported | ||
| with fbq('track', '<StandardEvent>', {...}) from wherever they happen. | ||
|
|
||
| fbevents.js installs no history listener, so App Router navigations would | ||
| otherwise go unreported. Each route change re-fires the PageView track | ||
| (safe to repeat, and queues before the script finishes loading). | ||
|
|
||
| Production only, so localhost and preview deployments stay out of the ad | ||
| data. To check the pixel locally, temporarily drop the NODE_ENV guard. */ | ||
|
|
||
| const META_PIXEL_ID = "1782353696227901"; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| fbq?: (...args: unknown[]) => void; | ||
| } | ||
| } | ||
|
|
||
| function PageViewTracker() { | ||
| const pathname = usePathname(); | ||
| const searchParams = useSearchParams(); | ||
| // last path reported; doubles as the initial-load marker so the first | ||
| // render (already tracked by the inline snippet) and strict-mode effect | ||
| // re-runs don't double-count | ||
| const lastPath = useRef<string | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| const query = searchParams?.toString(); | ||
| const path = query ? `${pathname}?${query}` : pathname; | ||
|
|
||
| if (lastPath.current === null) { | ||
| lastPath.current = path; | ||
| return; | ||
| } | ||
| if (lastPath.current === path) return; | ||
| lastPath.current = path; | ||
|
|
||
| window.fbq?.("track", "PageView"); | ||
| }, [pathname, searchParams]); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| export function MetaPixel() { | ||
| if (process.env.NODE_ENV !== "production") return null; | ||
|
|
||
| return ( | ||
| <> | ||
| <Script id="meta-pixel" strategy="afterInteractive"> | ||
| {` | ||
| !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', '${META_PIXEL_ID}'); | ||
| fbq('track', 'PageView'); | ||
| `} | ||
| </Script> | ||
| <noscript> | ||
| {/* eslint-disable-next-line @next/next/no-img-element */} | ||
| <img | ||
| height="1" | ||
| width="1" | ||
| style={{ display: "none" }} | ||
| alt="" | ||
| src={`https://www.facebook.com/tr?id=${META_PIXEL_ID}&ev=PageView&noscript=1`} | ||
| /> | ||
| </noscript> | ||
| {/* useSearchParams requires a Suspense boundary in the App Router */} | ||
| <Suspense fallback={null}> | ||
| <PageViewTracker /> | ||
| </Suspense> | ||
| </> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the deployment applies
public/_headers, its CSP blocksconnect.facebook.netfrom loadingfbevents.jsand blocks thewww.facebook.comfallback image, causing the new pixel to report no PageView events.Prompt To Fix With AI