Skip to main content

Flutter SDK

A lightweight Flutter SDK with full cross-platform support.

Requirements

  • Flutter 3.10+
  • Dart 3.0+

Platform Support

PlatformSupported
iOSYes
AndroidYes
WebYes
macOSYes
WindowsYes
LinuxYes

Installation

Add to your pubspec.yaml:

dependencies:
mostly_good_metrics_flutter: ^0.1.0

Then run:

flutter pub get

Quick Start

Initialize

Initialize once at app startup (typically in main.dart):

import 'package:flutter/material.dart';
import 'package:mostly_good_metrics_flutter/mostly_good_metrics_flutter.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

await MostlyGoodMetrics.configure(
MGMConfiguration(apiKey: 'mgm_proj_your_api_key'),
);

runApp(MyApp());
}

Track Events

// Simple event
MostlyGoodMetrics.track('button_clicked');

// Event with properties
MostlyGoodMetrics.track('purchase_completed', properties: {
'product_id': 'SKU123',
'price': 29.99,
'currency': 'USD',
});

Identify Users

// Set user identity
await MostlyGoodMetrics.identify('user_123');

// Reset identity (e.g., on logout)
await MostlyGoodMetrics.resetIdentity();

Configuration Options

await MostlyGoodMetrics.configure(
MGMConfiguration(
apiKey: 'mgm_proj_your_api_key',
baseUrl: 'https://mostlygoodmetrics.com',
environment: 'production',
appVersion: '1.0.0',
maxBatchSize: 100,
flushInterval: 30,
maxStoredEvents: 10000,
enableDebugLogging: kDebugMode,
trackAppLifecycleEvents: true,
),
);
OptionDefaultDescription
apiKeyRequiredYour API key
baseUrlhttps://mostlygoodmetrics.comAPI endpoint
environment"production"Environment name
appVersion-App version (required for install/update tracking)
maxBatchSize100Events per batch
flushInterval30Auto-flush interval in seconds
maxStoredEvents10000Max cached events
enableDebugLoggingfalseEnable debug output
trackAppLifecycleEventstrueAuto-track lifecycle events

Automatic Events

EventWhenProperties
$app_installedFirst launch after install-
$app_updatedFirst launch after version changeprevious_version, current_version
$app_openedApp started-
$app_foregroundedApp became active-
$app_backgroundedApp went to background-

Session Management

// Start a new session manually
await MostlyGoodMetrics.startNewSession();

// Access current session ID
final sessionId = MostlyGoodMetrics.sessionId;

Error Handling

try {
MostlyGoodMetrics.track('invalid-event-name');
} on MGMError catch (e) {
print('Error type: ${e.type}');
print('Message: ${e.message}');
}

Error types:

  • MGMErrorType.notConfigured - SDK not configured
  • MGMErrorType.invalidEventName - Invalid event name
  • MGMErrorType.invalidProperties - Invalid properties
  • MGMErrorType.networkError - Network failure
  • MGMErrorType.rateLimited - API rate limited

Manual Flush

await MostlyGoodMetrics.flush();

// Check pending events
final count = await MostlyGoodMetrics.getPendingEventCount();
print('$count events pending');

// Clear pending events
await MostlyGoodMetrics.clearPendingEvents();