Getting Started
Dieser Inhalt ist in Ihrer Sprache noch nicht verfügbar.
-
Install the package
Terminal-Fenster npm i @capgo/capacitor-appinsightsTerminal-Fenster pnpm add @capgo/capacitor-appinsightsTerminal-Fenster yarn add @capgo/capacitor-appinsightsTerminal-Fenster bun add @capgo/capacitor-appinsights -
Sync with native projects
Terminal-Fenster npx cap syncTerminal-Fenster pnpm cap syncTerminal-Fenster yarn cap syncTerminal-Fenster bunx cap sync
Usage
Import the plugin and initialize it with your AppInsights credentials:
import { CapacitorAppinsights } from '@capgo/capacitor-appinsights';
// Initialize the SDKconst initializeAnalytics = async () => { await CapacitorAppinsights.init({ partnerId: 'your-partner-id', partnerKey: 'your-partner-key' });};
// Set user ID for trackingconst setUser = async (userId: string) => { await CapacitorAppinsights.setUserId({ userId });};
// Get SDK stateconst checkSDKState = async () => { const state = await CapacitorAppinsights.getState(); console.log('SDK State:', state);};
// Get plugin versionconst checkVersion = async () => { const { version } = await CapacitorAppinsights.getPluginVersion(); console.log('Plugin version:', version);};API Reference
init(options)
Initialize the AppInsights SDK with your credentials.
await CapacitorAppinsights.init({ partnerId: 'your-partner-id', partnerKey: 'your-partner-key'});Parameters:
partnerId(string): Your AppInsights partner IDpartnerKey(string): Your AppInsights partner key
setUserId(options)
Set or update the user ID after initialization.
await CapacitorAppinsights.setUserId({ userId: 'user-123'});Parameters:
userId(string): The user ID to set for tracking
getState()
Get the current state of the SDK.
const state = await CapacitorAppinsights.getState();console.log(state);// {// initCompleted: true,// jobScheduled: true,// permissionAcquired: true// }Returns:
initCompleted(boolean): Whether SDK initialization is completejobScheduled(boolean): Whether data collection job is scheduledpermissionAcquired(boolean): Whether necessary permissions are acquired
getPluginVersion()
Get the native Capacitor plugin version.
const { version } = await CapacitorAppinsights.getPluginVersion();console.log('Version:', version);Complete Example
import { CapacitorAppinsights } from '@capgo/capacitor-appinsights';
export class AnalyticsService { private initialized = false;
async initialize(partnerId: string, partnerKey: string) { try { await CapacitorAppinsights.init({ partnerId, partnerKey });
const state = await CapacitorAppinsights.getState(); this.initialized = state.initCompleted;
console.log('AppInsights initialized:', state); } catch (error) { console.error('Failed to initialize AppInsights:', error); } }
async identifyUser(userId: string) { if (!this.initialized) { throw new Error('AppInsights not initialized'); }
await CapacitorAppinsights.setUserId({ userId }); console.log('User identified:', userId); }
async getSDKStatus() { const state = await CapacitorAppinsights.getState(); return { ready: state.initCompleted && state.permissionAcquired, tracking: state.jobScheduled, state }; }}Best Practices
-
Initialize early Initialize the SDK as soon as your app starts to ensure accurate tracking:
// In your app initializationawait CapacitorAppinsights.init({partnerId: process.env.APPINSIGHTS_PARTNER_ID,partnerKey: process.env.APPINSIGHTS_PARTNER_KEY}); -
Set user ID on login Identify users after authentication:
async function onUserLogin(user) {await CapacitorAppinsights.setUserId({userId: user.id});} -
Check SDK state Verify SDK is ready before critical operations:
const state = await CapacitorAppinsights.getState();if (!state.initCompleted) {console.warn('AppInsights not ready');} -
Handle errors gracefully
try {await CapacitorAppinsights.init(config);} catch (error) {console.error('Analytics initialization failed:', error);// Continue app operation even if analytics fails}
Platform Notes
iOS
- Requires iOS 10.0+
- Ensure you have the necessary permissions in Info.plist for tracking
Android
- Requires Android 5.0 (API 21)+
- May require usage stats permission depending on tracking features
Web
- Not supported on web platform