Analytics

No big project can live without gathering analytical information about their users, their habits and sources, and so on. A world leader in this field is Google Analytics, so let's create a simple integration between Next.js and this great product.

First, you need to create a project in Google Analytics and copy the ID:

We start with packages:

$ npm install next --save-dev$ npm install react react-dom react-ga --save

Next, let's create a HOC for pages that require analytics:

import React from "react";import Router from 'next/router';import ReactGA from 'react-ga';const GA_TRACKING_ID = '...'; // paste your ID hereconst WINDOWPROP ...

Get Next.js Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.