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

Discussion (0)