forked from segmentio/analytics_flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
186 lines (178 loc) · 6.79 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import 'package:analytics/event.dart';
import 'package:analytics/state.dart';
import 'package:analytics_example/config.dart';
import 'package:analytics_plugin_advertising_id/plugin_advertising_id.dart';
import 'package:analytics_plugin_idfa/plugin_idfa.dart';
import 'package:analytics_example/firebase_options.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:analytics/client.dart';
import 'package:analytics_plugin_firebase/plugin_firebase.dart'
show FirebaseDestination;
void main() {
runApp(const MyApp());
}
class MyApp extends MaterialApp {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final analytics = createClient(Configuration(writeKey,
debug: true, trackApplicationLifecycleEvents: false));
@override
void initState() {
super.initState();
initPlatformState();
analytics
.addPlugin(FirebaseDestination(DefaultFirebaseOptions.currentPlatform));
analytics.addPlugin(PluginAdvertisingId());
analytics.addPlugin(PluginIdfa());
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
}
@override
Widget build(BuildContext context) {
return MaterialApp(navigatorObservers: [
ScreenObserver()
], routes: {
'/': (context) => Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child:
Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
TextButton(
onPressed: () {
Navigator.pushNamed(context, "/next");
},
child: const Text('Next Screen'),
),
TextButton(
onPressed: () {
analytics
.track("Test Event", properties: {"prop1": "value1"});
},
child: const Text('Track Product Viewed'),
),
TextButton(
onPressed: () {
analytics.track('Products Searched',
properties: {"query": 'blue roses'});
analytics.track('Product List Viewed', properties: {
"list_id": 'hot_deals_1',
"category": 'Deals',
"products": [
{
"product_id": '507f1f77bcf86cd799439011',
"sku": '45790-32',
"name": 'Monopoly: 3rd Edition',
"price": 19,
"position": 1,
"category": 'Games',
"url": 'https://www.example.com/product/path',
"image_url": 'https://www.example.com/product/path.jpg'
},
{
"product_id": '505bd76785ebb509fc183733',
"sku": '46493-32',
"name": 'Uno Card Game',
"price": 3,
"position": 2,
"category": 'Games'
}
]
});
analytics.track('Promotion Viewed', properties: {
"promotion_id": 'promo_1',
"creative": 'top_banner_2',
"name": '75% store-wide shoe sale',
"position": 'home_banner_top'
});
analytics.track('Product Clicked', properties: {
"product_id": '507f1f77bcf86cd799439011',
"sku": 'G-32',
"category": 'Games',
"name": 'Monopoly: 3rd Edition',
"brand": 'Hasbro',
"variant": '200 pieces',
"price": 18.99,
"quantity": 1,
"coupon": 'MAYDEALS',
"position": 3,
"url": 'https://www.example.com/product/path',
"image_url": 'https://www.example.com/product/path.jpg'
});
analytics.track('Product Shared', properties: {
"share_via": 'email',
"share_message": 'Hey, check out this item',
"recipient": '[email protected]',
"product_id": '507f1f77bcf86cd799439011',
"sku": 'G-32',
"category": 'Games',
"name": 'Monopoly: 3rd Edition',
"brand": 'Hasbro',
"variant": '200 pieces',
"price": 18.99,
"url": 'https://www.example.com/product/path',
"image_url": 'https://www.example.com/product/path.jpg'
});
analytics.track('Cart Shared', properties: {
"share_via": 'email',
"share_message": 'Hey, check out this item',
"recipient": '[email protected]',
"cart_id": 'd92jd29jd92jd29j92d92jd',
"products": [
{"product_id": '507f1f77bcf86cd799439011'},
{"product_id": '505bd76785ebb509fc183733'}
]
});
},
child: const Text('Track eCommerce events'),
),
TextButton(
onPressed: () {
analytics.reset();
},
child: const Text('Reset'),
),
TextButton(
onPressed: () {
analytics.flush();
},
child: const Text('Flush'),
),
])),
),
'/next': (context) => Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child:
Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Next Screen'),
),
TextButton(
onPressed: () {
analytics.identify(
userId: "testUserId",
userTraits: UserTraits(name: "Test User"));
},
child: const Text('Identify Event'),
),
])),
)
});
}
}