flutter_analytics_mock 0.0.1
flutter_analytics_mock: ^0.0.1 copied to clipboard
Mock analytics for testing without sending real data
import 'package:flutter/material.dart';
import 'package:flutter_analytics_mock/flutter_analytics_mock.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Analytics Mock Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Analytics Mock Example'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final MockAnalytics _analytics = MockAnalytics();
String _lastAction = 'No actions yet';
void _trackButtonClick(String buttonName) {
_analytics.trackEvent('button_clicked', parameters: {
'button_name': buttonName,
'timestamp': DateTime.now().toIso8601String(),
});
setState(() {
_lastAction = 'Tracked button click: $buttonName';
});
}
void _setUserProperty(String propertyName, String value) {
_analytics.setUserProperty(propertyName, value);
setState(() {
_lastAction = 'Set user property: $propertyName = $value';
});
}
void _trackScreenView(String screenName) {
_analytics.trackScreenView(screenName);
setState(() {
_lastAction = 'Tracked screen view: $screenName';
});
}
void _setUserId(String userId) {
_analytics.setUserId(userId);
setState(() {
_lastAction = 'Set user ID: $userId';
});
}
void _clearAnalytics() {
_analytics.clear();
setState(() {
_lastAction = 'Cleared all analytics data';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Last Action: $_lastAction',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 20),
Text(
'Analytics Statistics:',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 10),
Text('Total Events: ${_analytics.totalEventCount}'),
Text('Total User Properties: ${_analytics.totalUserPropertyCount}'),
Text('Total Screen Views: ${_analytics.totalScreenViewCount}'),
Text('Total User IDs: ${_analytics.totalUserIdCount}'),
const SizedBox(height: 20),
Text(
'Actions:',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 10),
Wrap(
spacing: 8.0,
runSpacing: 8.0,
children: [
ElevatedButton(
onPressed: () => _trackButtonClick('Primary'),
child: const Text('Track Primary Button'),
),
ElevatedButton(
onPressed: () => _trackButtonClick('Secondary'),
child: const Text('Track Secondary Button'),
),
ElevatedButton(
onPressed: () => _setUserProperty('user_type', 'premium'),
child: const Text('Set User Type'),
),
ElevatedButton(
onPressed: () => _setUserProperty('subscription', 'monthly'),
child: const Text('Set Subscription'),
),
ElevatedButton(
onPressed: () => _trackScreenView('home_screen'),
child: const Text('Track Home Screen'),
),
ElevatedButton(
onPressed: () => _trackScreenView('profile_screen'),
child: const Text('Track Profile Screen'),
),
ElevatedButton(
onPressed: () => _setUserId('user_123'),
child: const Text('Set User ID'),
),
ElevatedButton(
onPressed: _clearAnalytics,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
child: const Text('Clear Analytics'),
),
],
),
const SizedBox(height: 20),
if (_analytics.events.isNotEmpty) ...[
Text(
'Recent Events:',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 10),
Expanded(
child: ListView.builder(
itemCount: _analytics.events.length,
itemBuilder: (context, index) {
final event = _analytics.events[index];
return Card(
child: ListTile(
title: Text(event.name),
subtitle: Text(
event.parameters?.toString() ?? 'No parameters'),
trailing: Text(
'${event.timestamp.hour}:${event.timestamp.minute.toString().padLeft(2, '0')}',
),
),
);
},
),
),
],
],
),
),
);
}
}