Skip to content
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

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

Merged
merged 12 commits into from
Jun 15, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/with-facebook-pixel/app/about/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Link from 'next/link'

export default function About() {
return (
<>
<h1>About</h1>

<Link href="/">back to home</Link>
</>
)
}
31 changes: 31 additions & 0 deletions examples/with-facebook-pixel/app/components/FacebookPixel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'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
1 change: 1 addition & 0 deletions examples/with-facebook-pixel/app/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as FacebookPixel } from './FacebookPixel'
12 changes: 12 additions & 0 deletions examples/with-facebook-pixel/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FacebookPixel } from './components'

export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
{children}
<FacebookPixel />
</body>
</html>
)
}
14 changes: 14 additions & 0 deletions examples/with-facebook-pixel/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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>
</>
)
}
1 change: 1 addition & 0 deletions examples/with-facebook-pixel/app/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implementation for Next 13+ using the App Router.
27 changes: 27 additions & 0 deletions examples/with-facebook-pixel/public/scripts/pixel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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)