blog / components /Gtag.js
sandy-try's picture
Upload 699 files
1b72d7e verified
raw
history blame contribute delete
456 Bytes
import { useEffect } from 'react'
import { useRouter } from 'next/router'
import * as gtag from '@/lib/gtag'
const Gtag = () => {
const router = useRouter()
useEffect(() => {
const gtagRouteChange = url => {
gtag.pageview(url)
}
router.events.on('routeChangeComplete', gtagRouteChange)
return () => {
router.events.off('routeChangeComplete', gtagRouteChange)
}
}, [router.events])
return null
}
export default Gtag