Skip to content

Commit b81bec6

Browse files
unix14eyalYaakobi
authored andcommitted
migrate to android SDK 34
Add/remove reporters logic implemented new screen refactored code into seperate files
1 parent f5afd0b commit b81bec6

File tree

9 files changed

+353
-76
lines changed

9 files changed

+353
-76
lines changed

analitix.iml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
7+
<excludeFolder url="file://$MODULE_DIR$/.pub" />
8+
<excludeFolder url="file://$MODULE_DIR$/build" />
9+
</content>
10+
<orderEntry type="sourceFolder" forTests="false" />
11+
<orderEntry type="library" name="Dart SDK" level="project" />
12+
<orderEntry type="library" name="Dart Packages" level="project" />
13+
<orderEntry type="library" name="Flutter Plugins" level="project" />
14+
</component>
15+
</module>

lib/analitix/abstract/analytix_manager.dart

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ class AnalytixManager {
1414
bool _isCollectionEnabled = true;
1515
String? _screenName;
1616
String? _userId;
17-
Map<String, dynamic> _userProperties = {}; /// todo add device properties collection
18-
List<AnalytixEvent> _allEvents = [];
17+
Map<String, dynamic> _userProperties = {};
18+
Map<String, dynamic> _deviceProperties = {};
19+
final List<AnalytixEvent> _allEvents = [];
1920

2021
factory AnalytixManager() {
2122
return _instance;
@@ -33,23 +34,58 @@ class AnalytixManager {
3334
print('AnalytixManager init');
3435
}
3536

36-
void logEvent(String name, String subEventName, Map<String, dynamic> params) {
37+
void logScreenView(String screenName) {
38+
print('AnalytixManager setScreenName: $screenName');
39+
_screenName = screenName;
40+
logEvent('screen_view', screenName);
41+
}
42+
43+
void logEvent(String name, String subEventName, {Map<String, dynamic> params = const {} }) {
3744
if(!_isCollectionEnabled) {
3845
print('AnalytixManager logEvent: Collection is disabled');
3946
return;
4047
}
4148
print('AnalytixManager logEvent: $name, $params');
49+
Map<String, dynamic> newParams = {};
50+
newParams.addAll(params);
4251

4352
for(BaseReporter reporter in reporters) {
44-
params['screenName'] = _screenName;
45-
params['userId'] = _userId;
46-
params['userProperties'] = _userProperties;
47-
var newEvent = AnalytixEvent(name, subEventName: subEventName, parameters: params);
53+
newParams['screenName'] = _screenName;
54+
newParams['userId'] = _userId;
55+
newParams['userProperties'] = _userProperties;
56+
newParams['deviceProperties'] = _deviceProperties;
57+
newParams['eventTime'] = DateTime.now();
58+
var newEvent = AnalytixEvent(name, subEventName: subEventName, parameters: newParams);
4859
reporter.logEvent(newEvent);
4960
_allEvents.add(newEvent);
5061
}
5162
}
5263

64+
void addReporters(List<BaseReporter> reporters) {
65+
reporters.addAll(reporters);
66+
}
67+
68+
void addReporter(BaseReporter reporter) {
69+
reporters.add(reporter);
70+
}
71+
72+
void removeReporters(List<BaseReporter> reporters) {
73+
reporters.removeWhere((element) => reporters.contains(element));
74+
}
75+
76+
void removeReporter(BaseReporter reporter) {
77+
reporters.remove(reporter);
78+
}
79+
80+
void clearReporters() {
81+
reporters.clear();
82+
}
83+
84+
void setDeviceProperties(Map<String, dynamic> properties) {
85+
print('AnalytixManager setDeviceProperties: $properties');
86+
_userProperties.addAll(properties);
87+
}
88+
5389
void setUserProperty(String name, dynamic value) {
5490
print('AnalytixManager setUserProperty: $name, $value');
5591
_userProperties[name] = value;
@@ -60,11 +96,6 @@ class AnalytixManager {
6096
_userId = userId;
6197
}
6298

63-
void setScreenName(String name) {
64-
print('AnalytixManager setScreenName: $name');
65-
_screenName = name;
66-
}
67-
6899
void setAnalyticsCollectionEnabled(bool enabled) {
69100
print('AnalytixManager setAnalyticsCollectionEnabled: $enabled');
70101
_isCollectionEnabled = enabled;
@@ -75,6 +106,7 @@ class AnalytixManager {
75106
_userId = null;
76107
_screenName = null;
77108
_userProperties = {};
109+
_deviceProperties = {};
78110
_allEvents.clear();
79111
}
80112
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
import 'dart:io';
3+
4+
import 'package:flutter/foundation.dart';
5+
6+
extension DeviceExtension on dynamic {
7+
/// This function returns the device's name
8+
String get deviceName {
9+
return Platform.localHostname; // Returns the device's hostname
10+
}
11+
12+
/// This function returns the device's platform
13+
String get devicePlatform {
14+
return Platform.isAndroid ? 'Android' : 'iOS';
15+
}
16+
17+
/// This function returns the device's version
18+
String get deviceVersion {
19+
return Platform.version; // Note: This may not be available in all contexts
20+
}
21+
22+
/// This function returns the device's identifier
23+
String get deviceIdentifier {
24+
return Platform.localHostname; // Not a unique identifier; used as a placeholder
25+
}
26+
27+
/// This function returns the device's model
28+
String get deviceModel {
29+
return Platform.version;
30+
}
31+
32+
/// This function returns the device's manufacturer
33+
String get deviceManufacturer {
34+
return Platform.isAndroid ? 'Unknown Manufacturer' : 'Apple';
35+
}
36+
37+
/// This function returns the device's isPhysicalDevice
38+
bool get isDebugMode {
39+
return kDebugMode;
40+
}
41+
42+
Map<String, dynamic> get deviceProperties {
43+
return {
44+
'deviceName': deviceName,
45+
'devicePlatform': devicePlatform,
46+
'deviceVersion': deviceVersion,
47+
'deviceIdentifier': deviceIdentifier,
48+
'deviceModel': deviceModel,
49+
'deviceManufacturer': deviceManufacturer,
50+
'isDebugMode': isDebugMode,
51+
};
52+
}
53+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
import 'package:analitix/analitix/models/analytix_event.dart';
3+
import '../analitix/abstract/base_reporter.dart';
4+
5+
/// ScreenLoggerReporter class
6+
/// This class is an example of a custom reporter that extends the BaseReporter class
7+
/// its responsibility is to log events to the screen
8+
class ScreenLoggerReporter extends BaseReporter {
9+
10+
ScreenLoggerReporter._internal();
11+
static final ScreenLoggerReporter _instance = ScreenLoggerReporter._internal();
12+
factory ScreenLoggerReporter() => _instance;
13+
14+
List<String> allReports = [];
15+
16+
@override
17+
void init() {
18+
print('ScreenLoggerReporter: init');
19+
}
20+
21+
@override
22+
void logEvent(AnalytixEvent event) {
23+
print('ScreenLoggerReporter: logEvent: $event');
24+
allReports.add(event.toString());
25+
}
26+
}

lib/main.dart

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,29 @@
11
import 'package:analitix/analitix/abstract/analytix_manager.dart';
2-
import 'package:analitix/analitix/models/analytix_event.dart';
32
import 'package:flutter/material.dart';
3+
import 'custom_reporters/screen_logger_reporter.dart';
4+
import 'ui/analytix_example_screen.dart';
45

5-
void main() => runApp(const MyApp());
6-
7-
class MyApp extends StatelessWidget {
8-
const MyApp({super.key});
9-
10-
// This widget is the root of your application.
11-
@override
12-
Widget build(BuildContext context) {
13-
return MaterialApp(
14-
title: 'Analytix Demo',
15-
theme: ThemeData(
16-
primarySwatch: Colors.blue,
17-
),
18-
home: MyHomePage(),
19-
);
20-
}
21-
}
22-
23-
class MyHomePage extends StatefulWidget {
24-
@override
25-
State<MyHomePage> createState() => _MyHomePageState();
6+
void main() {
7+
_initAnalytics();
8+
runApp(const AnalytixExampleScreen());
269
}
2710

28-
class _MyHomePageState extends State<MyHomePage> {
29-
int _counter = 0;
30-
31-
void _incrementCounter() {
32-
setState(() {
33-
_counter++;
34-
});
35-
AnalytixManager().logEvent('main.dart Page', 'button_press', {'counter': _counter});
36-
}
37-
38-
// todo build the tester app in a new screen, and add the analytics code to the button
39-
// todo also add a new screen button which reports the screen view
40-
// todo also add user properties to the analytics
41-
42-
@override
43-
Widget build(BuildContext context) {
44-
return Scaffold(
45-
appBar: AppBar(
46-
title: const Text("Analytix Demo"),
47-
),
48-
body: Center(
49-
child: Column(
50-
mainAxisAlignment: MainAxisAlignment.center,
51-
children: <Widget>[
52-
const Text(
53-
'You have reported the button press to the Analytix Manager this many times:',
54-
),
55-
Text(
56-
'$_counter',
57-
style: Theme.of(context).textTheme.headlineMedium,
58-
),
59-
],
60-
),
61-
),
62-
floatingActionButton: FloatingActionButton(
63-
onPressed: _incrementCounter,
64-
tooltip: 'Increment',
65-
child: const Icon(Icons.add),
66-
), // This trailing comma makes auto-formatting nicer for build methods.
67-
);
68-
}
11+
_initAnalytics() {
12+
// Init the AnalytixManager
13+
AnalytixManager().init();
14+
15+
// Add the ScreenLoggerReporter
16+
AnalytixManager().addReporter(ScreenLoggerReporter());
17+
18+
// Set the user properties
19+
AnalytixManager().setUserId("1234567890");
20+
AnalytixManager().setUserProperty("name", "Eyal");
21+
AnalytixManager().setUserProperty("last_name", "Yaakobi");
22+
AnalytixManager().setUserProperty("email", "[email protected]");
23+
AnalytixManager().setUserProperty("website", "https://www.3p-cups.com");
24+
AnalytixManager().setUserProperty("isMale", true);
25+
AnalytixManager().setUserProperty("isAdmin", false);
26+
AnalytixManager().setUserProperty("amountOfConnectedSessions", 3);
27+
AnalytixManager().setUserProperty("lastLoginTime", DateTime.now());
28+
AnalytixManager().setUserProperty("refreshToken", "FNvof-Qq3llcnu8nvknerk87@#aaAxz-zZq3");
6929
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import 'package:analitix/ui/simple_event_example_screen.dart';
3+
import 'package:flutter/material.dart';
4+
5+
class AnalytixExampleScreen extends StatelessWidget {
6+
const AnalytixExampleScreen({super.key});
7+
8+
// This widget is the root of your application.
9+
@override
10+
Widget build(BuildContext context) {
11+
return MaterialApp(
12+
title: 'Analytix Demo',
13+
theme: ThemeData(
14+
primarySwatch: Colors.blue,
15+
),
16+
home: SimpleEventExampleScreen(),
17+
);
18+
}
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
import 'package:analitix/analitix/abstract/analytix_manager.dart';
3+
import 'package:flutter/material.dart';
4+
5+
class ScreenViewExampleScreen extends StatefulWidget {
6+
const ScreenViewExampleScreen({super.key});
7+
8+
@override
9+
State<ScreenViewExampleScreen> createState() => _ScreenViewExampleScreenState();
10+
}
11+
12+
class _ScreenViewExampleScreenState extends State<ScreenViewExampleScreen> {
13+
14+
@override
15+
void initState() {
16+
AnalytixManager().logScreenView("screen_view_example_screen.dart");
17+
super.initState();
18+
}
19+
20+
@override
21+
Widget build(BuildContext context) {
22+
return Scaffold(
23+
appBar: AppBar(
24+
title: const Text("Screen View Example"),
25+
),
26+
body: Center(
27+
child: Text(
28+
'This is the screen view example\n\nlog screen view event was sent to the reporter',
29+
style: Theme.of(context).textTheme.bodyLarge,
30+
textAlign: TextAlign.center,
31+
),
32+
),
33+
);
34+
}
35+
}

0 commit comments

Comments
 (0)