Skip to content

Conversation

@codeunia-dev
Copy link
Owner

@codeunia-dev codeunia-dev commented Nov 13, 2025

  • Add session-based deduplication for event view tracking
  • Create new useViewTracking hook with session management logic
  • Refactor event view tracking API route to use AnalyticsService
  • Implement client-side session tracking to prevent duplicate views
  • Add logging and error handling for view tracking process
  • Improve analytics tracking reliability and performance Enhances event view tracking by preventing multiple view counts from the same user session and providing more robust tracking mechanisms.

Summary by CodeRabbit

  • New Features

    • Session-based view deduplication prevents duplicate view counts within the same browsing session.
  • Improvements

    • Enhanced analytics tracking reliability with improved error handling.
    • Extended tracking delay to ensure more accurate user interaction measurement.
    • Improved view counting accuracy with streamlined tracking mechanism.

- Add session-based deduplication for event view tracking
- Create new `useViewTracking` hook with session management logic
- Refactor event view tracking API route to use `AnalyticsService`
- Implement client-side session tracking to prevent duplicate views
- Add logging and error handling for view tracking process
- Improve analytics tracking reliability and performance
Enhances event view tracking by preventing multiple view counts from the same user session and providing more robust tracking mechanisms.
@codeunia-dev codeunia-dev merged commit 56ff944 into main Nov 13, 2025
1 of 2 checks passed
@vercel
Copy link

vercel bot commented Nov 13, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
codeunia Building Building Preview Comment Nov 13, 2025 11:16am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 13, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Session-based view deduplication is introduced across analytics tracking. The API route now uses AnalyticsService instead of direct database queries. New view tracking hooks manage per-session deduplication via sessionStorage. The analytics service includes RPC fallback mechanisms and refined error handling to gracefully degrade.

Changes

Cohort / File(s) Summary
API Route Refactoring
app/api/events/[slug]/track-view/route.ts
Replaced direct Supabase queries with AnalyticsService.trackEventView(). Added Node.js runtime specification and sessionId extraction from request body (400 if missing). Removed events table queries and company daily analytics RPCs. On error, logs and returns 200 with skipped message.
Debug Logging
app/events/[slug]/page.tsx
Added console.log statements before and after useAnalyticsTracking hook invocation to log slug and trackClick reference for debugging.
Analytics Tracking Hooks
hooks/useAnalyticsTracking.ts, hooks/useViewTracking.ts
Introduced session-based utilities (getSessionId, hasViewedInSession, markAsViewed) for per-session view deduplication. useAnalyticsTracking now checks viewed lists before sending requests and persists state via sessionStorage. Added default values for trackView (true) and trackClick (false). Increased tracking delay from 1s to 2s. Expanded logging for initialization and decisions. useViewTracking is a new hook for focused event view tracking with session deduplication, 2s delay, and graceful error handling.
Analytics Service
lib/services/analytics-service.ts
Updated trackEventView to filter events by approval_status = 'approved'. Replaced direct view increment with RPC call increment_event_views(event_id), with fallback to direct update on RPC failure. Wrapped company analytics update in try/catch to avoid propagating failures. Improved error handling to distinguish event-not-found from fetch errors.

Sequence Diagram(s)

sequenceDiagram
    participant Page as EventPage
    participant Hook as useViewTracking
    participant Storage as sessionStorage
    participant API as /track-view API
    participant Service as AnalyticsService
    participant DB as Database

    Page->>Hook: Mount with eventSlug
    Hook->>Storage: Get/Generate sessionId
    Hook->>Storage: Check viewed_events list
    alt Already viewed in session
        Hook->>Hook: Mark as tracked, exit
    else First view this session
        Note over Hook: Wait 2 seconds
        Hook->>API: POST /api/events/{slug}/track-view<br/>(with sessionId)
        API->>Service: trackEventView(slug, sessionId)
        Service->>DB: Filter events by approved status
        Service->>DB: Call RPC increment_event_views
        alt RPC succeeds
            Service-->>API: ✓ Success
        else RPC fails
            Service->>DB: Fallback: Direct update views
            Service-->>API: ✓ Success
        end
        API-->>Hook: 200 OK
        Hook->>Storage: Add eventSlug to viewed_events
        Hook->>Hook: Set hasTracked = true
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • hooks/useAnalyticsTracking.ts — Heterogeneous changes including new session utilities, expanded tracking logic, modified signatures, and added logging; each branch warrants separate reasoning.
  • hooks/useViewTracking.ts — New hook with session deduplication, sessionStorage interactions, and timer-based delayed tracking; requires careful review of timing and cleanup.
  • lib/services/analytics-service.ts — RPC fallback logic and try/catch error handling adjustments; verify that graceful degradation doesn't mask critical failures.
  • app/api/events/[slug]/track-view/route.ts — Confirm sessionId extraction, validation, and proper error status codes (400 vs. 200 for skipped).

Poem

🐰 A hop through sessions, views now blessed,
Dedup with storage, put to test!
Two seconds delay, no bot's disguise,
Session IDs guard what's true and wise.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/session-based-view-tracking

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e4dd194 and c7210dd.

📒 Files selected for processing (5)
  • app/api/events/[slug]/track-view/route.ts (1 hunks)
  • app/events/[slug]/page.tsx (1 hunks)
  • hooks/useAnalyticsTracking.ts (2 hunks)
  • hooks/useViewTracking.ts (1 hunks)
  • lib/services/analytics-service.ts (1 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants