Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ publishDate: "2025-12-01" # optional - 예약 발행
- hydration 관련 변경 후에는 `pnpm build`, `pnpm preview`로 `Loading...`, Suspense hidden segment, React hydration warning이 없는지 확인

### Page Views
- `page_view`는 GA4 자동 측정(초기 로드·브라우저 History 변화)에서만 전송합니다. 앱이나 GTM 맞춤 이벤트 태그에서 중복 발행하지 않습니다.
- Google Apps Script API로 GA 데이터 조회
- `usePageViews()` 훅: sessionStorage 캐시 (TTL 15분)
- 메모리 + sessionStorage 이중 캐시
Expand Down
14 changes: 14 additions & 0 deletions docs/analytics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 블로그 분석 이벤트

GTM 웹 컨테이너는 `GTM-T7H7JNZ5`를 사용한다. GA4 측정 ID는 `G-SW73FSL7LQ`를 유지한다. GTM 웹 컨테이너의 Google 태그가 초기 페이지 조회와 SPA History 변화를 측정한다. 앱은 `page_view`를 직접 발행하지 않는다.

GTM 맞춤 이벤트 트리거는 아래 이벤트 이름에 정확히 일치시킨다. 전체 이벤트 정규식이나 History Change 트리거를 이 이벤트 태그에 연결하지 않는다.

| 이벤트 | 발생 시점 | 데이터 영역의 매개변수 |
|---|---|---|
| `view_post` | 게시글 화면 진입 | `post_title`, `post_slug` |
| `search` | 검색 창에서 검색 결과 선택 | `search_term`, `result_count` |

앱이 두 이벤트와 함께 `page_location`·`page_title`을 데이터 영역에 전달하고, GA4 이벤트 태그에서 이 값을 연결해 SPA 이동 전 주소에 이벤트가 연결되지 않도록 한다. 페이지뷰용 GA4 이벤트 태그를 추가하지 않는다.

게시 후 첫 접속, 게시글 이동, 검색 결과 이동, 뒤로 가기에서 `page_view`가 화면당 한 번 전송되는지 확인한다. `view_post`에는 글 제목·slug, `search`에는 검색어·결과 수가 전달되는지도 실제 수집 요청으로 확인한다. 전송 요청과 GA4 표준 보고서 반영은 별도로 확인한다.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-NZLJVG36');</script>
})(window,document,'script','dataLayer','GTM-T7H7JNZ5');</script>
<!-- End Google Tag Manager -->
<script>
// Prevent theme FOUC
Expand All @@ -29,7 +29,7 @@
</head>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-NZLJVG36"
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-T7H7JNZ5"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<div id="root"></div>
Expand Down
22 changes: 1 addition & 21 deletions src/layouts/RootLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react"
import { useEffect } from "react"
import { Outlet, useLocation } from "react-router-dom"
import { SidebarProvider, SidebarInset, useSidebar } from "@/components/ui/sidebar"
import { Button } from "@/components/ui/button"
Expand All @@ -14,28 +14,9 @@ import { Confetti } from "@/components/Confetti"
import { LanguageSuggestionBanner } from "@/components/LanguageSuggestionBanner"
import { NavigationStatus } from "@/components/NavigationStatus"
import { RoutePreloader } from "@/components/RoutePreloader"
import { trackPageView } from "@/lib/analytics"
import { getRouteLanguage } from "@/lib/i18n-routing"
import { useLanguage } from "@/i18n"

function RouteAnalytics() {
const location = useLocation()
const isInitialPageLoad = useRef(true)

useEffect(() => {
if (isInitialPageLoad.current) {
isInitialPageLoad.current = false
return
}

const path = `${location.pathname}${location.search}`
const timeoutId = window.setTimeout(() => trackPageView(path), 0)
return () => window.clearTimeout(timeoutId)
}, [location.pathname, location.search])

return null
}

function RouteLanguageSync() {
const location = useLocation()
const { language, setLanguage } = useLanguage()
Expand Down Expand Up @@ -73,7 +54,6 @@ export function RootLayout() {
<NavigationStatus />
<RoutePreloader />
<ScrollToTop />
<RouteAnalytics />
<RouteLanguageSync />
<Confetti />
<AppSidebar />
Expand Down
14 changes: 4 additions & 10 deletions src/lib/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@ function pushDataLayer(eventName: string, params?: AnalyticsParams) {
function trackEvent(eventName: string, params?: AnalyticsParams) {
if (import.meta.env.DEV || /^(localhost|127\.0\.0\.1)$/.test(window.location.hostname)) return

const eventParams = { page_location: window.location.href, page_title: document.title, ...params }

if (window.gtag) {
window.gtag("event", eventName, params)
window.gtag("event", eventName, eventParams)
return
}

pushDataLayer(eventName, params)
}

export function trackPageView(path: string) {
trackEvent("page_view", {
page_path: path,
page_location: `${window.location.origin}${path}`,
page_title: document.title,
})
pushDataLayer(eventName, eventParams)
}

export const analytics = {
Expand Down
Loading