Skip to content

Commit fbbe71b

Browse files
authored
Merge pull request #156 from Soohyuniii/fix/GA4-설정-변경
fix: GA4 설정 변경
2 parents 0c931a6 + ea00a47 commit fbbe71b

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

src/App.tsx

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,42 @@ import { initGA, trackPageView } from "@/libs/analytics";
2323

2424
const PageTracker = () => {
2525
const location = useLocation();
26+
const { pathname, state } = location;
2627

2728
const trackedPaths = [
28-
{ path: "/", label: "home" },
29-
{ path: "/login", label: "login" },
30-
{ path: "/contents" }
29+
{ path: "/", page: "홈", element: "" },
30+
{ path: "/login", page: "로그인/회원가입", element: "로그인" },
31+
{ path: "/contents", page: "일반콘텐츠", element: "" },
32+
{ path: "/my-info", page: "내 정보", element: "" },
33+
{ path: "/chat", page: "채팅방", element: "" },
34+
{ path: "/select-info", page: "빠른 대화 설정", element: "" },
35+
{ path: "/select-info", page: "친구 저장", element: "대화 시작하기" }
3136
];
3237

3338
useEffect(() => {
34-
const { pathname } = location;
39+
const trackedContentPaths = ["/contents/1", "/contents/2"];
3540

36-
trackedPaths.forEach(({ path, label }) => {
37-
if (path === "/contents" && pathname.startsWith(path)) {
38-
trackPageView(label || pathname);
39-
} else if (pathname === path) {
40-
trackPageView(label || pathname);
41+
trackedPaths.forEach(({ path, page, element }) => {
42+
// 콘텐츠 상세 페이지 (일반 콘텐츠만 추적)
43+
if (trackedContentPaths.includes(pathname)) {
44+
if (path === "/contents") {
45+
trackPageView(path, { page, element });
46+
}
47+
}
48+
// select-info 페이지에서 state로 분기
49+
else if (pathname === "/select-info" && path === pathname) {
50+
if (state === "fastFriend" && page === "빠른 대화 설정") {
51+
trackPageView(path, { page, element });
52+
} else if (state === "virtualFriend" && page === "친구 저장") {
53+
trackPageView(path, { page, element });
54+
}
55+
}
56+
// 나머지 일반 path
57+
else if (pathname === path && path !== "/select-info") {
58+
trackPageView(path, { page, element });
4159
}
4260
});
43-
}, [location.pathname]);
61+
}, [location.pathname, location.state]);
4462

4563
return null;
4664
};

src/libs/analytics.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,28 @@ const GA_MEASUREMENT_ID = import.meta.env.VITE_GA_MEASUREMENT_ID || "";
44

55
const isProduction = import.meta.env.MODE === "production";
66

7-
const initGA = () => {
7+
export const initGA = () => {
88
console.log("isProduction", isProduction);
99
console.log("ID", GA_MEASUREMENT_ID);
1010
if (isProduction && GA_MEASUREMENT_ID) {
11+
console.log("initGA");
1112
ReactGA.initialize(GA_MEASUREMENT_ID);
1213
}
1314
};
1415

15-
const trackPageView = (path: string) => {
16-
console.log("trackPageView 1");
16+
export const trackPageView = (url: string, params?: Record<string, string>) => {
1717
if (isProduction && GA_MEASUREMENT_ID) {
18-
console.log("trackPageView 2");
19-
ReactGA.send({ hitType: "pageview", page: path });
18+
console.log("trackPageView");
19+
ReactGA.gtag("event", "page_view", {
20+
page_path: url,
21+
...params
22+
});
2023
}
2124
};
2225

23-
const trackEvent = (category: string, action: string, label?: string) => {
26+
export const trackEvent = (name: string, params?: Record<string, string>) => {
2427
if (isProduction && GA_MEASUREMENT_ID) {
25-
ReactGA.event({
26-
category,
27-
action,
28-
label
29-
});
28+
console.log("trackEvent");
29+
ReactGA.event(name, params);
3030
}
3131
};
32-
33-
export { initGA, trackPageView, trackEvent };

src/pages/Content.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,10 @@ const Content = () => {
178178

179179
const handleStartChat = async () => {
180180
try {
181-
trackEvent("User", "Clicked Start Chat Button", "Start Chat");
182-
181+
trackEvent("Click", {
182+
page: "일반 콘텐츠",
183+
element: "대화 시작하기"
184+
});
183185
const response = await instance.post("/api/fast-friend");
184186
console.log("Success!!", response.data);
185187
} catch (error) {

0 commit comments

Comments
 (0)