-
-
Save brunosabot/167ef4e051d5a06664b6e4f458125a7e to your computer and use it in GitHub Desktop.
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 type { AppProps, NextWebVitalsMetric } from "next/app"; | |
import Script from "next/script"; | |
import { useEffect } from "react"; | |
import { useRouter } from "next/router"; | |
import Analytics from "../components/Analytics"; | |
export function reportWebVitals({ | |
id, | |
name, | |
label, | |
value, | |
}: NextWebVitalsMetric) { | |
if (typeof gtag === "undefined") return; | |
window.gtag("event", name, { | |
event_category: | |
label === "web-vital" ? "Web Vitals" : "Next.js custom metric", | |
value: Math.round(name === "CLS" ? value * 1000 : value), | |
event_label: id, | |
non_interaction: true, | |
}); | |
} | |
function MyApp({ Component, pageProps }: AppProps) { | |
const router = useRouter(); | |
useEffect(() => { | |
const handleRouteChange = (url: string) => { | |
const ua = process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS ?? ""; | |
window.gtag("config", ua, { page_path: url }); | |
}; | |
router.events.on("routeChangeComplete", handleRouteChange); | |
return () => { | |
router.events.off("routeChangeComplete", handleRouteChange); | |
}; | |
}, [router.events]); | |
return ( | |
<> | |
<Analytics /> | |
<Component {...pageProps} /> | |
</> | |
); | |
} | |
export default MyApp; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment