forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_session_telemetry.dart
More file actions
48 lines (41 loc) · 1.31 KB
/
Copy pathapp_session_telemetry.dart
File metadata and controls
48 lines (41 loc) · 1.31 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
import 'package:omi/utils/analytics/analytics_manager.dart';
import 'package:uuid/uuid.dart';
enum AppSessionStartKind { coldStart, foreground }
/// Owns the mobile app-session boundary.
///
/// A cold launch starts one session. A later session is started only after the
/// app has first entered the background, so repeated `resumed` callbacks do
/// not inflate the retention denominator.
class AppSessionTelemetry {
AppSessionTelemetry({
AnalyticsManager? analytics,
String Function()? createSessionId,
}) : _analytics = analytics ?? AnalyticsManager(),
_createSessionId = createSessionId ?? const Uuid().v4;
final AnalyticsManager _analytics;
final String Function() _createSessionId;
bool _isBackgrounded = false;
bool _coldStartRecorded = false;
void recordColdStart() {
if (_coldStartRecorded) return;
_coldStartRecorded = true;
_emit(AppSessionStartKind.coldStart);
}
void recordBackgrounded() {
_isBackgrounded = true;
}
void recordResumed() {
if (!_isBackgrounded) return;
_isBackgrounded = false;
_emit(AppSessionStartKind.foreground);
}
void _emit(AppSessionStartKind kind) {
_analytics.track(
'App Session Started',
properties: {
'app_session_id': _createSessionId(),
'start_kind': kind.name,
},
);
}
}