The application was experiencing network errors when PostHog tried to flush analytics data:
Error while flushing PostHog [PostHogFetchNetworkError: Network error while fetching PostHog]
Implemented a comprehensive error handling system for PostHog network failures:
- Purpose: Centralized error handling and retry logic for PostHog
- Features:
- Tracks retry attempts (default: 3 max retries)
- Temporarily disables PostHog after max retries
- Automatic re-enabling after 5 minutes
- Comprehensive logging of errors and state changes
- Purpose: Wraps the PostHog provider with error handling
- Features:
- Catches console errors related to PostHog
- Uses the PostHog service for error management
- Gracefully falls back to rendering without PostHog when disabled
- Conservative configuration to reduce network calls
- Purpose: Uses the new PostHog wrapper instead of direct PostHog provider
- Change: Replaced
PostHogProviderwithPostHogProviderWrapper
- When PostHog encounters network errors, the app continues to function normally
- Analytics are temporarily disabled rather than causing app crashes
- User experience remains unaffected
- PostHog is automatically re-enabled after 5 minutes
- Retry counter is reset upon recovery
- System logs recovery events for monitoring
- Reduced flush frequency (60 seconds instead of 10)
- Smaller batch sizes (10 events instead of 20)
- 10-second request timeout
- Comprehensive error logging
The implementation includes comprehensive unit tests:
- Error handling logic
- Retry mechanism
- Disable/enable functionality
- Status tracking
- Timer-based recovery
- Component rendering with PostHog enabled/disabled
- Error handling integration
- Configuration validation
- Navigation ref support
- Service integration
All tests pass successfully and provide good coverage of the error handling functionality.
- Reliability: App no longer crashes due to PostHog network errors
- User Experience: Seamless operation even when analytics fail
- Monitoring: Comprehensive logging for debugging
- Performance: Reduced network calls and optimized configuration
- Resilience: Automatic recovery from network issues
The system uses environment variables for PostHog configuration:
POSTHOG_API_KEY: PostHog API keyPOSTHOG_HOST: PostHog server URL (default: https://us.i.posthog.com)
When no API key is provided, the app runs without PostHog entirely.