Skip to content

Commit 72efb66

Browse files
committed
1 parent 69d9842 commit 72efb66

8 files changed

+75
-3
lines changed

Diff for: .env.development

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
NEXT_PUBLIC_API_URL="https://api-dev.pycon.kr"
1+
NEXT_PUBLIC_API_URL="https://api-dev.pycon.kr"
2+
NEXT_PUBLIC_GA_ID="G-QNY29HSQWN"

Diff for: .env.production

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
NEXT_PUBLIC_API_URL="https://api.pycon.kr"
1+
NEXT_PUBLIC_API_URL="https://api.pycon.kr"
2+
NEXT_PUBLIC_GA_ID="G-QNY29HSQWN"

Diff for: lib/gtag.ts

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

Diff for: package-lock.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@egjs/flicking-plugins": "^4.7.0",
1717
"@egjs/react-flicking": "^4.10.6",
1818
"@stitches/react": "^1.2.8",
19+
"@types/gtag.js": "^0.0.12",
1920
"axios": "^1.3.4",
2021
"next": "13.1.5",
2122
"next-themes": "^0.2.1",

Diff for: pages/_app.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import type { AppProps } from 'next/app';
22
import React from 'react';
33
import { QueryClient, QueryClientProvider } from 'react-query';
4+
import { useRouter } from 'next/router';
5+
import * as gtag from '@/lib/gtag';
46
import { RecoilRoot, useRecoilSnapshot } from 'recoil';
57
import { ReactQueryDevtools } from 'react-query/devtools';
68
import Container from '@/components/layout/Container';
9+
import Script from 'next/script';
710
import { darkTheme } from '@/stitches.config';
811
import { ThemeProvider } from 'next-themes';
912

@@ -23,6 +26,19 @@ function RecoilDebugObserver() {
2326
const queryClient = new QueryClient();
2427

2528
function MyApp({ Component, pageProps }: AppProps) {
29+
const router = useRouter();
30+
React.useEffect(() => {
31+
const handleRouteChange = (url: URL) => {
32+
gtag.pageview(url);
33+
};
34+
router.events.on('routeChangeComplete', handleRouteChange);
35+
router.events.on('hashChangeComplete', handleRouteChange);
36+
return () => {
37+
router.events.off('routeChangeComplete', handleRouteChange);
38+
router.events.off('hashChangeComplete', handleRouteChange);
39+
};
40+
}, [router.events]);
41+
2642
return (
2743
<ThemeProvider
2844
attribute="class"
@@ -37,6 +53,10 @@ function MyApp({ Component, pageProps }: AppProps) {
3753
{process.env.NODE_ENV === 'development' && <RecoilDebugObserver />}
3854
<QueryClientProvider client={queryClient}>
3955
<Container>
56+
<Script
57+
strategy="afterInteractive"
58+
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`}
59+
/>
4060
<Component {...pageProps} />
4161
</Container>
4262
<ReactQueryDevtools initialIsOpen={false} position="bottom-right" />

Diff for: pages/_document.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import NextDocument, { Html, Head, Main, NextScript } from 'next/document';
33
import { getCssText, globalCss } from '@/stitches.config';
4+
import * as gtag from '@/lib/gtag';
45

56
const globalStyle = globalCss({
67
'*': {
@@ -38,6 +39,18 @@ export default class Document extends NextDocument {
3839
type="text/css"
3940
/>
4041
<style dangerouslySetInnerHTML={{ __html: getCssText() }} />
42+
<script
43+
dangerouslySetInnerHTML={{
44+
__html: `
45+
window.dataLayer = window.dataLayer || [];
46+
function gtag(){dataLayer.push(arguments);}
47+
gtag('js', new Date());
48+
gtag('config', '${gtag.GA_TRACKING_ID}', {
49+
page_path: window.location.pathname,
50+
});
51+
`,
52+
}}
53+
/>
4154
</Head>
4255
<body>
4356
<Main />

Diff for: tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"baseUrl": ".",
1919
"paths": {
2020
"@/*": ["./*"]
21-
}
21+
},
22+
"types": ["@types/gtag.js"]
2223
},
2324
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
2425
"exclude": ["node_modules"]

0 commit comments

Comments
 (0)