Skip to content

Commit 2adb274

Browse files
macmac
authored andcommitted
feat: ga 코드 위치 수정
1 parent bb74df2 commit 2adb274

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/components/Header/Header.tsx

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
import { useMediaQuery } from '@chakra-ui/react';
2-
import { useEffect, useState } from 'react';
3-
import ReactGA from 'react-ga4';
4-
import { useLocation } from 'react-router-dom';
2+
53
import MobileHeader from './MobileHeader';
64
import PcHeader from './PcHeader';
75
import { devices } from '@/styles/theme';
86

97
const Header = () => {
108
const [isLargerThanTablet] = useMediaQuery(devices.tablet);
11-
const [initialized, setInitialized] = useState(false);
12-
const location = useLocation();
13-
14-
useEffect(() => {
15-
if (!window.location.href.includes('localhost')) {
16-
ReactGA.initialize(import.meta.env.VITE_GOOGLE_ANALYTICS_TRAKING_ID);
17-
setInitialized(true);
18-
}
19-
}, []);
20-
21-
useEffect(() => {
22-
if (initialized) {
23-
ReactGA.set({ page: location.pathname });
24-
ReactGA.send('pageview');
25-
}
26-
}, [initialized, location]);
279

2810
return (
2911
<header>{isLargerThanTablet ? <PcHeader /> : <MobileHeader />}</header>

src/routes/Layout.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
import { Box } from '@chakra-ui/react';
2+
import { useEffect, useState } from 'react';
3+
import ReactGA from 'react-ga4';
4+
import { useLocation } from 'react-router-dom';
25
import { Outlet } from 'react-router-dom';
36
import { Header } from '@/components';
47
import { HEADER_HEIGHT } from '@/components/Header/constants';
58
import UserFeedbackFab from '@/components/UserFeedbackFab/UserFeedbackFab';
69

710
const Layout = () => {
11+
const [initialized, setInitialized] = useState(false);
12+
const location = useLocation();
13+
useEffect(() => {
14+
if (!window.location.href.includes('localhost')) {
15+
ReactGA.initialize(import.meta.env.VITE_GOOGLE_ANALYTICS_TRAKING_ID);
16+
setInitialized(true);
17+
}
18+
}, []);
19+
20+
useEffect(() => {
21+
if (initialized) {
22+
ReactGA.set({ page: location.pathname });
23+
ReactGA.send('pageview');
24+
}
25+
}, [initialized, location]);
26+
827
return (
928
<>
1029
<Header />

0 commit comments

Comments
 (0)