61

next-analytics - Next.js HOC to integrate Google Analytics and FB Pixel

 5 years ago
source link: https://www.tuicool.com/articles/hit/eaIvEri
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

next-analytics

Next.js HOC to integrate analytics tools (GA and FBQ).

Usage

Install it

yarn add next-analytics

Import it inside your pages/_app.js ;

import withAnalytics from "next-analytics";

Wrap your custom App container with it

// pass an object with your Google Analytics and/or Facebook Pixel code as first argument
export default withAnalytics({ ga: "UA-xxxxxxxxx-1", fbq: "139xxxxxxxxx3" })(MyApp);

That's it, now when the user access a page it will log a pageview to Google Analytics and/or Facebook Pixel, each page change after that will also trigger a pageview on GA and/or FBQ.

Note: This module only applies the HOC next-ga or next-fbq if the code is sent when instancing

Credits

Thanks to @joecohens for creating next-fbq .


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK