Skip to content

Commit 30ec994

Browse files
committed
feat: 🎸 google analytics
1 parent dfa94d5 commit 30ec994

File tree

4 files changed

+40
-26
lines changed

4 files changed

+40
-26
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use client'
2+
3+
import { usePathname, useSearchParams } from 'next/navigation'
4+
import { useEffect } from 'react'
5+
6+
export const GoogleAnalyticsTrackPv = ({
7+
GA_MEASUREMENT_ID,
8+
}: {
9+
GA_MEASUREMENT_ID: string
10+
}) => {
11+
const pathname = usePathname()
12+
const searchParams = useSearchParams()
13+
14+
useEffect(() => {
15+
const url = pathname + searchParams.toString()
16+
17+
pageview(GA_MEASUREMENT_ID, url)
18+
}, [pathname, searchParams, GA_MEASUREMENT_ID])
19+
20+
return null
21+
}
22+
23+
const pageview = (GA_MEASUREMENT_ID: string, url: string) => {
24+
if (typeof window !== 'undefined' && window.gtag) {
25+
window.gtag('config', GA_MEASUREMENT_ID, {
26+
page_path: url,
27+
})
28+
}
29+
}
Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
1-
'use client'
2-
3-
import { usePathname, useSearchParams } from 'next/navigation'
1+
import { GoogleAnalyticsTrackPv } from '@/analytics/google-analytics-track-pv'
42
import Script from 'next/script'
5-
import { useEffect } from 'react'
3+
import { Suspense } from 'react'
64

75
export const GoogleAnalytics = ({
86
GA_MEASUREMENT_ID,
97
}: {
108
GA_MEASUREMENT_ID: string
119
}) => {
12-
const pathname = usePathname()
13-
const searchParams = useSearchParams()
14-
15-
useEffect(() => {
16-
const url = pathname + searchParams.toString()
17-
18-
pageview(GA_MEASUREMENT_ID, url)
19-
}, [pathname, searchParams, GA_MEASUREMENT_ID])
20-
2110
return (
2211
<>
2312
<Script
@@ -43,12 +32,9 @@ export const GoogleAnalytics = ({
4332
`,
4433
}}
4534
/>
35+
<Suspense fallback={null}>
36+
<GoogleAnalyticsTrackPv GA_MEASUREMENT_ID={GA_MEASUREMENT_ID} />
37+
</Suspense>
4638
</>
4739
)
4840
}
49-
50-
const pageview = (GA_MEASUREMENT_ID: string, url: string) => {
51-
window.gtag('config', GA_MEASUREMENT_ID, {
52-
page_path: url,
53-
})
54-
}

‎apps/tlink-web/app/layout.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { TailwindIndicator } from '@/components/tailwind-indicator'
55
import '@rainbow-me/rainbowkit/styles.css'
66
import type { Metadata } from 'next'
77
import localFont from 'next/font/local'
8-
import { Suspense } from 'react'
98
import './globals.css'
109

1110
const geistSans = localFont({
@@ -33,9 +32,7 @@ export default function RootLayout({
3332
return (
3433
<html lang="en">
3534
<head>
36-
<Suspense fallback={null}>
37-
<GoogleAnalytics GA_MEASUREMENT_ID="G-3576R4G248" />
38-
</Suspense>
35+
<GoogleAnalytics GA_MEASUREMENT_ID="G-3576R4G248" />
3936
</head>
4037
<body
4138
className={`${geistSans.variable} ${geistMono.variable} antialiased`}

‎apps/tlink-web/components/cookie-banner.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ const AGREE_COOKIE_KEY = 'sl-cookie'
1313

1414
const initGoogleAnalytics = (cookieConsent: boolean = true) => {
1515
const newValue = cookieConsent ? 'granted' : 'denied'
16-
window.gtag('consent', 'update', {
17-
analytics_storage: newValue,
18-
})
16+
if (typeof window !== 'undefined' && window.gtag) {
17+
window.gtag('consent', 'update', {
18+
analytics_storage: newValue,
19+
})
20+
}
1921
}
2022

2123
export const CookieBanner = () => {

0 commit comments

Comments
 (0)