Skip to content

Commit

Permalink
Refactor: modify GA script && preconnect assets
Browse files Browse the repository at this point in the history
  • Loading branch information
hookor committed Jun 3, 2024
1 parent b1a4507 commit ab27472
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 71 deletions.
18 changes: 18 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
type="font/woff2"
crossorigin="anonymous"
href="https://cdn.jsdelivr.net/gh/webfontworld/pretendard/Pretendard-Bold.woff2" />
<link
rel="preload"
href="/png/coffee_mainimg.png" />
<link
rel="preconnect"
href="https://ddocker.kro.kr" />

<link
rel="manifest"
Expand Down Expand Up @@ -175,6 +181,18 @@
rel="apple-touch-startup-image"
href="./public/assets/apple-splash-1136-640.jpg"
media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2) and (orientation: landscape)" />

<script
async
src="https://www.googletagmanager.com/gtag/js?id=G-HXVP19S89G"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-HXVP19S89G');
</script>
</head>
<body>
<script>
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@prerenderer/rollup-plugin": "^0.3.12",
"@tanstack/react-query": "^5.24.1",
"@types/event-source-polyfill": "^1.0.5",
"@types/gtag.js": "^0.0.20",
"@types/node": "^20.10.4",
"@types/react": "^18.2.42",
"@types/react-dom": "^18.2.17",
Expand All @@ -47,12 +48,10 @@
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"imagemin-gifsicle": "^7.0.0",
"imagemin-mozjpeg": "^10.0.0",
"imagemin-pngquant": "^9.0.2",
"imagemin-svgo": "^10.0.1",
"puppeteer": "^22.10.0",
"react-ga4": "^2.1.0",
"rollup-plugin-visualizer": "^5.10.0",
"typescript": "^5.3.2",
"vite": "^5.0.6",
Expand Down
49 changes: 7 additions & 42 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 10 additions & 16 deletions src/hooks/google/useTrackRoute.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { useEffect, useState } from 'react';
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import ReactGA from 'react-ga4';

const useTrackRoute = () => {
const location = useLocation();
const [initialized, setInitialized] = useState(false);
const { NODE_ENV } = import.meta.env;
const isPro = NODE_ENV === 'production';

useEffect(() => {
if (import.meta.env.VITE_GA) {
ReactGA.initialize(import.meta.env.VITE_GA);
setInitialized(true);
}
}, []);

// location 변경 감지시 pageview 이벤트 전송
useEffect(() => {
if (initialized) {
ReactGA.set({ page: location.pathname });
ReactGA.send('pageview');
}
}, [initialized, location]);
if (!isPro) return;
window.gtag('event', 'pageview', {
page_path: location.pathname + location.search + location.hash,
page_search: location.search,
page_hash: location.hash
});
}, [location]);
};

export default useTrackRoute;
8 changes: 0 additions & 8 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@ import { RouterProvider } from 'react-router-dom';
import { router } from '@/pages/Router';
import './main.css';
import '/styled-system/styles.css';
import ReactGA from 'react-ga4';
import { HelmetProvider } from 'react-helmet-async';

const { NODE_ENV } = import.meta.env;
const isPro = NODE_ENV === 'production';

if (isPro && import.meta.env.VITE_GA) {
ReactGA.initialize(import.meta.env.VITE_GA);
}

const queryClient = new QueryClient({
defaultOptions: {
queries: {
Expand Down
16 changes: 13 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import tsconfigpaths from 'vite-tsconfig-paths';
import removeConsole from 'vite-plugin-remove-console';
import imageminMozjpeg from 'imagemin-mozjpeg';
import imageminPngQuant from 'imagemin-pngquant';
import imageminGifSicle from 'imagemin-gifsicle';
import viteImagemin from '@vheemstra/vite-plugin-imagemin';
import imageminSvgo from 'imagemin-svgo';
import prerender from '@prerenderer/rollup-plugin';
Expand Down Expand Up @@ -60,9 +59,20 @@ export default defineConfig({
plugins: {
jpg: imageminMozjpeg(),
png: imageminPngQuant(),
gif: imageminGifSicle(),
svg: imageminSvgo()
}
})
]
],
build: {
rollupOptions: {
output: {
manualChunks: id => {
if (id.indexOf('node_modules') !== -1) {
const module = id.split('node_modules/').pop().split('/')[0];
return `vendor-${module}`;
}
}
}
}
}
});

0 comments on commit ab27472

Please sign in to comment.