we_segment_flutter 1.0.0
we_segment_flutter: ^1.0.0 copied to clipboard
WebEngage Flutter implementation of Segment Analytics for iOS and Android.
example/lib/main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:we_segment_flutter/flutter_segment.dart';
import 'package:webengage_flutter/webengage_flutter.dart';
void main() async {
/// Wait until the platform channel is properly initialized so we can call
/// `setContext` during the app initialization.
WidgetsFlutterBinding.ensureInitialized();
await Segment.config(
options: SegmentConfig(
writeKey: 'KEY',
trackApplicationLifecycleEvents: true,
webengageIntegrationEnabled: true,
debug: true),
);
/// The `context.device.token` is a special property.
/// When you define it, setting the context again with no token property (ex: `{}`)
/// has no effect on cleaning up the device token.
///
/// This is used as an example to allow you to set string-based
/// device tokens, which is the use case when integrating with
/// Firebase Cloud Messaging (FCM).
///
/// This plugin currently does not support Apple Push Notification service (APNs)
/// tokens, which are binary structures.
///
/// Aside from this special use case, any other context property that needs
/// to be defined (or re-defined) can be done.
Segment.setContext({
'device': {
'token': 'testing',
}
});
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
// TODO: implement initState
super.initState();
var _we = WebEngagePlugin();
_we.setUpInAppCallbacks(
_onInAppClick, _onInAppShown, _onInAppDismiss, _onInAppPrepared);
}
void _onInAppPrepared(Map<String, dynamic>? message) {
print(
"MILIND : This is a inapp Prepare callback from native to flutter. Payload " +
message.toString());
}
void _onInAppClick(Map<String, dynamic>? message, String? s) {
print(
"MILIND : This is a inapp click callback from native to flutter. Payload " +
message.toString());
}
void _onInAppShown(Map<String, dynamic>? message) {
print(
"MILIND : This is a callback on inapp shown from native to flutter. Payload " +
message.toString());
}
void _onInAppDismiss(Map<String, dynamic>? message) {
print(
"MILIND : This is a callback on inapp dismiss from native to flutter. Payload " +
message.toString());
}
@override
Widget build(BuildContext context) {
Segment.screen(
screenName: 'Example Screen',
);
// If you want to flush the data now
Segment.flush();
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Segment example app'),
),
body: Column(
children: <Widget>[
const Spacer(),
Center(
child: ElevatedButton(
child: const Text('LOGIN'),
onPressed: () {
Segment.identify(userId: "User_${DateTime.now()}");
},
),
),
Spacer(),
Center(
child: ElevatedButton(
child: const Text('TRACK ACTION WITH SEGMENT'),
onPressed: () {
Segment.track(
eventName: 'ButtonClicked',
properties: {
'foo': 'bar',
'number': 1337,
'clicked': true,
},
);
},
),
),
const Spacer(),
Center(
child: ElevatedButton(
child: const Text('Update Context'),
onPressed: () {
Segment.setContext({'custom': 123});
},
),
),
const Spacer(),
Center(
child: ElevatedButton(
child: const Text('Clear Context'),
onPressed: () {
Segment.setContext({});
},
),
),
const Spacer(),
Center(
child: ElevatedButton(
child: const Text('Disable'),
onPressed: () async {
await Segment.disable();
Segment.track(eventName: 'This event will not be logged');
},
),
),
const Spacer(),
Center(
child: ElevatedButton(
child: const Text('Enable'),
onPressed: () async {
await Segment.enable();
Segment.track(eventName: 'Enabled tracking events!');
},
),
),
const Spacer(),
if (Platform.isIOS)
Center(
child: ElevatedButton(
child: const Text('Debug mode'),
onPressed: () {
Segment.debug(true);
},
),
)
else
Container(),
const Spacer(),
],
),
),
navigatorObservers: [
SegmentObserver(),
],
);
}
}