Skip to content

Commit 8ee1eea

Browse files
committed
feat: gtag code refatcor
1 parent 523c67c commit 8ee1eea

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

packages/velog-web/src/features/home/components/PostCardGrid/PostCardGrid.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import PostCardSkeletonGrid from './PostCardSkeletonGrid'
1414
import { TrendingPost } from '../../interface/post'
1515
import PostCard from '../PostCard/PostCard'
1616
import { PostCardSkeleton } from '../PostCard/PostCardSkeleton'
17+
import useGtag from '@/hooks/useGtag'
1718

1819
const cx = bindClassNames(styles)
1920

@@ -36,6 +37,7 @@ function PostCardGrid({
3637
}: Props) {
3738
const params = useParams()
3839
const pathname = usePathname()
40+
const { gtag } = useGtag()
3941
const timeframe = (params.timeframe ?? 'week') as Timeframe
4042
const hasLoaded = useRef<boolean>(false)
4143
const hasClicked = useRef<boolean>(false)
@@ -61,13 +63,13 @@ function PostCardGrid({
6163
useEffect(() => {
6264
if (hasLoaded.current) return
6365
hasLoaded.current = true
64-
gtag('event', 'ads_feed_view')
65-
}, [])
66+
gtag('ads_feed_view')
67+
}, [gtag])
6668

6769
const onClick = () => {
6870
if (hasClicked.current) return
6971
hasClicked.current = true
70-
gtag('event', 'ads_feed_click')
72+
gtag('ads_feed_click')
7173
}
7274

7375
if (isLoading) return <PostCardSkeletonGrid forHome={forHome} forPost={forPost} />

packages/velog-web/src/hooks/useGtag.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client'
22

33
export default function useGtag() {
4-
function gtag(action: EventAction, eventOptions: EventOptions) {
4+
function gtag(action: EventAction, eventOptions?: EventOptions) {
55
const isProductionContainer = process.env.DOCKER_ENV === 'production'
66
if (!isProductionContainer) return
77
if (!window.gtag) return
@@ -10,7 +10,13 @@ export default function useGtag() {
1010
return { gtag }
1111
}
1212

13-
type EventAction = 'recommend_click' | 'recommend_guest_click' | 'ads_click' | Gtag.EventNames
13+
type EventAction =
14+
| 'recommend_click'
15+
| 'recommend_guest_click'
16+
| 'ads_click'
17+
| 'ads_feed_view'
18+
| 'ads_feed_click'
19+
| Gtag.EventNames
1420

1521
type EventOptions =
1622
| {

0 commit comments

Comments
 (0)