amplitude_engagement_flutter 0.0.7
amplitude_engagement_flutter: ^0.0.7 copied to clipboard
Official Amplitude Engagement Flutter SDK, supporting Android, iOS
example/lib/main.dart
import 'dart:async';
import 'package:amplitude_flutter/events/identify.dart';
import 'package:app_links/app_links.dart';
import 'package:flutter/material.dart';
import 'package:amplitude_engagement_flutter/amplitude_engagement.dart'
as engagement;
import 'package:amplitude_flutter/amplitude.dart';
import 'package:amplitude_flutter/configuration.dart';
import 'package:amplitude_flutter/constants.dart';
import 'package:amplitude_flutter/default_tracking.dart';
// Global API key configuration
const String amplitudeApiKey = '6dba5c25868be3716e69f525035e33b6';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
showSemanticsDebugger: false,
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> {
late Amplitude analytics;
late AppLinks _appLinks;
StreamSubscription<Uri>? _linkSubscription;
@override
void initState() {
super.initState();
analytics = Amplitude(
Configuration(
apiKey: amplitudeApiKey,
logLevel: LogLevel.debug,
defaultTracking: DefaultTrackingOptions.all(),
),
);
_initializeSDKs();
_initDeepLinks();
}
@override
void dispose() {
_linkSubscription?.cancel();
super.dispose();
}
Future<void> _initializeSDKs() async {
try {
await engagement.installAmplitudeEngagementPlugin(analytics);
analytics.identify(Identify()..set('test_property', 'test_value'));
engagement.addCallback("test_callback", () {
debugPrint('Callback triggered');
});
} catch (e, stack) {
print('Error initializing SDKs: $e');
print('Stack trace: $stack');
}
}
void _initDeepLinks() {
_appLinks = AppLinks();
// Handle link that launched the app (cold start)
_appLinks.getInitialLink().then((uri) {
if (uri != null) _handleDeepLink(uri);
});
// Handle links received while the app is running (warm start)
_linkSubscription = _appLinks.uriLinkStream.listen(_handleDeepLink);
}
Future<void> _handleDeepLink(Uri uri) async {
debugPrint('Deep link received: $uri');
final handled = await engagement.handleURL(uri.toString());
debugPrint('Deep link handled by engagement SDK: $handled');
}
@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: [
const SizedBox(height: 16),
Wrap(
spacing: 8,
runSpacing: 8,
children: [
ElevatedButton(
onPressed: () => {engagement.screen('HomeScreen')},
child: const Text('Track Screen'),
),
Semantics(
tagForChildren: const engagement.AmplitudeEngagementView(
'track_screen_button2',
),
child: ElevatedButton(
onPressed: () => {engagement.screen('HomeScreen')},
child: const Text('Track Screen'),
),
),
ElevatedButton(
onPressed: () => {engagement.closeAll()},
child: const Text('Close All'),
),
ElevatedButton(
onPressed: () => {
engagement.forwardEvent({
'event_type': 'test_event',
'event_properties': {'test_property': 'test_value'},
}),
},
child: const Text('Forward Event'),
),
],
),
],
),
),
);
}
}