11import type { AppProps } from "next/app"
22import Head from "next/head"
3- import { useEffect , useRef , useState } from "react"
3+ import { useEffect , useState } from "react"
44import CookieConsent , { getCookieConsentValue } from "react-cookie-consent"
55import "tailwindcss/tailwind.css"
66import Footer from "../components/Footer"
77import NavBar from "../components/NavBar"
88import "../public/global.css"
99import * as gtag from "../utils/gtag"
10- import Script from "next/script"
1110
1211export default function MyApp ( { Component, pageProps, router } : AppProps ) {
1312 const [ consented , setConsented ] = useState ( null as boolean | undefined | null )
@@ -23,27 +22,25 @@ export default function MyApp({ Component, pageProps, router }: AppProps) {
2322 } , [ ] )
2423
2524 useEffect ( ( ) => {
26- if ( ! consented )
27- return
25+ if ( consented )
26+ gtag . allowCookies ( )
27+ } , [ consented ] )
2828
29+ useEffect ( ( ) => {
2930 const handleRouteChange = ( url : string ) => {
3031 gtag . pageview ( url )
3132 }
3233 router . events . on ( "routeChangeComplete" , handleRouteChange )
3334 return ( ) => {
3435 router . events . off ( "routeChangeComplete" , handleRouteChange )
3536 }
36- } , [ router . events , consented ] )
37+ } , [ router . events ] )
3738
3839 return < div className = "bg-slate-50 dark:bg-slate-700 min-h-screen flex flex-col items-center justify-between text-slate-900 dark:text-slate-100" >
3940 < Head >
4041 < title > { router . pathname . substring ( 1 ) . replace ( / ^ \w / , w => w . toUpperCase ( ) ) } | Hu Tao</ title >
4142 < link rel = "icon" href = "/favicon.ico" />
4243 < meta httpEquiv = "content-language" content = "en-us" > </ meta >
43-
44- { consented && < Script async
45- src = { `https://www.googletagmanager.com/gtag/js?id=${ gtag . GA_TRACKING_ID } ` }
46- /> }
4744 </ Head >
4845
4946 < div className = "w-full" >
0 commit comments