Skip to content

Commit

Permalink
Use useRouter over Router for with-google-analytics example (#16846)
Browse files Browse the repository at this point in the history
Updating `with-google-analytics` example to be in line with [our documentation recommending `useRouter`](https://nextjs.org/docs/api-reference/next/router#userouter). Verified that it works.
  • Loading branch information
chibicode authored Sep 4, 2020
1 parent f7435b4 commit 1c7cc60
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions examples/with-google-analytics/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { useEffect } from 'react'
import Router from 'next/router'
import { useRouter } from 'next/router'
import * as gtag from '../lib/gtag'

const App = ({ Component, pageProps }) => {
const router = useRouter()
useEffect(() => {
const handleRouteChange = (url) => {
gtag.pageview(url)
}
Router.events.on('routeChangeComplete', handleRouteChange)
router.events.on('routeChangeComplete', handleRouteChange)
return () => {
Router.events.off('routeChangeComplete', handleRouteChange)
router.events.off('routeChangeComplete', handleRouteChange)
}
}, [])
}, [router.events])

return <Component {...pageProps} />
}
Expand Down

0 comments on commit 1c7cc60

Please sign in to comment.