Skip to content

Commit

Permalink
feat: useGA hooks 분리 #430
Browse files Browse the repository at this point in the history
  • Loading branch information
novice0840 committed Dec 25, 2024
1 parent 1f8fbc9 commit c398408
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
10 changes: 10 additions & 0 deletions frontend/src/lib/googleAnalytics/hooks/useGAInitialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useEffect } from 'react';
import ReactGA from 'react-ga4';

const useGAInitialize = () => {
useEffect(() => {
ReactGA.initialize(process.env.GA_MANAGEMENT_ID || '');
}, []);
};

export default useGAInitialize;
10 changes: 10 additions & 0 deletions frontend/src/lib/googleAnalytics/hooks/useGAPageTimeSpent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useEffect } from 'react';
import ReactGA from 'react-ga4';

const useGAPageTimeSpent = (page: string, timeSpent: number) => {
useEffect(() => {
ReactGA.send({ hitType: 'pageview', page, timeSpent });
});
};

export default useGAPageTimeSpent;
6 changes: 2 additions & 4 deletions frontend/src/router/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import { Outlet } from 'react-router-dom';

import AsyncErrorBoundary from '@/components/common/ErrorBoundary/AsyncErrorBoundary';
import RootErrorBoundary from '@/components/common/ErrorBoundary/RootErrorBoundary';
import useGAInitialize from '@/lib/googleAnalytics/hooks/useGAInitialize';
import ModalProvider from '@/providers/ModalProvider/ModalProvider';

const MainLayout = () => {
useEffect(() => {
ReactGA.initialize('G-3BFVVPQT0Z');
ReactGA.send({ hitType: 'pageview', page: '/my-path', title: 'Custom Title' });
});
useGAInitialize();

return (
<RootErrorBoundary>
Expand Down

0 comments on commit c398408

Please sign in to comment.