amplitude_engagement_flutter 0.0.1
amplitude_engagement_flutter: ^0.0.1 copied to clipboard
Official Amplitude Engagement Flutter SDK, supporting Android, iOS
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:amplitude_engagement_flutter/amplitude_engagement_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Amplitude Engagement Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(title: 'Amplitude Engagement Demo'),
);
}
}
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 AmplitudeEngagementFlutter _amplitudeEngagement =
AmplitudeEngagementFlutter();
int? _instanceId;
List<GuideOrSurvey> _guides = [];
String _status = 'Not initialized';
@override
void initState() {
super.initState();
_setupAmplitudeEngagement();
_listenToEvents();
}
void _setupAmplitudeEngagement() async {
try {
// Initialize with your API key
final instanceId = await _amplitudeEngagement.newInstance(
'20595ac065a64117dca3194d85af7b3f',
);
// Boot the instance
await _amplitudeEngagement.boot(
instanceId,
userId: 'user123',
deviceId: 'device456',
);
// Set theme mode
await _amplitudeEngagement.setThemeMode(
instanceId,
AmplitudeThemeMode.light,
);
setState(() {
_instanceId = instanceId;
_status = 'Initialized successfully';
});
// Load guides and surveys
_loadGuides();
} catch (e) {
setState(() {
_status = 'Error: $e';
});
}
}
void _listenToEvents() {
// Listen to tracking events
_amplitudeEngagement.onTrackEvent.listen((event) {
debugPrint(
'Track Event: ${event.eventType}, Properties: ${event.eventProperties}',
);
});
// Listen to callback invocations
_amplitudeEngagement.onInvokeCallback.listen((callback) {
debugPrint('Callback Invoked: ID=${callback.id}, Key=${callback.key}');
});
}
void _loadGuides() async {
if (_instanceId == null) return;
try {
final guides = await _amplitudeEngagement.list(_instanceId!);
setState(() {
_guides = guides;
});
} catch (e) {
debugPrint('Error loading guides: $e');
}
}
void _showGuide(String key) async {
if (_instanceId == null) return;
try {
await _amplitudeEngagement.show(_instanceId!, key, 0);
} catch (e) {
debugPrint('Error showing guide: $e');
}
}
void _trackScreen(String screenName) async {
if (_instanceId == null) return;
try {
await _amplitudeEngagement.screen(_instanceId!, screenName);
} catch (e) {
debugPrint('Error tracking screen: $e');
}
}
void _addCallback(String key) async {
if (_instanceId == null) return;
try {
await _amplitudeEngagement.addCallback(_instanceId!, key);
} catch (e) {
debugPrint('Error adding callback: $e');
}
}
void _closeAll() async {
if (_instanceId == null) return;
try {
await _amplitudeEngagement.closeAll(_instanceId!);
} catch (e) {
debugPrint('Error closing all: $e');
}
}
void _handleURL(String url) async {
if (_instanceId == null) return;
try {
final success = await _amplitudeEngagement.handleURL(_instanceId!, url);
debugPrint('Handle URL success: $success');
} catch (e) {
debugPrint('Error handling URL: $e');
}
}
void _forwardEvent() async {
if (_instanceId == null) return;
try {
final event = {
'event_type': 'custom_event',
'event_id': 12345,
'platform': 'Flutter',
'event_properties': {
'custom_property': 'custom_value',
'timestamp': DateTime.now().toIso8601String(),
},
};
await _amplitudeEngagement.forwardEvent(_instanceId!, event);
} catch (e) {
debugPrint('Error forwarding event: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Status:',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
Text(_status),
],
),
),
),
const SizedBox(height: 16),
if (_instanceId != null) ...[
Text(
'Available Guides & Surveys:',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
if (_guides.isEmpty)
const Text('No guides available')
else
..._guides.map(
(guide) => Card(
child: ListTile(
title: Text(guide.title),
subtitle: Text(
'Status: ${guide.status}, Step: ${guide.step}',
),
trailing: ElevatedButton(
onPressed: () => _showGuide(guide.id.toString()),
child: const Text('Show'),
),
),
),
),
const SizedBox(height: 16),
Wrap(
spacing: 8,
runSpacing: 8,
children: [
ElevatedButton(
onPressed: _loadGuides,
child: const Text('Refresh Guides'),
),
ElevatedButton(
onPressed: () => _trackScreen('home_screen'),
child: const Text('Track Screen'),
),
ElevatedButton(
onPressed: () => _addCallback('callback_key'),
child: const Text('Add Callback'),
),
ElevatedButton(
onPressed: _closeAll,
child: const Text('Close All'),
),
ElevatedButton(
onPressed: () => _handleURL('https://example.com'),
child: const Text('Handle URL'),
),
ElevatedButton(
onPressed: _forwardEvent,
child: const Text('Forward Event'),
),
],
),
],
],
),
),
);
}
}