Skip to content

Instantly share code, notes, and snippets.

@gragland
Last active July 30, 2019 03:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gragland/8c0f60f54df033c602ee5e207e0693f8 to your computer and use it in GitHub Desktop.
Save gragland/8c0f60f54df033c602ee5e207e0693f8 to your computer and use it in GitHub Desktop.
Import this at the top of /pages/_app.js in your Next.js app
import Router from 'next/router';
import Analytics from 'analytics';
import googleAnalyticsPlugin from 'analytics-plugin-ga';
const analytics = Analytics({
debug: process.env.NODE_ENV !== 'production',
plugins: [
googleAnalyticsPlugin({
trackingId: process.env.GA_TRACKING_ID
})
]
});
if (typeof window !== 'undefined') {
analytics.page();
}
Router.onRouteChangeComplete = url => {
analytics.page();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment