Skip to content

Commit 0a2f0dd

Browse files
authored
refactor: gtag 에러 수정 (#141)
1 parent 43facfa commit 0a2f0dd

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

src/main.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ const initializeGA4 = () => {
3232
script.onload = () => {
3333
(window as any).dataLayer = (window as any).dataLayer || [];
3434

35-
function gtag(...args: any[]) {
35+
(window as any).gtag = function (...args: any[]) {
3636
(window as any).dataLayer.push(args);
37-
}
38-
gtag("js", new Date());
37+
};
3938

40-
gtag("config", ga4Id);
39+
(window as any).gtag("js", new Date());
40+
(window as any).gtag("config", ga4Id);
4141
};
4242
};
4343

src/types/global.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ declare global {
2828
}
2929
interface Window {
3030
// eslint-disable-next-line @typescript-eslint/no-explicit-any
31-
gtag: (...args: any[]) => void;
31+
gtag: (event: string, action: string, params?: Record<string, any>) => void;
3232
response?: {
3333
receiveScanResult: (jsonData: string) => void;
3434
receiveGeneratedReview: (jsonData: string) => void;

src/utils/gtag.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
export const gTagLogEvent = (eventLabel: string) => {
2-
window.gtag("event", "click", {
3-
event_category: "button",
4-
event_label: eventLabel,
5-
});
2+
if (typeof window !== "undefined" && typeof window.gtag === "function") {
3+
window.gtag("event", "click", {
4+
event_category: "button",
5+
event_label: eventLabel,
6+
});
7+
} else {
8+
console.warn("Google Analytics is not initialized yet.");
9+
}
610
};

0 commit comments

Comments
 (0)