Last active
July 30, 2019 03:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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