Skip to content

Commit f97e2e5

Browse files
committed
[unified_analytics] Add Dash ENV var utils
1 parent 5d5b09f commit f97e2e5

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

pkgs/unified_analytics/lib/src/enums.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
List<String> get validDashTools =>
77
DashTool.values.map((e) => e.label).toList()..sort();
88

9+
/// Environment variables used by the unified analytics package.
10+
enum DashEnvVar {
11+
/// Environment variable used to suppress analytics.
12+
suppressAnalytics('DASH__SUPPRESS_ANALYTICS'),
13+
14+
/// Environment variable used to specify the top-level tool.
15+
tool('DASH__TOOL');
16+
17+
final String name;
18+
const DashEnvVar(this.name);
19+
}
20+
921
/// Values for the event name to be sent to Google Analytics.
1022
///
1123
/// The [label] for each enum value is what will be logged, the [description]

pkgs/unified_analytics/lib/src/utils.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ bool checkDirectoryForWritePermissions(Directory directory) {
3535
return fileStat.modeString()[1] == 'w';
3636
}
3737

38+
/// Compute whether to suppress analytics based on the environment variable
39+
/// `DASH__SUPPRESS_ANALYTICS`.
40+
///
41+
/// If the environment variable is set and not "false", return the
42+
/// corresponding boolean value. Otherwise, return the [defaultValue].
43+
bool computeSuppressAnalytics({bool defaultValue = false}) =>
44+
bool.fromEnvironment(
45+
DashEnvVar.suppressAnalytics.name,
46+
defaultValue: defaultValue,
47+
);
48+
49+
/// Compute the top-level tool from the environment variable `DASH__TOOL`.
50+
///
51+
/// If the environment variable is set and valid, return the corresponding
52+
/// [DashTool]. Otherwise, return the [current] tool.
53+
DashTool computeTopLevelTool(DashTool current) {
54+
final toolValue = io.Platform.environment[DashEnvVar.tool.name];
55+
if (toolValue != null) {
56+
try {
57+
return DashTool.fromLabel(toolValue);
58+
} on Exception {
59+
// Fallback to `current` if the value in ENV is invalid.
60+
// todo(pq): do we need better error handling here?
61+
}
62+
}
63+
64+
return current;
65+
}
66+
3867
/// Format time as 'yyyy-MM-dd HH:mm:ss Z' where Z is the difference between the
3968
/// timezone of t and UTC formatted according to RFC 822.
4069
String formatDateTime(DateTime t) {

pkgs/unified_analytics/lib/unified_analytics.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
export 'src/analytics.dart' show Analytics, FakeAnalytics, NoOpAnalytics;
66
export 'src/config_handler.dart' show ToolInfo;
7-
export 'src/enums.dart' show DashTool;
7+
export 'src/enums.dart' show DashEnvVar, DashTool;
88
export 'src/event.dart' show CustomMetrics, Event;
99
export 'src/log_handler.dart' show LogFileStats;
1010
export 'src/survey_handler.dart' show Survey, SurveyButton, SurveyHandler;
11-
export 'src/utils.dart' show parseDartSDKVersion;
11+
export 'src/utils.dart'
12+
show computeSuppressAnalytics, computeTopLevelTool, parseDartSDKVersion;

0 commit comments

Comments
 (0)