DEV Community

Agik Setiawan
Agik Setiawan

Posted on

8 5

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay