Skip to content

bring back google analytics for both app/pages routers #1674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-env node */

import nextra from "nextra"
import path from "node:path"
import withLess from "next-with-less"
Expand Down Expand Up @@ -55,6 +57,8 @@ export default withLess(
// If you want to cache the remote images, you can set the time to live of the cache in seconds.
// The default value is 0 seconds.
nextImageExportOptimizer_remoteImageCacheTTL: "0",
NEXT_PUBLIC_GA_ID:
process.env.NODE_ENV === "production" ? "UA-44373548-16" : "",
},
trailingSlash: true,
// Only for local development, skip 200 statusCode due following error:
Expand Down
32 changes: 32 additions & 0 deletions src/app/ga.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use client"

import Script from "next/script"

export default function GoogleAnalytics() {
const gaId = process.env.NEXT_PUBLIC_GA_ID
if (!gaId) {
return null
}
return (
<>
<Script
strategy="afterInteractive"
src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`}
/>
<Script
id="gtag-init"
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gaId}', {
page_path: location.pathname,
});
`,
}}
/>
</>
)
}
6 changes: 5 additions & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ReactElement, ReactNode } from "react"
import { Metadata } from "next"
import { Roboto_Flex } from "next/font/google"
import GoogleAnalytics from "@/app/ga"
import "../globals.css"

const font = Roboto_Flex({
Expand Down Expand Up @@ -31,7 +32,10 @@ export default function RootLayout({
<head>
<style>{`html { scroll-padding-top: 5rem }`}</style>
</head>
<body className="bg-conf-black">{children}</body>
<body className="bg-conf-black">
<GoogleAnalytics />
{children}
</body>
</html>
)
}
21 changes: 19 additions & 2 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
import type { AppProps } from "next/app"
import { Roboto_Flex, Roboto_Mono } from "next/font/google"
import { useRouter } from "next/router"
import { useEffect } from "react"
import "@/globals.css"
import "@/codemirror.less"
import "@/index.less"
import { Roboto_Flex, Roboto_Mono } from "next/font/google"

const robotoFlex = Roboto_Flex({
// weight: ["300", """500", "600", "700"],
subsets: ["latin"],
})

const robotoMono = Roboto_Mono({
subsets: ["latin"],
})

const gaId = process.env.NEXT_PUBLIC_GA_ID

// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
function handleRouteChange(url: string) {
;(window as any).gtag("config", gaId, { page_path: url })
}

export default function App({ Component, pageProps }: AppProps) {
const router = useRouter()
useEffect(() => {
if (!gaId) return
router.events.on("routeChangeComplete", handleRouteChange)
return () => {
router.events.off("routeChangeComplete", handleRouteChange)
}
}, [])

return (
<>
<style jsx global>{`
Expand Down