mitr_flutter 0.2.0
mitr_flutter: ^0.2.0 copied to clipboard
Official Flutter SDK for Mitr Analytics — privacy-first, zero-PII event tracking for iOS, Android, desktop, and web.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:mitr_flutter/mitr_flutter.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// dryRun logs events to the console instead of sending them, so this example
// runs with placeholder credentials and no backend.
await MitrAnalytics.instance.initialize(
siteId: 'YOUR_SITE_ID',
secretKey: 'YOUR_SECRET_KEY',
enableLogging: true,
dryRun: true,
);
runApp(const MitrExampleApp());
}
class MitrExampleApp extends StatelessWidget {
const MitrExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Mitr Analytics Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
useMaterial3: true,
),
// Reports a pageview on every route push/pop.
navigatorObservers: [MitrNavigationObserver()],
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _status = 'Initialized in dry-run mode — events are logged, not sent.';
void _report(String message) => setState(() => _status = message);
Future<void> _trackEvent() async {
await MitrAnalytics.instance.track(
'button_clicked',
meta: {'screen': 'home', 'plan': 'pro'},
);
_report('Tracked: button_clicked');
}
Future<void> _trackPageView() async {
// pageView dedupes consecutive calls for the same path, so alternate.
final path = DateTime.now().second.isEven ? '/home' : '/pricing';
await MitrAnalytics.instance.pageView(path);
_report('Tracked pageview: $path');
}
void _identify() {
// The raw id is SHA-256 hashed on-device before it ever leaves the app.
MitrAnalytics.instance.identify('user_42');
_report('Identified as user_42 (hashed on-device)');
}
void _reset() {
MitrAnalytics.instance.reset();
_report('Identity reset — future events send no uid at all');
}
Future<void> _flush() async {
await MitrAnalytics.instance.flush();
_report('Flushed the queue');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Mitr Analytics Example')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(_status, textAlign: TextAlign.center),
const SizedBox(height: 32),
FilledButton(
onPressed: _trackEvent,
child: const Text('track()'),
),
const SizedBox(height: 8),
FilledButton.tonal(
onPressed: _trackPageView,
child: const Text('pageView()'),
),
const SizedBox(height: 8),
OutlinedButton(
onPressed: _identify,
child: const Text('identify()'),
),
const SizedBox(height: 8),
OutlinedButton(
onPressed: _reset,
child: const Text('reset()'),
),
const SizedBox(height: 8),
TextButton(
onPressed: _flush,
child: const Text('flush()'),
),
],
),
),
),
);
}
}