Skip to content

feat(mobile): add PostHog analytics with autocapture and screen tracking#932

Merged
saddlepaddle merged 2 commits intomainfrom
posthogmb
Jan 24, 2026
Merged

feat(mobile): add PostHog analytics with autocapture and screen tracking#932
saddlepaddle merged 2 commits intomainfrom
posthogmb

Conversation

@saddlepaddle
Copy link
Copy Markdown
Collaborator

@saddlepaddle saddlepaddle commented Jan 24, 2026

Summary

  • Integrates PostHog React Native SDK for analytics in the mobile app
  • Adds automatic screen tracking using expo-router's usePathname
  • Enables touch autocapture for button/interaction tracking
  • Sets up user identification when authenticated

Changes

  • Added posthog-react-native and required Expo dependencies (expo-application, expo-device, expo-file-system, expo-localization)
  • Created PostHogProvider wrapper with screen tracking and debug mode support
  • Created PostHogUserIdentifier component for authenticated user identification
  • Added PostHog config to lib/posthog/ with environment variable support

Test plan

  • Verified events flow to PostHog dashboard (identify, screen, autocapture)
  • Confirmed expo-file-system v19 works natively without AsyncStorage workaround
  • Lint and typecheck pass

Summary by CodeRabbit

  • New Features

    • Integrated comprehensive analytics and user session tracking capabilities throughout the mobile application
  • Chores

    • Added new project dependencies including analytics library, device utilities, localization framework, and enhanced async storage management
    • Extended environment configuration with analytics-related settings and variables
    • Reorganized application initialization structure and root layout components for improved code organization

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 24, 2026

📝 Walkthrough

Walkthrough

This PR integrates PostHog analytics into the mobile app by adding environment configuration for PostHog credentials, creating a PostHog client and provider, refactoring the root layout to manage QueryClient and PostHog providers, and implementing components for tracking user identity and initializing analytics settings.

Changes

Cohort / File(s) Summary
Configuration & Dependencies
apps/mobile/app.config.ts, apps/mobile/lib/env.ts, apps/mobile/package.json
Added expo-localization to Expo plugins. Introduced EXPO_PUBLIC_POSTHOG_KEY and EXPO_PUBLIC_POSTHOG_HOST environment variables with URL default. Added posthog-react-native, expo-localization, and updated async-storage dependencies.
PostHog Library Setup
apps/mobile/lib/posthog/client.ts, apps/mobile/lib/posthog/index.ts
Created PostHog configuration object importing env variables, defining apiKey, host, and debug/session replay options. Exported via index module for public access.
Root Layout Refactoring & Providers
apps/mobile/app/_layout.tsx, apps/mobile/screens/RootLayout/RootLayout.tsx, apps/mobile/screens/RootLayout/index.ts, apps/mobile/screens/RootLayout/providers/PostHogProvider/...
Moved RootLayout implementation from app/_layout.tsx to screens directory. New RootLayout wraps app in QueryClientProvider and PostHogProvider, renders Stack with PortalHost. PostHogProvider initializes PostHog with app registration, debug mode, and screen view tracking via pathname changes.
User Identity Tracking
apps/mobile/screens/RootLayout/components/PostHogUserIdentifier/PostHogUserIdentifier.tsx, apps/mobile/screens/RootLayout/components/PostHogUserIdentifier/index.ts
Introduced PostHogUserIdentifier component that subscribes to session changes, calling posthog.identify with user id and attributes when logged in, and posthog.reset on logout.

Sequence Diagrams

sequenceDiagram
    participant App as Mobile App
    participant RootLayout as RootLayout
    participant QueryProvider as QueryClientProvider
    participant PostHogProv as PostHogProvider
    participant PostHog as PostHog Instance
    participant PathTracker as PostHogInitializer

    App->>RootLayout: Mount
    RootLayout->>QueryProvider: Wrap children
    QueryProvider->>PostHogProv: Wrap children
    PostHogProv->>PostHog: Initialize with config
    PostHog-->>PostHogProv: Ready
    PathTracker->>PostHog: Register app (app_name: mobile)
    PathTracker->>PostHog: Enable debug if configured
    PathTracker->>PostHog: Capture initial screen view
    PostHogProv->>App: Render Stack + PortalHost
Loading
sequenceDiagram
    participant Session as Session State
    participant UserIdent as PostHogUserIdentifier
    participant PostHog as PostHog Instance

    Note over Session,PostHog: User logs in
    Session->>UserIdent: Session updated with user
    UserIdent->>PostHog: identify(userId, {email, name})
    PostHog-->>UserIdent: Identified
    
    Note over Session,PostHog: User logs out
    Session->>UserIdent: Session becomes null
    UserIdent->>PostHog: reset()
    PostHog-->>UserIdent: Reset
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Poem

🐰 Hop, skip, and PostHog trace,
Identity tracked in the user's space,
Screens paint their footprints across the land,
Analytics woven with a developer's hand.

🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main changes: adding PostHog analytics integration with autocapture and screen tracking to the mobile app.
Description check ✅ Passed The PR description covers the key aspects including summary, changes made, and a detailed test plan, though it deviates from the template structure with custom formatting.

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

✨ Finishing touches
  • 📝 Generate docstrings

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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 24, 2026

🧹 Preview Cleanup Complete

The following preview resources have been cleaned up:

  • ✅ Neon database branch
  • ✅ Electric Fly.io app

Thank you for your contribution! 🎉

- Configure PostHogProvider with touch autocapture
- Add manual screen tracking using expo-router's usePathname
- Register app_name super property for mobile events
- Identify users on auth with email/name
@saddlepaddle saddlepaddle merged commit 7d6474c into main Jan 24, 2026
13 checks passed
@Kitenite Kitenite deleted the posthogmb branch January 25, 2026 07:41
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