Skip to content

Commit dd51779

Browse files
committed
feat: ga4, gtm 연동
1 parent 81ad0d8 commit dd51779

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

.pnp.cjs

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"classnames": "^2.5.1",
2525
"react": "^18.3.1",
2626
"react-dom": "^18.3.1",
27+
"react-gtm-module": "^2.0.11",
2728
"react-router-dom": "^7.1.3",
2829
"zustand": "^5.0.3"
2930
},
@@ -46,6 +47,7 @@
4647
"@types/node": "^22.10.7",
4748
"@types/react": "^18.3.18",
4849
"@types/react-dom": "^18.3.5",
50+
"@types/react-gtm-module": "^2",
4951
"@typescript-eslint/eslint-plugin": "^8.20.0",
5052
"@typescript-eslint/parser": "^8.20.0",
5153
"@vitejs/plugin-react": "^4.3.4",

src/main.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import { StrictMode } from "react";
23
import ReactDom from "react-dom/client";
4+
import TagManager from "react-gtm-module";
35

46
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
57

@@ -15,6 +17,35 @@ import { ToastProvider } from "@/hooks/common/useToast";
1517
import "@/styles/reset.scss";
1618
import "@/styles/global.scss";
1719

20+
const gtmTag = {
21+
gtmId: import.meta.env.VITE_GTM_ID,
22+
};
23+
24+
const initializeGA4 = () => {
25+
const ga4Id = import.meta.env.VITE_GA4_ID;
26+
27+
const script = document.createElement("script");
28+
script.async = true;
29+
script.src = `https://www.googletagmanager.com/gtag/js?id=${ga4Id}`;
30+
document.head.appendChild(script);
31+
32+
script.onload = () => {
33+
(window as any).dataLayer = (window as any).dataLayer || [];
34+
35+
function gtag(...args: any[]) {
36+
(window as any).dataLayer.push(args);
37+
}
38+
gtag("js", new Date());
39+
40+
gtag("config", ga4Id);
41+
};
42+
};
43+
44+
if (import.meta.env.MODE === "production") {
45+
TagManager.initialize(gtmTag);
46+
initializeGA4();
47+
}
48+
1849
ReactDom.createRoot(document.getElementById("root")!).render(
1950
<StrictMode>
2051
<ReactQueryClientProvider>

yarn.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,6 +3038,13 @@ __metadata:
30383038
languageName: node
30393039
linkType: hard
30403040

3041+
"@types/react-gtm-module@npm:^2":
3042+
version: 2.0.4
3043+
resolution: "@types/react-gtm-module@npm:2.0.4"
3044+
checksum: 10c0/0bba7b841617fca4a23aacdd6e69299e0b9f193988d381db285e91de3b17bf4599bc58236d2f76a2eb2cf589b4e4d665d66feb2f90f87b549e957a79765ffec6
3045+
languageName: node
3046+
linkType: hard
3047+
30413048
"@types/react@npm:^18.3.18":
30423049
version: 18.3.18
30433050
resolution: "@types/react@npm:18.3.18"
@@ -8283,6 +8290,7 @@ __metadata:
82838290
"@types/node": "npm:^22.10.7"
82848291
"@types/react": "npm:^18.3.18"
82858292
"@types/react-dom": "npm:^18.3.5"
8293+
"@types/react-gtm-module": "npm:^2"
82868294
"@typescript-eslint/eslint-plugin": "npm:^8.20.0"
82878295
"@typescript-eslint/parser": "npm:^8.20.0"
82888296
"@vitejs/plugin-react": "npm:^4.3.4"
@@ -8303,6 +8311,7 @@ __metadata:
83038311
prettier: "npm:^3.4.2"
83048312
react: "npm:^18.3.1"
83058313
react-dom: "npm:^18.3.1"
8314+
react-gtm-module: "npm:^2.0.11"
83068315
react-router-dom: "npm:^7.1.3"
83078316
sass: "npm:^1.83.4"
83088317
sass-loader: "npm:^16.0.4"
@@ -9158,6 +9167,13 @@ __metadata:
91589167
languageName: node
91599168
linkType: hard
91609169

9170+
"react-gtm-module@npm:^2.0.11":
9171+
version: 2.0.11
9172+
resolution: "react-gtm-module@npm:2.0.11"
9173+
checksum: 10c0/ceedf296a1232934788583e4a20452a82e872fcd79e24827bf615b1cd3bceb59c294d169b1bf8313ba48b9e480e681ce520cd2a5cf2381c5aaa1fb74d4f88930
9174+
languageName: node
9175+
linkType: hard
9176+
91619177
"react-is@npm:^17.0.1":
91629178
version: 17.0.2
91639179
resolution: "react-is@npm:17.0.2"

0 commit comments

Comments
 (0)