Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit 2528775

Browse files
committed
fix(blog): prevent duplicate blog_post_time_spent events
- Add trackedTimeSpent ref to track if time-spent event already fired - Check flag in visibilitychange handler and cleanup function - Remove duplicate interface declaration in BlogPostAnalyticsProps - Remove duplicate BlogPostAnalytics component opening tag
1 parent 610599f commit 2528775

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

apps/web-roo-code/src/app/blog/[slug]/page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ export default async function BlogPostPage({ params }: BlogPostPageProps) {
8989
<script type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(breadcrumbSchema) }} />
9090

9191
{/* PostHog Analytics */}
92-
<BlogPostAnalytics
9392
<BlogPostAnalytics
9493
post={{
9594
slug: post.slug,

apps/web-roo-code/src/components/blog/blog-analytics.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export function BlogIndexAnalytics({ postCount }: BlogIndexAnalyticsProps) {
2525
return null
2626
}
2727

28-
interface BlogPostAnalyticsProps {
2928
interface BlogPostAnalyticsProps {
3029
post: {
3130
slug: string
@@ -44,6 +43,7 @@ interface BlogPostAnalyticsProps {
4443
export function BlogPostAnalytics({ post }: BlogPostAnalyticsProps) {
4544
const trackedView = useRef(false)
4645
const trackedDepths = useRef<Set<25 | 50 | 75 | 100>>(new Set())
46+
const trackedTimeSpent = useRef(false)
4747
const startTime = useRef<number>(Date.now())
4848

4949
useEffect(() => {
@@ -74,7 +74,8 @@ export function BlogPostAnalytics({ post }: BlogPostAnalyticsProps) {
7474

7575
// Track time spent on page when leaving
7676
const handleVisibilityChange = () => {
77-
if (document.visibilityState === "hidden") {
77+
if (document.visibilityState === "hidden" && !trackedTimeSpent.current) {
78+
trackedTimeSpent.current = true
7879
const timeSpent = Date.now() - effectStartTime
7980
trackBlogPostTimeSpent(post as BlogPost, timeSpent)
8081
}
@@ -90,9 +91,12 @@ export function BlogPostAnalytics({ post }: BlogPostAnalyticsProps) {
9091
window.removeEventListener("scroll", handleScroll)
9192
document.removeEventListener("visibilitychange", handleVisibilityChange)
9293

93-
// Track time spent when component unmounts
94-
const timeSpent = Date.now() - effectStartTime
95-
trackBlogPostTimeSpent(post as BlogPost, timeSpent)
94+
// Track time spent when component unmounts (only if not already tracked)
95+
if (!trackedTimeSpent.current) {
96+
trackedTimeSpent.current = true
97+
const timeSpent = Date.now() - effectStartTime
98+
trackBlogPostTimeSpent(post as BlogPost, timeSpent)
99+
}
96100
}
97101
// eslint-disable-next-line react-hooks/exhaustive-deps
98102
}, [post.slug])

0 commit comments

Comments
 (0)