Unverified Commit 29bd0980 authored by Nicolás Figueroa's avatar Nicolás Figueroa Committed by GitHub

with-facebook-pixel: new implementation with app folder (#49880)

### What?

* **Facebook pixel implementation with the new `app folder` structure.**
* The pixel script can now be kept in the public folder.
* The Facebook script can be placed in the `layout.js` file
* A component called `FacebookPixel` is provided to handle the initial script setup and manage `PageView` events when the page changes.
* Classic implementation with `pages` folder now is in `_pages`

### Why?
To provide an example of implementing the new `app folder` structure with Next.js ˆ13, which includes using the new files like `layout`, `page`, etc.

### How?

Utilizing the `next/script` package and the `layout.js` file to initialize the Facebook pixel when the application starts and manage subsequent events.

### Evidence of Implementation:

Here are some examples that demonstrate the successful implementation of the Facebook pixel using the new `app folder` structure and `Next.js ˆ13`


Tracking main page `app/page.js`
<img width="399" alt="image" src="https://github.com/vercel/next.js/assets/26902419/fd3baee8-363d-4f2c-b949-517409ad36de">

<img width="2105" alt="image" src="https://github.com/vercel/next.js/assets/26902419/7d91d786-cd31-4c21-bb79-7e9a78fb1c3b">

Tracking another path `app/about/page.js`
<img width="333" alt="image" src="https://github.com/vercel/next.js/assets/26902419/dda7bc05-6fb5-482d-8a6a-d981a08c66e1">

<img width="2102" alt="image" src="https://github.com/vercel/next.js/assets/26902419/b0392d6e-31fe-42d1-a7b4-ca38879dde94">

navigation between pages with `next/link`
<img width="2098" alt="image" src="https://github.com/vercel/next.js/assets/26902419/21115bee-a77f-4467-bb25-c95e2850cda2">
parent c59c39a4
import Link from 'next/link'
export default function About() {
return (
<>
<h1>About</h1>
<Link href="/">back to home</Link>
</>
)
}
'use client'
import { usePathname } from 'next/navigation'
import Script from 'next/script'
import { useEffect, useState } from 'react'
import * as pixel from '/lib/fpixel'
const FacebookPixel = () => {
const [loaded, setLoaded] = useState(false)
const pathname = usePathname()
useEffect(() => {
if (!loaded) return
pixel.pageview()
}, [pathname, loaded])
return (
<div>
<Script
id="fb-pixel"
src="/scripts/pixel.js"
strategy="afterInteractive"
onLoad={() => setLoaded(true)}
data-pixel-id={pixel.FB_PIXEL_ID}
/>
</div>
)
}
export default FacebookPixel
export { default as FacebookPixel } from './FacebookPixel'
import { FacebookPixel } from './components'
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
{children}
<FacebookPixel />
</body>
</html>
)
}
import Link from 'next/link'
export default function Home() {
return (
<>
<h2>
Go to `app/layout.js` to see how to implement Facebook Pixel in Next.js
13 with the App Router
</h2>
<h2>If you want to see old implementation, go to `_pages/index.js`</h2>
<Link href="/about">About page</Link>
</>
)
}
Implementation for Next 13+ using the App Router.
\ No newline at end of file
const PIXEL_ID = document.currentScript.getAttribute('data-pixel-id')
function initializeFacebookPixel(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)
}
initializeFacebookPixel(
window,
document,
'script',
'https://connect.facebook.net/en_US/fbevents.js'
)
window.fbq('init', PIXEL_ID)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment