insightpro-ai-sdk-react-native
v1.0.0
Published
InsightPro React Native SDK for mobile event tracking
Maintainers
Readme
InsightPro React Native SDK
Mobile event tracking SDK for React Native (iOS & Android).
Features
- ✅ Auto-tracking: Screen views and app lifecycle events
- ✅ Session Management: Automatic session tracking with timeout
- ✅ Event Batching: Efficient batching and buffering
- ✅ Offline Support: Events queued when offline
- ✅ Device Info: Automatic device and platform metadata
- ✅ TypeScript Support: Full TypeScript definitions
- ✅ React Navigation Integration: Automatic screen tracking
Installation
npm install @insightpro/sdk-react-native
# or
yarn add @insightpro/sdk-react-nativeiOS
cd ios && pod installAndroid
No additional steps required.
Quick Start
Initialize SDK
import InsightPro from '@insightpro/sdk-react-native';
const analytics = new InsightPro({
endpoint: 'https://api.insightpro.com',
tenantId: 'your-tenant-id',
apiKey: 'your-api-key', // Optional
debug: __DEV__, // Enable debug logs in development
});Track Events
Screen Views
// Manual screen tracking
analytics.trackScreenView('Home', {
category: 'main',
});
// With React Navigation
import { useNavigationContainerRef } from '@react-navigation/native';
const navigationRef = useNavigationContainerRef();
<NavigationContainer
ref={navigationRef}
onStateChange={() => {
const currentRoute = navigationRef.getCurrentRoute();
analytics.trackScreenView(currentRoute?.name || 'Unknown');
}}
>
{/* Your screens */}
</NavigationContainer>E-commerce
// Product view
analytics.trackEcommerce({
action: 'view_item',
productId: 'prod-123',
productName: 'Awesome Product',
price: 99.99,
currency: 'USD',
});
// Purchase
analytics.trackEcommerce({
action: 'purchase',
orderId: 'order-456',
revenue: 199.98,
currency: 'USD',
});User Actions
analytics.trackAction('button_press', {
buttonName: 'Subscribe',
screen: 'Home',
});Errors
try {
// Your code
} catch (error) {
analytics.trackError(error.message, {
stack: error.stack,
level: 'error',
});
}User Identification
// After login
analytics.identify('user-123', {
email: '[email protected]',
name: 'John Doe',
plan: 'premium',
});
// After logout
analytics.reset();React Navigation Integration
import { NavigationContainer } from '@react-navigation/native';
import { useNavigationContainerRef } from '@react-navigation/native';
function App() {
const navigationRef = useNavigationContainerRef();
const routeNameRef = useRef<string>();
return (
<NavigationContainer
ref={navigationRef}
onReady={() => {
routeNameRef.current = navigationRef.getCurrentRoute()?.name;
}}
onStateChange={async () => {
const previousRouteName = routeNameRef.current;
const currentRouteName = navigationRef.getCurrentRoute()?.name;
if (previousRouteName !== currentRouteName) {
analytics.trackScreenView(currentRouteName || 'Unknown', {
previousScreen: previousRouteName,
});
}
routeNameRef.current = currentRouteName;
}}
>
{/* Your navigation stack */}
</NavigationContainer>
);
}Configuration Options
{
/** API endpoint URL (required) */
endpoint: string;
/** Tenant ID (required) */
tenantId: string;
/** API key for authentication (optional) */
apiKey?: string;
/** Enable debug logging (default: false) */
debug?: boolean;
/** Batch size for event buffering (default: 10) */
batchSize?: number;
/** Batch interval in milliseconds (default: 5000) */
batchInterval?: number;
/** Enable automatic screen view tracking (default: true) */
autoTrackScreenViews?: boolean;
/** Enable session tracking (default: true) */
enableSessions?: boolean;
/** Session timeout in milliseconds (default: 30 minutes) */
sessionTimeout?: number;
/** Custom properties to include with every event */
globalProperties?: Record<string, any>;
/** Retry failed requests (default: true) */
retryFailedRequests?: boolean;
/** Maximum retry attempts (default: 3) */
maxRetries?: number;
}Device Information
The SDK automatically collects:
- Device ID (persisted across app launches)
- Platform (iOS/Android)
- OS Version
- App Version
- Device Manufacturer
- Device Model
- Locale
Offline Support
Events are automatically queued when the device is offline and sent when connectivity is restored.
TypeScript Support
Full TypeScript definitions are included:
import InsightPro, {
InsightProConfig,
EcommerceEvent,
} from '@insightpro/sdk-react-native';
const config: InsightProConfig = {
endpoint: 'https://api.insightpro.com',
tenantId: 'your-tenant-id',
debug: true,
};
const analytics = new InsightPro(config);License
MIT
