Skip to content

Commit d0794fe

Browse files
authored
bring back google analytics for both app/pages routers (graphql#1674)
* bring back google analytics * add for app router as well * do not render locally
1 parent dd598f0 commit d0794fe

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

Diff for: next.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-env node */
2+
13
import nextra from "nextra"
24
import path from "node:path"
35
import withLess from "next-with-less"
@@ -55,6 +57,8 @@ export default withLess(
5557
// If you want to cache the remote images, you can set the time to live of the cache in seconds.
5658
// The default value is 0 seconds.
5759
nextImageExportOptimizer_remoteImageCacheTTL: "0",
60+
NEXT_PUBLIC_GA_ID:
61+
process.env.NODE_ENV === "production" ? "UA-44373548-16" : "",
5862
},
5963
trailingSlash: true,
6064
// Only for local development, skip 200 statusCode due following error:

Diff for: src/app/ga.tsx

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use client"
2+
3+
import Script from "next/script"
4+
5+
export default function GoogleAnalytics() {
6+
const gaId = process.env.NEXT_PUBLIC_GA_ID
7+
if (!gaId) {
8+
return null
9+
}
10+
return (
11+
<>
12+
<Script
13+
strategy="afterInteractive"
14+
src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`}
15+
/>
16+
<Script
17+
id="gtag-init"
18+
strategy="afterInteractive"
19+
dangerouslySetInnerHTML={{
20+
__html: `
21+
window.dataLayer = window.dataLayer || [];
22+
function gtag(){dataLayer.push(arguments);}
23+
gtag('js', new Date());
24+
gtag('config', '${gaId}', {
25+
page_path: location.pathname,
26+
});
27+
`,
28+
}}
29+
/>
30+
</>
31+
)
32+
}

Diff for: src/app/layout.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ReactElement, ReactNode } from "react"
22
import { Metadata } from "next"
33
import { Roboto_Flex } from "next/font/google"
4+
import GoogleAnalytics from "@/app/ga"
45
import "../globals.css"
56

67
const font = Roboto_Flex({
@@ -31,7 +32,10 @@ export default function RootLayout({
3132
<head>
3233
<style>{`html { scroll-padding-top: 5rem }`}</style>
3334
</head>
34-
<body className="bg-conf-black">{children}</body>
35+
<body className="bg-conf-black">
36+
<GoogleAnalytics />
37+
{children}
38+
</body>
3539
</html>
3640
)
3741
}

Diff for: src/pages/_app.tsx

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
import type { AppProps } from "next/app"
2+
import { Roboto_Flex, Roboto_Mono } from "next/font/google"
3+
import { useRouter } from "next/router"
4+
import { useEffect } from "react"
25
import "@/globals.css"
36
import "@/codemirror.less"
47
import "@/index.less"
5-
import { Roboto_Flex, Roboto_Mono } from "next/font/google"
68

79
const robotoFlex = Roboto_Flex({
8-
// weight: ["300", """500", "600", "700"],
910
subsets: ["latin"],
1011
})
1112

1213
const robotoMono = Roboto_Mono({
1314
subsets: ["latin"],
1415
})
1516

17+
const gaId = process.env.NEXT_PUBLIC_GA_ID
18+
19+
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
20+
function handleRouteChange(url: string) {
21+
;(window as any).gtag("config", gaId, { page_path: url })
22+
}
23+
1624
export default function App({ Component, pageProps }: AppProps) {
25+
const router = useRouter()
26+
useEffect(() => {
27+
if (!gaId) return
28+
router.events.on("routeChangeComplete", handleRouteChange)
29+
return () => {
30+
router.events.off("routeChangeComplete", handleRouteChange)
31+
}
32+
}, [])
33+
1734
return (
1835
<>
1936
<style jsx global>{`

0 commit comments

Comments
 (0)