DEV Community

Agik Setiawan
Agik Setiawan

Posted on

Add Facebook Pixel code in Next JS

Itegrate facebook pixel very easy with react-facebook-pixel

Open and modify _app.tsx and add code like below

 useEffect(() => {
    import('react-facebook-pixel')
      .then((x) => x.default)
      .then((ReactPixel) => {
        ReactPixel.init('FACEBOOK_PIXEL_ID')
        ReactPixel.pageView()

        router.events.on('routeChangeComplete', () => {
          ReactPixel.pageView()
        })
      })
  }, [router.events])
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
cetele profile image
Conchi • Edited

Hello, Agik

As you know, router.events is not supported in NextJs 13 with the app router.

Would it be possible to know the equivalent code to integrate Facebook pixel in a site built on app router?

Thank you very much and greetings