-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathanalytics_controller.dart
More file actions
109 lines (91 loc) · 3.29 KB
/
analytics_controller.dart
File metadata and controls
109 lines (91 loc) · 3.29 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
// Copyright 2021 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
import 'dart:async';
import 'package:flutter/foundation.dart';
import '../development_helpers.dart';
import '../globals.dart';
import '../managers/dtd_manager_extensions.dart';
Future<AnalyticsController> get analyticsController async {
if (_analyticsController != null) return _analyticsController!;
var enabled = false;
var shouldShowConsentMessage = false;
try {
enabled =
debugSendAnalytics ||
(kReleaseMode && await dtdManager.analyticsTelemetryEnabled());
shouldShowConsentMessage =
debugShowAnalyticsConsentMessage ||
(kReleaseMode && await dtdManager.shouldShowAnalyticsConsentMessage());
} catch (_) {
// Ignore issues if analytics could not be initialized.
}
return _analyticsController = AnalyticsController(
enabled: enabled,
shouldShowConsentMessage: shouldShowConsentMessage,
consentMessage: await dtdManager.analyticsConsentMessage(),
);
}
AnalyticsController? _analyticsController;
/// A synchronous check to see if analytics are enabled.
///
/// Returns `false` if analytics are disabled or not yet initialized.
bool get isAnalyticsEnabled =>
_analyticsController?.analyticsEnabled.value ?? false;
/// Whether the analytics controller has been initialized.
bool get isAnalyticsControllerInitialized => _analyticsController != null;
typedef AsyncAnalyticsCallback = FutureOr<void> Function();
class AnalyticsController {
AnalyticsController({
required bool enabled,
required bool shouldShowConsentMessage,
required this.consentMessage,
}) : analyticsEnabled = ValueNotifier<bool>(enabled),
_shouldPrompt = ValueNotifier<bool>(
shouldShowConsentMessage && consentMessage.isNotEmpty,
) {
if (_shouldPrompt.value) {
unawaited(toggleAnalyticsEnabled(true));
}
if (analyticsEnabled.value) {
setUpAnalytics();
}
}
final ValueNotifier<bool> analyticsEnabled;
ValueListenable<bool> get shouldPrompt => _shouldPrompt;
final ValueNotifier<bool> _shouldPrompt;
bool get analyticsInitialized => _analyticsInitialized;
bool _analyticsInitialized = false;
/// Method to call to confirm with package:unified_analytics the user has
/// seen the consent message.
Future<void> markConsentMessageAsShown() async =>
await dtdManager.analyticsClientShowedMessage();
/// Consent message for package:unified_analytics to be shown on first run.
final String consentMessage;
/// Sets whether google analytics are enabled.
Future<void> toggleAnalyticsEnabled(bool? enable) async {
if (enable == true) {
analyticsEnabled.value = true;
if (!_analyticsInitialized) {
setUpAnalytics();
}
if (kReleaseMode) {
await dtdManager.setAnalyticsTelemetry(true);
}
} else {
analyticsEnabled.value = false;
hidePrompt();
if (kReleaseMode) {
await dtdManager.setAnalyticsTelemetry(false);
}
}
}
void setUpAnalytics() {
if (_analyticsInitialized) return;
assert(analyticsEnabled.value = true);
_analyticsInitialized = true;
}
void hidePrompt() {
_shouldPrompt.value = false;
}
}