From 7be2be5d456e9fa3490afa08d591f40bd8662a7d Mon Sep 17 00:00:00 2001 From: Zeck Li <11781254+zeckli@users.noreply.github.com> Date: Mon, 16 Dec 2024 23:26:25 +0800 Subject: [PATCH] feat(tracker): replace articleId with shortHash for the article read timer --- src/common/utils/analytics.ts | 2 +- src/components/Hook/useReadTimer.ts | 10 ++++++---- src/views/ArticleDetail/Content/index.tsx | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/common/utils/analytics.ts b/src/common/utils/analytics.ts index 43db1a8f84..e86013ea12 100644 --- a/src/common/utils/analytics.ts +++ b/src/common/utils/analytics.ts @@ -241,7 +241,7 @@ interface AuthenticateProp { } interface ReadTimeProp { - articleId: string + shortHash: string time: number } diff --git a/src/components/Hook/useReadTimer.ts b/src/components/Hook/useReadTimer.ts index 02f4a3efb2..24fabc7f44 100644 --- a/src/components/Hook/useReadTimer.ts +++ b/src/components/Hook/useReadTimer.ts @@ -5,11 +5,10 @@ import { useEffect, useRef } from 'react' import { analytics } from '~/common/utils' type Props = { - articleId: string container?: React.RefObject } -export const useReadTimer = ({ articleId, container }: Props) => { +export const useReadTimer = ({ container }: Props) => { const router = useRouter() //in ms @@ -25,11 +24,14 @@ export const useReadTimer = ({ articleId, container }: Props) => { }, 3000) const storeReadTime = () => { - if (articleId && readTimer) + if (router?.query?.shortHash && readTimer) { + const value = router.query.shortHash + const shortHash = value instanceof Array ? value[0] : value || '' analytics.trackEvent('read_time', { - articleId, + shortHash, time: readTimer.current, }) + } } window.addEventListener('scroll', handleScroll) diff --git a/src/views/ArticleDetail/Content/index.tsx b/src/views/ArticleDetail/Content/index.tsx index ee3570620b..e6ae20cd32 100644 --- a/src/views/ArticleDetail/Content/index.tsx +++ b/src/views/ArticleDetail/Content/index.tsx @@ -119,7 +119,7 @@ const Content = ({ } }, [lastScroll]) - useReadTimer({ articleId, container: contentContainer }) + useReadTimer({ container: contentContainer }) return ( <>