mixpanel-react-native
Version:
Official React Native Tracking Library for Mixpanel Analytics
134 lines (107 loc) โข 6 kB
Markdown
# Codebase Map: mixpanel-react-native
## Executive Summary
This is a React Native library implementing Mixpanel analytics with a **dual architecture strategy**: native iOS/Android modules for performance plus pure JavaScript fallback for Expo/Web compatibility. The codebase demonstrates sophisticated cross-platform patterns with token-based multi-instance support.
## ๐ฏ Entry Points
### Primary Library Interface
- `index.js` - Main export exposing Mixpanel, People, and MixpanelGroup classes
- `index.d.ts` - TypeScript definitions with comprehensive type coverage
- `package.json` - Library metadata, dependencies (AsyncStorage, uuid, expo-crypto)
### Configuration
- `react-native.config.js` - React Native autolinking configuration for native modules
- `MixpanelReactNative.podspec` - iOS CocoaPods specification
## ๐๏ธ Core Architecture Components
### Native Implementation (Performance Path)
```
ios/
โโโ MixpanelReactNative.swift # iOS bridge using Mixpanel Swift SDK
โโโ MixpanelReactNative.m # Objective-C bridge file
โโโ AutomaticProperties.swift # Device property collection
โโโ MixpanelTypeHandler.swift # Type conversion utilities
โโโ Constants.swift # iOS-specific constants
android/src/main/java/com/mixpanel/reactnative/
โโโ MixpanelReactNativeModule.java # Android bridge using Mixpanel Android SDK
โโโ MixpanelReactNativePackage.java # React Native package registration
โโโ AutomaticProperties.java # Device property collection
โโโ ReactNativeHelper.java # Utility functions
```
### JavaScript Implementation (Compatibility Path)
```
javascript/
โโโ mixpanel-main.js # JS mode entry point - full API implementation
โโโ mixpanel-core.js # Core tracking logic shared between modes
โโโ mixpanel-queue.js # Event queue management with auto-flush
โโโ mixpanel-network.js # HTTP client with retry logic
โโโ mixpanel-storage.js # AsyncStorage abstraction layer
โโโ mixpanel-persistent.js # State persistence management
โโโ mixpanel-config.js # Configuration management
โโโ mixpanel-logger.js # Centralized logging system
โโโ mixpanel-constants.js # Shared constants and enums
โโโ mixpanel-utils.js # Utility functions
```
## ๐งช Testing Infrastructure
### Test Organization
```
__tests__/
โโโ jest_setup.js # Test environment configuration
โโโ core.test.js # Core functionality tests
โโโ index.test.js # Main API tests
โโโ main.test.js # JavaScript implementation tests
โโโ network.test.js # HTTP client tests
โโโ queue.test.js # Queue management tests
__mocks__/
โโโ @react-native-async-storage/ # AsyncStorage mock for testing
โโโ async-storage.js
```
### Test Patterns
- **Comprehensive mocking**: All external dependencies (AsyncStorage, React Native modules)
- **Jest configuration**: Custom setup with React Native preset
- **Module isolation**: Each core component tested independently
## ๐ฑ Sample Applications
### Development/Testing Apps
```
Samples/
โโโ SimpleMixpanel/ # Basic integration example (TypeScript)
โโโ MixpanelDemo/ # Full-featured demo app
โโโ ContextAPIMixpanel/ # React Context API integration
โโโ MixpanelExpo/ # Expo-compatible example
```
Each sample includes complete React Native app structure with platform-specific builds.
## ๐ Documentation
### Generated Documentation
```
docs/ # JSDoc-generated API documentation
โโโ index.html # Main documentation entry
โโโ Mixpanel.html # Mixpanel class documentation
โโโ People.html # People Analytics documentation
โโโ MixpanelGroup.html # Group Analytics documentation
```
### Maintenance Scripts
- `generate_docs.sh` - JSDoc documentation generation script
- `release.py` - Release automation script
## ๐ Key Architectural Insights
### Multi-Instance Design
- **Token-based isolation**: Each Mixpanel project gets separate instance
- **Shared infrastructure**: Common queue, network, and storage layers
- **State persistence**: User IDs, super properties survive app restarts
### Cross-Platform Strategy
- **Automatic fallback**: Native โ JavaScript mode based on availability
- **Platform detection**: iOS/Android-specific behavior handling
- **Unified API**: Same interface regardless of implementation mode
### Performance Considerations
- **Batched flushing**: Events sent in configurable batches (default 50)
- **Background flushing**: Automatic data transmission on app state changes
- **Memory management**: In-memory queues with persistent backup
### Error Handling Philosophy
- **Graceful degradation**: JavaScript mode as native fallback
- **Input validation**: Comprehensive parameter checking with helpful errors
- **Logging strategy**: Centralized, configurable logging system
## ๐ง Configuration Patterns
### React Native Integration
- **Autolinking**: Automatic native module discovery
- **Manual linking support**: Fallback for older RN versions
- **Metro bundler**: Standard React Native build pipeline
### Dependencies Strategy [Updated: 2025-05-30]
- **Minimal external deps**: Only essential libraries (AsyncStorage, uuid, expo-crypto)
- **Platform compatibility**: Enhanced Expo support with expo-crypto for UUID generation
- **Version constraints**: Conservative dependency versioning
- **Graceful fallbacks**: expo-crypto optional, falls back to uuid package