-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmain.dart
More file actions
345 lines (309 loc) · 8.68 KB
/
Copy pathmain.dart
File metadata and controls
345 lines (309 loc) · 8.68 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:matomo_tracker/matomo_tracker.dart';
import 'package:url_launcher/url_launcher.dart';
// See the docker folder for instructions on how to get a
// test Matomo instance running
const _matomoEndpoint = 'http://localhost:8765/matomo.php';
const _sideId = "1";
const _testUserId = 'Nelson Pandela';
// Use this as dispatchSettings in MatomoTracker.instance.initialize()
// to test persistent actions. Then run the example, cause some actions
// by clicking around, close the example within 5min to prevent the
// dispatchment of the actions, run the example again, wait at least 5min,
// finally check the Matomo dashboard to see if all actions are there.
const DispatchSettings dispatchSettingsEndToEndTest =
DispatchSettings.persistent(dequeueInterval: Duration(minutes: 5));
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await MatomoTracker.instance.initialize(
siteId: _sideId,
url: _matomoEndpoint,
verbosityLevel: Level.all,
// dispatchSettings: dispatchSettingsEndToEndTest,
);
MatomoTracker.instance.setVisitorUserId(_testUserId);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Matomo Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Matomo Example'),
navigatorObservers: [
matomoLocalObserver,
MatomoGlobalObserver(),
],
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
MyHomePageState createState() => MyHomePageState();
}
/// Sends a page view to Matomo on widget creation by implementing TraceableClientMixin
class MyHomePageState extends State<MyHomePage> with TraceableClientMixin {
int _counter = 0;
void _incrementCounter() {
// Send an event to Matomo on tap.
MatomoTracker.instance.trackEvent(
eventInfo: EventInfo(
category: 'Main',
action: 'Click',
name: 'IncrementCounter',
),
);
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const ContentWidget(),
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 20.0),
child: ElevatedButton(
onPressed: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const OtherPage(),
),
);
},
child: const Text('Go to OtherPage'),
),
),
const Outlink(),
const SearchWidget(),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
@override
String get actionName => widget.title;
@override
String? get path => '/homepage';
}
class OtherPage extends StatefulWidget {
const OtherPage({super.key});
@override
State<OtherPage> createState() => _OtherPageState();
}
class _OtherPageState extends State<OtherPage> {
late bool _loading;
late Duration? _workTime;
@override
void initState() {
super.initState();
_loading = true;
// simulate work
final workStart = DateTime.now();
Future.delayed(Duration(seconds: Random().nextInt(4) + 3))
.then((_) => setState(() {
_loading = false;
_workTime = DateTime.now().difference(workStart);
}));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Other Page'),
),
body: Center(
child: _loading
? const CircularProgressIndicator()
: TraceableWidget(
path: '/otherpage',
actionName: 'Other Page',
performanceInfo: PerformanceInfo(
serverTime: _workTime,
),
child: const Text(
'Welcome to the other page!',
),
),
),
);
}
}
/// Example for outlink tracking
class Outlink extends StatelessWidget {
static const _outlink = 'https://github.com/Floating-Dartists/matomo-tracker';
const Outlink({super.key});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 15.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('Outlink tracking test:'),
ElevatedButton(
onPressed: _onPressed,
child: const Text('View on GitHub'),
),
],
),
);
}
void _onPressed() {
MatomoTracker.instance.trackOutlink(
link: _outlink,
);
launchUrl(Uri.parse(_outlink));
}
}
class SearchWidget extends StatefulWidget {
const SearchWidget({super.key});
@override
State<SearchWidget> createState() => _SearchWidgetState();
}
class _SearchWidgetState extends State<SearchWidget> {
final _searchController = TextEditingController(text: 'Enter Search Text...');
bool _canSearch = true;
String? _lastSearch;
@override
void initState() {
super.initState();
_searchController.addListener(_textChange);
}
@override
void dispose() {
super.dispose();
_searchController.removeListener(_textChange);
}
void _textChange() {
final canSearch = _searchController.text.trim().isNotEmpty;
if (_canSearch != canSearch) {
setState(() {
_canSearch = canSearch;
});
}
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 15.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
width: 300.0,
child: TextField(controller: _searchController),
),
IconButton(
onPressed: _canSearch ? _search : null,
icon: const Icon(Icons.search),
),
],
),
_lastSearch == null
? const Text('No search yet!')
: Text('Last search: $_lastSearch'),
],
),
);
}
void _search() {
MatomoTracker.instance.trackSearch(
searchKeyword: _searchController.text,
);
setState(() {
_lastSearch = _searchController.text;
});
}
}
class ContentWidget extends StatefulWidget {
const ContentWidget({
super.key,
});
@override
State<ContentWidget> createState() => _ContentWidgetState();
}
class _ContentWidgetState extends State<ContentWidget> {
late bool _closed;
final Content _exampleContent = Content(
name: 'Matomo is great',
piece: 'banner',
);
@override
void initState() {
super.initState();
_closed = false;
MatomoTracker.instance.trackContentImpression(
content: _exampleContent,
);
}
@override
Widget build(BuildContext context) {
return Visibility(
visible: !_closed,
child: Card(
color: Colors.blue,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
IconButton(
onPressed: _close,
icon: const Icon(
Icons.close,
),
),
const SizedBox(
width: 200,
height: 150,
child: Center(
child: Text(
'Matomo is great!',
),
),
)
],
)));
}
void _close() {
setState(() {
_closed = true;
});
MatomoTracker.instance.trackContentInteraction(
interaction: 'close',
content: _exampleContent,
);
}
}