Skip to content

Commit 290eac4

Browse files
committed
feat: add ga
1 parent 115649f commit 290eac4

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

.env.local.example

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
NEXT_PUBLIC_SUPABASE_URL=YOUR_SUPABASE_URL
2-
NEXT_PUBLIC_SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEY
2+
NEXT_PUBLIC_SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEY
3+
NEXT_PUBLIC_GA_ID=

common/ga.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @ts-nocheck
2+
export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID
3+
4+
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
5+
export const pageview = url => {
6+
window.gtag('config', GA_TRACKING_ID, {
7+
page_path: url,
8+
})
9+
}
10+
11+
// https://developers.google.com/analytics/devguides/collection/gtagjs/events
12+
export const event = ({ action, category, label, value }) => {
13+
window.gtag('event', action, {
14+
event_category: category,
15+
event_label: label,
16+
value: value,
17+
})
18+
}

pages/_app.tsx

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { CssBaseline, GeistProvider } from '@geist-ui/react'
22
import { AppProps } from 'next/app'
3+
import Script from 'next/script'
34
import Head from 'next/head'
5+
import * as gtag from '../common/ga'
46
import { LocaleProvider, useLocale } from '../common/hooks/useLocale'
57

68
const App: React.FC<AppProps> = ({ Component, pageProps }) => {
@@ -15,20 +17,33 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
1517
name="viewport"
1618
content="width=device-width, initial-scale=1, user-scalable=no"
1719
/>
18-
<meta
19-
name="description"
20-
content={t('AppDesc')}
21-
/>
20+
<meta name="description" content={t('AppDesc')} />
2221
<meta
2322
name="keywords"
2423
content="ETH,EVM,EVM Compatible Network,Network,Blockchain,chain"
2524
/>
2625
<meta property="og:title" content="EVM Box" />
27-
<meta
28-
property="og:description"
29-
content={t('AppDesc')}
30-
/>
26+
<meta property="og:description" content={t('AppDesc')} />
3127
</Head>
28+
{/* Global Site Tag (gtag.js) - Google Analytics */}
29+
<Script
30+
strategy="afterInteractive"
31+
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`}
32+
/>
33+
<Script
34+
id="gtag-init"
35+
strategy="afterInteractive"
36+
dangerouslySetInnerHTML={{
37+
__html: `
38+
window.dataLayer = window.dataLayer || [];
39+
function gtag(){dataLayer.push(arguments);}
40+
gtag('js', new Date());
41+
gtag('config', '${gtag.GA_TRACKING_ID}', {
42+
page_path: window.location.pathname,
43+
});
44+
`,
45+
}}
46+
/>
3247
<Component {...pageProps} />
3348
</GeistProvider>
3449
</LocaleProvider>

0 commit comments

Comments
 (0)