Skip to content

Commit 7a1d946

Browse files
authored
Merge pull request #82 from Stack-Knowledge/feature/googleAnalytics
[Client] googleAnalytics
2 parents 2af475c + 8c62b6f commit 7a1d946

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@
2828
"license": "ISC",
2929
"resolutions": {
3030
"@types/react": "^18.2.0"
31+
},
32+
"devDependencies": {
33+
"@types/gtag.js": "^0.0.17"
3134
}
3235
}

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/client/src/app/layout.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
55
import { GlobalStyle } from 'common';
66
import { Header } from 'common';
77

8+
import Script from 'next/script';
9+
10+
import * as gtag from 'client/libs/gtag';
11+
812
import Providers from './providers';
913

1014
export const metadata: Metadata = {
@@ -58,6 +62,27 @@ export default function RootLayout({
5862
}) {
5963
return (
6064
<html lang='ko'>
65+
<head>
66+
{/* Global Site Tag (gtag.js) - Google Analytics */}
67+
<Script
68+
async
69+
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`}
70+
/>
71+
<Script
72+
id='gtag-init'
73+
strategy='afterInteractive'
74+
dangerouslySetInnerHTML={{
75+
__html: `
76+
window.dataLayer = window.dataLayer || [];
77+
function gtag(){dataLayer.push(arguments);}
78+
gtag('js', new Date());
79+
gtag('config', '${gtag.GA_TRACKING_ID}', {
80+
page_path: window.location.pathname,
81+
});
82+
`,
83+
}}
84+
/>
85+
</head>
6186
<body>
6287
<Providers>
6388
<ReactQueryDevtools />

projects/client/src/libs/gtag.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID;
2+
3+
// https://developers.google.com/analytics/devguides/collection/gtagjs/events
4+
export const event = (
5+
action: Gtag.EventNames,
6+
{ event_category, event_label, value }: Gtag.EventParams
7+
) => {
8+
window.gtag('event', action, {
9+
event_category,
10+
event_label,
11+
value,
12+
});
13+
};
14+
15+
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
16+
export const pageview = (url: URL) => {
17+
if (GA_TRACKING_ID)
18+
window.gtag('config', GA_TRACKING_ID, {
19+
page_path: url,
20+
});
21+
};

projects/client/tsconfig.json

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,14 @@
22
"extends": "tsconfig/nextjs.json",
33
"compilerOptions": {
44
"jsx": "preserve",
5+
"types": ["@types/gtag.js"],
56
"paths": {
6-
"common/*": [
7-
"../../packages/common/src/*"
8-
],
9-
"client/*": [
10-
"./src/*"
11-
],
12-
"api/*": [
13-
"../../packages/api/*"
14-
]
7+
"common/*": ["../../packages/common/src/*"],
8+
"client/*": ["./src/*"],
9+
"api/*": ["../../packages/api/*"]
1510
},
1611
"strict": false
1712
},
18-
"include": [
19-
"next-env.d.ts",
20-
"**/*.ts",
21-
"**/*.tsx",
22-
".next/types/**/*.ts"
23-
],
24-
"exclude": [
25-
"node_modules"
26-
]
13+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
14+
"exclude": ["node_modules"]
2715
}

0 commit comments

Comments
 (0)