The @capgo/capacitor-appinsights package brings powerful native application analytics and insights to your Capacitor mobile applications. This comprehensive tutorial will guide you through integrating the Apptopia AppInsights SDK into your iOS and Android mobile apps built with Capacitor or Cordova. Whether you're developing a native mobile app, hybrid Capacitor application, or migrating from Cordova to Capacitor, this plugin provides cross-platform analytics for both iOS and Android platforms.
Capacitor is Ionic's native runtime that enables web developers to build iOS, Android, and Progressive Web Apps with a single codebase. Unlike traditional Cordova plugins, Capacitor provides modern native mobile app development with direct access to native iOS and Android APIs. This makes Capacitor ideal for building production-ready mobile applications that leverage native iOS features and Android capabilities.
Application analytics are critical for understanding user behavior in mobile applications. The @capgo/capacitor-appinsights plugin enables you to:
This Capacitor plugin wraps the Apptopia AppInsights SDK, providing a unified JavaScript API that works seamlessly on both iOS and Android mobile platforms, eliminating the need to write platform-specific native code for iOS or Android.
To install the @capgo/capacitor-appinsights package in your Capacitor mobile app, run the following command in your project's root directory:
npm install @capgo/capacitor-appinsights
npx cap sync
The npx cap sync command synchronizes the Capacitor plugin with your native iOS and Android projects, ensuring the AppInsights SDK is properly configured for both mobile platforms.
For iOS mobile apps, the plugin works out of the box. The Capacitor plugin automatically integrates the AppInsights SDK into your native iOS application. However, ensure your iOS app has proper tracking permissions configured in your Info.plist if you're tracking user data on iOS devices.
For Android mobile applications, the plugin integrates seamlessly with your native Android project. Capacitor handles the Android configuration automatically, but you may need to ensure your Android app has appropriate permissions in AndroidManifest.xml for analytics tracking on Android devices.
Import the plugin and initialize it with your AppInsights credentials. This works identically on both iOS and Android mobile platforms:
import { CapacitorAppinsights } from '@capgo/capacitor-appinsights';
// Initialize AppInsights for iOS and Android
async function initializeAnalytics() {
await CapacitorAppinsights.init({
partnerId: 'your-partner-id',
partnerKey: 'your-partner-key'
});
console.log('AppInsights initialized for mobile app');
}
// Call initialization when your Capacitor app starts
initializeAnalytics();
Track individual users across iOS and Android platforms by setting a unique user ID:
import { CapacitorAppinsights } from '@capgo/capacitor-appinsights';
// Set user ID for iOS and Android analytics
async function identifyUser(userId: string) {
await CapacitorAppinsights.setUserId({ userId });
console.log(`User ${userId} identified in mobile app`);
}
// Example: Identify user after login in your Capacitor mobile app
identifyUser('user_123456');
Monitor the AppInsights SDK status on both iOS and Android platforms:
import { CapacitorAppinsights } from '@capgo/capacitor-appinsights';
// Check SDK state on iOS or Android device
async function checkAnalyticsState() {
const state = await CapacitorAppinsights.getState();
console.log('AppInsights SDK State:');
console.log('Initialization complete:', state.initCompleted);
console.log('Job scheduled:', state.jobScheduled);
console.log('Permission acquired:', state.permissionAcquired);
return state;
}
// Use in your Capacitor mobile application
checkAnalyticsState();
Verify the Capacitor plugin version running on iOS or Android:
import { CapacitorAppinsights } from '@capgo/capacitor-appinsights';
// Get version on iOS and Android
async function getPluginInfo() {
const { version } = await CapacitorAppinsights.getPluginVersion();
console.log('AppInsights plugin version:', version);
}
Here's a production-ready analytics service for your iOS and Android mobile app:
import { CapacitorAppinsights } from '@capgo/capacitor-appinsights';
export class MobileAnalyticsService {
private initialized = false;
/**
* Initialize analytics for iOS and Android mobile platforms
*/
async initialize(partnerId: string, partnerKey: string) {
try {
// Initialize AppInsights SDK for native mobile app
await CapacitorAppinsights.init({
partnerId,
partnerKey
});
// Verify initialization on iOS or Android
const state = await CapacitorAppinsights.getState();
this.initialized = state.initCompleted;
if (this.initialized) {
console.log('Mobile analytics ready for iOS and Android');
}
return state;
} catch (error) {
console.error('Failed to initialize mobile analytics:', error);
throw error;
}
}
/**
* Identify user in mobile application
*/
async trackUser(userId: string) {
if (!this.initialized) {
throw new Error('Analytics not initialized for mobile app');
}
await CapacitorAppinsights.setUserId({ userId });
console.log(`User ${userId} tracked on mobile device`);
}
/**
* Get analytics status for iOS or Android
*/
async getStatus() {
const state = await CapacitorAppinsights.getState();
return {
ready: state.initCompleted && state.permissionAcquired,
tracking: state.jobScheduled,
platform: this.getPlatform(),
state
};
}
/**
* Detect if running on iOS or Android
*/
private getPlatform(): 'ios' | 'android' | 'web' {
const userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf('iphone') > -1 || userAgent.indexOf('ipad') > -1) {
return 'ios';
} else if (userAgent.indexOf('android') > -1) {
return 'android';
}
return 'web';
}
}
// Usage in your Capacitor mobile application
const analytics = new MobileAnalyticsService();
// Initialize for iOS and Android
await analytics.initialize('your-partner-id', 'your-partner-key');
// Track users on mobile platforms
await analytics.trackUser('mobile_user_123');
// Check status on iOS or Android device
const status = await analytics.getStatus();
console.log('Analytics status on mobile app:', status);
For accurate tracking on iOS and Android, initialize AppInsights early:
// In your Capacitor app initialization
import { CapacitorAppinsights } from '@capgo/capacitor-appinsights';
async function initMobileApp() {
// Initialize analytics first for mobile platforms
await CapacitorAppinsights.init({
partnerId: process.env.APPINSIGHTS_PARTNER_ID,
partnerKey: process.env.APPINSIGHTS_PARTNER_KEY
});
// Continue with mobile app initialization
console.log('Mobile app ready for iOS and Android');
}
Implement error handling for both iOS and Android platforms:
async function safeAnalyticsInit() {
try {
await CapacitorAppinsights.init({
partnerId: 'your-id',
partnerKey: 'your-key'
});
} catch (error) {
console.error('Analytics failed on mobile device:', error);
// Continue mobile app operation
}
}
Track authenticated users in your mobile application:
// After user logs into your iOS or Android app
async function onUserLogin(user) {
await CapacitorAppinsights.setUserId({
userId: user.id
});
console.log('User identified on mobile platform');
}
While this plugin works with both Capacitor and Cordova mobile frameworks, Capacitor offers advantages:
If you're building a new mobile application, Capacitor is recommended over Cordova for iOS and Android development.
Note: This plugin is designed for native mobile apps on iOS and Android. Web platform support is limited as it's intended for Capacitor mobile applications.
You have successfully integrated the @capgo/capacitor-appinsights plugin into your Capacitor mobile application. This plugin provides comprehensive analytics for both iOS and Android mobile platforms, enabling you to track user behavior and application performance in your native mobile app.
For detailed API documentation and advanced features for mobile app development, visit the GitHub repository.
Whether you're building a new Capacitor mobile app or migrating from Cordova, this analytics solution works seamlessly across iOS and Android platforms, providing the insights you need for successful mobile application development.