UNPKG

mixpanel-react-native

Version:

Official React Native Tracking Library for Mixpanel Analytics

134 lines (107 loc) โ€ข 6 kB
# 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