forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposthog_adapter.dart
More file actions
97 lines (82 loc) · 2.61 KB
/
Copy pathposthog_adapter.dart
File metadata and controls
97 lines (82 loc) · 2.61 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
import 'dart:async';
import 'package:posthog_flutter/posthog_flutter.dart';
import 'package:omi/utils/analytics/analytics_adapter.dart';
class PostHogAnalyticsAdapter implements AnalyticsAdapter {
PostHogAnalyticsAdapter({
required this.apiKey,
this.host = 'https://us.i.posthog.com',
// SDK default is `true`; we track lifecycle ourselves at meaningful boundaries.
this.captureLifecycleEvents = false,
this.debug = false,
});
final String apiKey;
final String host;
final bool captureLifecycleEvents;
final bool debug;
bool _initialized = false;
Timer? _targetExpiry;
@override
bool get isInitialized => _initialized;
@override
Future<void> init() async {
if (_initialized) return;
final config = PostHogConfig(apiKey);
config.host = host;
config.captureApplicationLifecycleEvents = captureLifecycleEvents;
config.debug = debug;
await Posthog().setup(config);
_initialized = true;
}
@override
void identify({required String userId, Map<String, Object>? userProperties}) {
if (!_initialized) return;
if (userProperties == null) {
Posthog().identify(userId: userId);
} else {
Posthog().identify(userId: userId, userProperties: userProperties);
}
}
@override
void alias({required String newUserId}) {
if (!_initialized) return;
Posthog().alias(alias: newUserId);
}
@override
void track({required String eventName, Map<String, Object>? properties}) {
if (!_initialized) return;
Posthog().capture(eventName: eventName, properties: properties);
}
@override
void setInteractionContext({String? screenName, required String target}) {
if (!_initialized) return;
// Native iOS rage-click capture runs before Flutter receives the current
// pointer. Registering every pointer's context means a qualifying third
// tap inherits the matching context recorded by the first two taps.
if (screenName != null && screenName.isNotEmpty) {
unawaited(Posthog().register(r'$screen_name', screenName));
unawaited(Posthog().register('screen', screenName));
}
unawaited(Posthog().register('target', target));
_targetExpiry?.cancel();
_targetExpiry = Timer(const Duration(seconds: 2), () {
if (_initialized) unawaited(Posthog().unregister('target'));
});
}
@override
void enable() {
if (!_initialized) return;
Posthog().enable();
}
@override
void disable() {
if (!_initialized) return;
Posthog().disable();
}
@override
void reset() {
if (!_initialized) return;
_targetExpiry?.cancel();
_targetExpiry = null;
Posthog().reset();
}
}