I know this may be a dumb question. How can I do to use facebook pixel on a next.js react app ?
What Is It: Next. js is an open-source front end web development JavaScript framework based on the popular Facebook-backed React.
js provides a zero-configuration setup process similar to what Create React App does through a package called Create Next App. Today, we will look at how you can make use of Next. js on a pre-existing React application. This will allow you to add SSR to an existing project.
there are no dumb questions.
You can see nextjs example about how implements fb pixel. Nextjs Facebook Pixel
Solution with typescript and hook with NextJs
yarn add react-facebook-pixel
_app.tsx
// pages/_app.tsx
import { useEffect } from 'react'
import { useRouter } from 'next/router'
const App = ({ Component, pageProps }) => {
const router = useRouter()
useEffect(() => {
import('react-facebook-pixel')
.then((x) => x.default)
.then((ReactPixel) => {
ReactPixel.init('XXXXXXXXXXXXXXXXXXXXX') // facebookPixelId
ReactPixel.pageView()
router.events.on('routeChangeComplete', () => {
ReactPixel.pageView()
})
})
}, [router.events])
return <Component {...pageProps} />
}
export default App
Remark: it works with typescript or JavaScript
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With