Skip to content
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

[FE] feat#301 구글 애널리틱스 설정 #302

Merged
merged 2 commits into from
Dec 18, 2023
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
1 change: 1 addition & 0 deletions packages/frontend/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_GOOGLE_MEASUREMENT_ID=G-66F9GF1VQ3
2 changes: 1 addition & 1 deletion packages/frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ yarn-debug.log*
yarn-error.log*

# env files
.env*
.env

# vercel
.vercel
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@testing-library/react": "^14.1.0",
"@testing-library/user-event": "^14.5.1",
"@types/d3": "^7",
"@types/gtag.js": "^0.0.18",
"@types/jest": "^29.5.8",
"@types/node": "^20",
"@types/react": "^18",
Expand Down
28 changes: 28 additions & 0 deletions packages/frontend/src/components/analytics/Scripts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Script from "next/script";

import { PRODUCTION } from "../../constants/config";
import { GA_TRACKING_ID } from "../../libs/gtag";

function Scripts() {
return (
<div>
{PRODUCTION && (
<>
<Script
src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`}
/>
<Script strategy="lazyOnload" id="ga">
{`window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_TRACKING_ID}', {
page_path: window.location.pathname,
});`}
</Script>
</>
)}
</div>
);
}

export default Scripts;
22 changes: 22 additions & 0 deletions packages/frontend/src/components/analytics/useGtagEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useRouter } from "next/router";
import { useEffect } from "react";

import { PRODUCTION } from "../../constants/config";
import * as gtag from "../../libs/gtag";

const useGtagEffect = () => {
const router = useRouter();
useEffect(() => {
if (!PRODUCTION) return () => {};

const handleRouteChange = (url: URL) => {
gtag.pageview(url);
};
router.events.on("routeChangeComplete", handleRouteChange);
return () => {
router.events.off("routeChangeComplete", handleRouteChange);
};
}, [router.events]);
return null;
};
export default useGtagEffect;
1 change: 1 addition & 0 deletions packages/frontend/src/constants/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PRODUCTION = process.env.NODE_ENV === "production";
30 changes: 30 additions & 0 deletions packages/frontend/src/libs/gtag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const GA_TRACKING_ID: string =
process.env.NEXT_PUBLIC_GOOGLE_MEASUREMENT_ID || "";

// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
export const pageview = (url: URL) => {
if (typeof window !== "object") return;
window.gtag("config", GA_TRACKING_ID, {
page_path: url,
});
};

// https://developers.google.com/analytics/devguides/collection/gtagjs/events
export const event = ({
action,
category,
label,
value,
}: {
action: string;
category: string;
label: string;
value: string;
}) => {
if (typeof window !== "object") return;
window.gtag("event", action, {
event_category: category,
event_label: label,
value,
});
};
5 changes: 5 additions & 0 deletions packages/frontend/src/pages/_app.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import React, { useEffect, useState } from "react";
import "react-toastify/dist/ReactToastify.min.css";

import { sessionAPI } from "../apis/session";
import Scripts from "../components/analytics/Scripts";
import useGtagEffect from "../components/analytics/useGtagEffect";
import { UserQuizStatusProvider } from "../contexts/UserQuizStatusContext";
import { ToastContainer, toast } from "../design-system/components/common";
import Layout from "../design-system/components/common/Layout";
Expand All @@ -31,6 +33,8 @@ export default function App({ Component, pageProps }: AppProps) {
})();
}, []);

useGtagEffect();

return (
<>
<React.StrictMode>
Expand All @@ -42,6 +46,7 @@ export default function App({ Component, pageProps }: AppProps) {
/>
</Head>
<UserQuizStatusProvider initialUserQuizStatus={userQuizStatus}>
<Scripts />
<ThemeWrapper>
<Layout>
<Component {...pageProps} />
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5457,6 +5457,13 @@ __metadata:
languageName: node
linkType: hard

"@types/gtag.js@npm:^0.0.18":
version: 0.0.18
resolution: "@types/gtag.js@npm:0.0.18"
checksum: af7a0a5769c5cfbe75e47f2b0346a65db8234b3c89775b897681548dfb6898edb3145153e2bc5dd1751a5ace221e03e6a68b7650ecacc6aa16a099aa9d925c99
languageName: node
linkType: hard

"@types/html-minifier-terser@npm:^6.0.0":
version: 6.1.0
resolution: "@types/html-minifier-terser@npm:6.1.0"
Expand Down Expand Up @@ -10951,6 +10958,7 @@ __metadata:
"@testing-library/react": "npm:^14.1.0"
"@testing-library/user-event": "npm:^14.5.1"
"@types/d3": "npm:^7"
"@types/gtag.js": "npm:^0.0.18"
"@types/jest": "npm:^29.5.8"
"@types/node": "npm:^20"
"@types/react": "npm:^18"
Expand Down