Skip to content

Fix: user:logged_in event never firesΒ #545

@aryanranderiya

Description

@aryanranderiya

Problem

Zero user:logged_in events in the last 30 days. The event is never being fired, which means:

  • No visibility into returning user sessions
  • DAU metric is blind
  • "Signup to first login" funnel returns no data

Root Cause

The frontend check in apps/web/src/features/auth/hooks/useFetchUser.ts (line 53) guards the tracking call behind:

if (accessToken || refreshToken) {
  trackEvent(ANALYTICS_EVENTS.USER_LOGGED_IN, { ... });
}

But the backend OAuth callback (/workos/callback in apps/api/app/api/v1/endpoints/oauth.py) redirects to /redirect with only an HttpOnly cookie set β€” it never passes access_token or refresh_token as URL query params. The condition is always false, so the event never fires.

Additionally, returning users who auto-login via the wos_session cookie have zero analytics visibility.

Proposed Fix

1. Backend: Track user:logged_in server-side in OAuth callback

  • Add USER_LOGGED_IN = "user:logged_in" to AnalyticsEvents in apps/api/app/services/analytics_service.py
  • Add a track_login() helper (mirrors existing track_signup() pattern)
  • In apps/api/app/services/oauth/oauth_service.py, modify store_user_info() to return (user_id, is_new_user) β€” call track_login() in the existing-user branch

2. Frontend: Add session resumption tracking

  • Remove the dead accessToken/refreshToken login tracking code from useFetchUser.ts
  • Add a user:session_resumed event using sessionStorage β€” fires once per browser session when a returning user is auto-authenticated via cookie
  • Add USER_SESSION_RESUMED to ANALYTICS_EVENTS in apps/web/src/lib/analytics.ts

Resulting Event Model

Scenario Event Tracked Where
New user signs up user:signed_up Backend (already works)
Existing user logs in via OAuth user:logged_in Backend (new)
Returning user auto-auth via cookie user:session_resumed Frontend (new)
User logs out user:logged_out Frontend (already works)

Key Files

  • apps/api/app/api/v1/endpoints/oauth.py β€” OAuth callback
  • apps/api/app/services/oauth/oauth_service.py β€” store_user_info()
  • apps/api/app/services/analytics_service.py β€” PostHog tracking helpers
  • apps/web/src/features/auth/hooks/useFetchUser.ts β€” frontend auth hook
  • apps/web/src/lib/analytics.ts β€” frontend analytics events

Created by Claude

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions