Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat/#543] Google Analytics 세팅 #544

Merged
merged 7 commits into from
Jan 21, 2025
Merged

[Feat/#543] Google Analytics 세팅 #544

merged 7 commits into from
Jan 21, 2025

Conversation

namdaeun
Copy link
Member

@namdaeun namdaeun commented Jan 20, 2025

✨ 해당 이슈 번호 ✨

closed #543

todo

  • react-ga4 설치를 통한 GA 세팅

📌 내가 알게 된 부분

  • react-ga4 라이브러리를 사용해주었습니다.
    배포시 포함되어야하기 때문에 dependencies로 설치해주었어요

react의 경우 SPA구조이기 때문에 각 페이지의 html마다 코드를 주입해주는 방법을 사용하지 않고, useLocation을 사용해서 위치의 변경을 인식할 때마다 정보를 날려주는 방식으로 구현했습니다.

RouteTracker.tsx

// 로컬일 경우 send하지 않음
  useEffect(() => {
    if (!window.location.href.includes('localhost')) {
      ReactGA.initialize(import.meta.env.VITE_GOOGLE_ANALYTICS_ID);

      setIsInitialized(true);
    }
  }, []);

// pathname과 정보를 날려줌
  useEffect(() => {
    if (isInitialized) {
      ReactGA.set({ page: location.pathname });
      ReactGA.send('pageview');
    }
  }, [isInitialized, location]);

원래는 유틸 함수처럼 만들고 App.tsx 상위에서 호출했으나, 구글링해보니 BrowseRouter 외부에서 호출된다는 에러가 뜬다고 해서 RouteTracker 라는 컴포넌트를 만들고 RouteProvider 하위에서 호출해주었습니다.

            <Suspense fallback={<Loading />}>
              <RouterProvider router={router} />
              <RouteTracker /> // 추가 !
            </Suspense>

작동이 한 번에 되면 좋겠지만 .. 동헌오빠랑 계속 확인해보면서 코드를 고쳐가야 할 것 같습니당

📌 질문할 부분

📌스크린샷(선택)

@namdaeun namdaeun added ✨ Feature 새로운 기능 추가 (새로운 구현) 다은 labels Jan 20, 2025
@namdaeun namdaeun self-assigned this Jan 20, 2025
Copy link

vercel bot commented Jan 20, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
mile-client ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 20, 2025 11:03am

@github-actions github-actions bot added the size/s size/s label Jan 20, 2025
Copy link
Member

@ljh0608 ljh0608 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@namdaeun namdaeun merged commit 4cf30cd into develop Jan 21, 2025
3 checks passed
@namdaeun namdaeun changed the title [Feat] Google Analytics 세팅 [Feat/#543] Google Analytics 세팅 Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ Feature 새로운 기능 추가 (새로운 구현) size/s size/s 다은
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[ Feat ] GA 세팅
2 participants