feat: GTM 태그와 이벤트 추적(각·글 보기, 공유, 로그인·가입, 지역 변경)

This commit is contained in:
minaChoi committed 2026-10-08 10:54:18 +09:00
1 parent e8382f450b
commit ce6efe979a
5 files changed
+102 -1

No files matched your search

+23 -1
View File
@@ -20,6 +20,7 @@ import {
updateMeMutation,
} from '../api/client/@tanstack/react-query.gen'
import type { CommentData, Factor, ResAuth, ResCommentList, ResIndexDetail } from '../api/client/types.gen'
import { track } from '../lib/analytics'
import { cx } from '../lib/utils'
import { useAuth } from './authContext'
import { ago, ARTICLE_CAT, articleMeta, AVATAR_COLORS, cardBg, CHARACTERS, dateRange, loadOf, MOODS, shortDay } from './lib'
@@ -301,6 +302,14 @@ function GakSheet({ indexId, onTitle }: { indexId: number; onTitle: (t: string)
onTitle(g ? `${g.rank}위 ${g.name}` : '계산 근거')
}, [g, onTitle])
// 지역이 바뀌어 다시 받아도 한 번만 보낸다
const tracked = useRef(0)
useEffect(() => {
if (!g || tracked.current === g.index_id) return
tracked.current = g.index_id
track('view_gak', { index_id: g.index_id, gak_name: g.name, score: g.score, rank: g.rank })
}, [g])
const load = loadOf(q)
if (!g) return <Body>{load === 'loading' ? <GakSkel /> : <StateBox load={load} onRetry={() => q.refetch()} empty="이 각의 계산 근거가 아직 없어요." hold={<GakSkel />} className="inset-0" />}</Body>
const factors = d.factors ?? []
@@ -393,6 +402,13 @@ function ArticleSheet({ articleId, onTitle }: { articleId: number; onTitle: (t:
onTitle(a ? (ARTICLE_CAT[a.category]?.[0] ?? '') : '')
}, [a, onTitle])
const tracked = useRef(0)
useEffect(() => {
if (!a || tracked.current === a.article_id) return
tracked.current = a.article_id
track('select_content', { content_type: 'article', item_id: a.article_id, category: ARTICLE_CAT[a.category]?.[0] ?? a.category })
}, [a])
const load = loadOf(q)
if (!a) return <Body>{load === 'loading' ? <ArticleSkel /> : <StateBox load={load} onRetry={() => q.refetch()} empty="글을 찾지 못했어요." hold={<ArticleSkel />} className="inset-0" />}</Body>
return (
@@ -493,7 +509,10 @@ function ShareSheet({ share }: { share: SharePayload }) {
const file = new File([image.blob], `nalssigak-${SHARE_SIZES[size].ratio.replace(':', 'x')}.png`, { type: 'image/png' })
if (navigator.canShare?.({ files: [file], text })) {
navigator.share({ files: [file], title: share.title, text }).then(
() => setToast('보냈어요'),
() => {
setToast('보냈어요')
track('share', { content_type: 'image', item_id: share.title, method: 'share_sheet', ratio: SHARE_SIZES[size].ratio })
},
(e: unknown) => {
if (e instanceof DOMException && e.name === 'AbortError') return
console.error('[share] send', e)
@@ -506,6 +525,7 @@ function ShareSheet({ share }: { share: SharePayload }) {
a.href = image.src
a.download = file.name
a.click()
track('share', { content_type: 'image', item_id: share.title, method: 'download', ratio: SHARE_SIZES[size].ratio })
if (!navigator.clipboard) {
setToast('이미지를 내려받았어요')
return
@@ -750,6 +770,7 @@ function LoginSheet({ onDone }: { onDone: () => void }) {
return
}
setTokens(res.access_token, res.refresh_token ?? null)
track(mode === 'login' ? 'login' : 'sign_up', { method: 'id' })
onDone()
}
const fail = () => setError('서버에 닿지 못했어요. 잠시 뒤에 다시 해 주세요.')
@@ -952,6 +973,7 @@ function RegionSheet({ onDone }: { onDone: () => void }) {
aria-current={r.code === region.code ? 'true' : undefined}
onClick={() => {
setRegion(r)
track('region_change', { region_name: r.name, method: 'list' })
onDone()
}}
className={cx('flex w-full cursor-pointer items-center justify-between border-b-[1.5px] border-(--line) px-1 py-3 text-left font-jua text-[17px]', r.code === region.code && 'text-(--orange)')}
+2
View File
@@ -1,6 +1,7 @@
import { useCallback, useEffect, useMemo, useState, type ReactNode } from 'react'
import { nearestRegion } from '../api/client/sdk.gen'
import type { RegionData } from '../api/client/types.gen'
import { track } from '../lib/analytics'
import { DEFAULT_REGION, RegionContext, type LocateStatus } from './regionContext'
const KEY = 'nalssigak.b.region'
@@ -43,6 +44,7 @@ export function RegionProvider({ children }: { children: ReactNode }) {
if (!res.region) throw new Error('가까운 구를 찾지 못함')
setRegion(res.region)
setLocateStatus('idle')
track('region_change', { region_name: res.region.name, method: 'location' })
})
.catch(() => setLocateStatus('error'))
},
+21
View File
@@ -0,0 +1,21 @@
/** GTM으로 보내는 이벤트와 값. 목록과 GTM 설정은 docs/이벤트_추적.md */
type Events = {
view_gak: { index_id: number; gak_name: string; score: number; rank: number }
select_content: { content_type: 'article'; item_id: number; category: string }
share: { content_type: 'image'; item_id: string; method: 'share_sheet' | 'download'; ratio: string }
login: { method: 'id' }
sign_up: { method: 'id' }
region_change: { region_name: string; method: 'list' | 'location' }
}
declare global {
interface Window {
dataLayer?: Record<string, unknown>[]
}
}
export function track<E extends keyof Events>(event: E, params: Events[E]) {
if (import.meta.env.DEV) return
window.dataLayer ??= []
window.dataLayer.push({ event, ...params })
}