Skip to content

Next.js HOC to integrate the Facebook Pixel on every page change

License

Notifications You must be signed in to change notification settings

joecohens/next-fbq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

next-fbq

Next.js High Order Component to integrate Facebook Pixel on every page change.

Usage

Install it

yarn add next-fbq

Import it inside your pages/_app.js;

import Router from "next/router";
import withFBQ from "next-fbq";

Wrap your custom App container with it

// pass your Facebook Pixel code as first argument
export default withFBQ("139xxxxxxxxx3", Router)(MyApp);

That's it, now when the user access a page it will log a pageview to Facebook Pixel, each page change after that will also trigger a pageview on FB Pixel. The second argument is used to tell next-fbq you are wrapping your _app instead of each page individually (defaults to true), in that case it will run on componentDidUpdate too.

Note: This module detects if it's running in localhost and do nothing there to avoid polluting your analytics with local data.

Credits

This is a complete copy of @sergiodxa Google Analytics implementation next-ga adapted to the Facebok Pixel