Skip to content

新增分析功能以追蹤語言選擇和課程進度,並優化用戶識別邏輯#13

Merged
Retsomm merged 2 commits into
mainfrom
dev
May 20, 2026
Merged

新增分析功能以追蹤語言選擇和課程進度,並優化用戶識別邏輯#13
Retsomm merged 2 commits into
mainfrom
dev

Conversation

@Retsomm
Copy link
Copy Markdown
Owner

@Retsomm Retsomm commented May 20, 2026

Summary by CodeRabbit

  • Refactoring

    • Consolidated analytics for language selection and lesson lifecycle into a single tracking layer.
    • Integrated user identity with authentication and preferred language to improve event attribution.
  • Bug Fixes

    • Improved accuracy of lesson start/completion and abandonment tracking so interactions are recorded more reliably.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 09295811-ef26-400c-9648-8ce7612229a1

📥 Commits

Reviewing files that changed from the base of the PR and between 4a282ea and 204bcac.

📒 Files selected for processing (1)
  • app/lesson/[lessonId].tsx

📝 Walkthrough

Walkthrough

This PR refactors PostHog analytics integration by consolidating event and identification logic into lib/analytics.ts, then updates all analytics callsites to use the new helpers. App-level user identification is wired in _layout.tsx tied to Clerk auth and language selection. Auth flows, language selection, and lesson tracking now emit events through the centralized helpers.

Changes

PostHog Analytics Refactor

Layer / File(s) Summary
PostHog analytics helper library
lib/analytics.ts
New identifyPostHogUser, captureLanguageSelected, captureLessonStarted, and captureLessonAbandoned functions that abstract PostHog event capture and user identification with consistent property schemas.
App-level user identification setup
app/_layout.tsx
Added PostHogUserIdentifier component that calls identifyPostHogUser on mount and whenever userId or selected language changes, using ref deduplication to prevent duplicate calls.
Auth flows with language identification
components/auth-screen.tsx
Updated signup, signin, and social auth completion paths to call identifyPostHogUser, passing language preference and signup flag from useLanguageStore and SSO metadata.
Language selection event capture
app/LanguageSelection.tsx
Removed usePostHog hook and replaced direct PostHog calls with captureLanguageSelected invoked on confirmation; language row selection now only updates state.

Sequence Diagram

sequenceDiagram
  participant User
  participant App as RootLayout
  participant AuthScreen
  participant LessonScreen
  participant identifyPostHogUser as Analytics.identifyPostHogUser
  participant captureLanguageSelected as Analytics.captureLanguageSelected
  participant captureLessonStarted as Analytics.captureLessonStarted
  participant captureLessonAbandoned as Analytics.captureLessonAbandoned
  participant PostHog

  User->>App: Load app
  App->>identifyPostHogUser: userId + languageId ready
  identifyPostHogUser->>PostHog: identify user with language

  User->>AuthScreen: Complete signup/signin
  AuthScreen->>identifyPostHogUser: userId + isSignUp + language
  identifyPostHogUser->>PostHog: identify/update user

  User->>App: Select language
  App->>captureLanguageSelected: language object
  captureLanguageSelected->>PostHog: emit language_selected event

  User->>LessonScreen: Enter lesson
  LessonScreen->>captureLessonStarted: lesson + languageName
  captureLessonStarted->>PostHog: emit lesson_started event

  User->>LessonScreen: Leave without completing
  LessonScreen->>captureLessonAbandoned: lesson metadata + time_into_lesson_seconds
  captureLessonAbandoned->>PostHog: emit lesson_abandoned event
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • Retsomm/react-native-lingua#9: Both PRs modify app/lesson/[lessonId].tsx—PR #9 introduced the screen, while this PR adds lesson start and abandonment analytics tracking.
  • Retsomm/react-native-lingua#8: Both PRs refactor PostHog instrumentation in app/LanguageSelection.tsx and components/auth-screen.tsx—PR #8 originally added those capture/identify calls, while this PR extracts them into lib/analytics.ts.

Poem

🐰 I hopped through code with a tape and a pen,
Moved analytics to helpers again and again,
User and language now hold hands in the log,
From signup to lesson, neat as a cog,
Hooray — events hop along down the analytics glen!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: adding analytics for language selection and lesson progress tracking, and optimizing user identification logic across multiple files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch dev

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

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@app/lesson/`[lessonId].tsx:
- Around line 15-45: The cleanup is firing captureLessonAbandoned because
didCompleteLessonRef.current is never set true; locate the place(s) that mark a
lesson as finished (e.g., the function that calls captureLessonCompleted or the
onCallEnded/navigation handler) and set didCompleteLessonRef.current = true
there (before any navigation/unmount or calling captureLessonCompleted),
ensuring lessonStartTimeRef and lastQuestionIndexRef remain intact so the
existing useEffect cleanup will skip captureLessonAbandoned when a lesson
actually completed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: bac12b90-f795-4a58-b66b-0fc5fe407322

📥 Commits

Reviewing files that changed from the base of the PR and between 164543b and 4a282ea.

📒 Files selected for processing (5)
  • app/LanguageSelection.tsx
  • app/_layout.tsx
  • app/lesson/[lessonId].tsx
  • components/auth-screen.tsx
  • lib/analytics.ts

Comment thread app/lesson/[lessonId].tsx
@Retsomm Retsomm merged commit 385614e into main May 20, 2026
1 check passed
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.

1 participant