Skip to content

Commit 5a72344

Browse files
authored
Add DashEvent for toggling telemetry collection + send event whenever toggled (#23)
* Added new event + modified setTelemetry to send toggle status to GA * setTelemetry saves the event + tests added
1 parent 28b7be8 commit 5a72344

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

pkgs/unified_analytics/lib/src/analytics.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ abstract class Analytics {
130130

131131
/// Pass a boolean to either enable or disable telemetry and make
132132
/// the necessary changes in the persisted configuration file
133+
///
134+
/// Setting the telemetry status will also send an event to GA
135+
/// indicating the latest status of the telemetry from [reportingBool]
133136
Future<void> setTelemetry(bool reportingBool);
134137
}
135138

@@ -260,8 +263,26 @@ class AnalyticsImpl implements Analytics {
260263
}
261264

262265
@override
263-
Future<void> setTelemetry(bool reportingBool) =>
264-
_configHandler.setTelemetry(reportingBool);
266+
Future<void> setTelemetry(bool reportingBool) {
267+
_configHandler.setTelemetry(reportingBool);
268+
269+
// Construct the body of the request to signal
270+
// telemetry status toggling
271+
//
272+
// We use don't use the sendEvent method because it may
273+
// be blocked by the [telemetryEnabled] getter
274+
final Map<String, Object?> body = generateRequestBody(
275+
clientId: _clientId,
276+
eventName: DashEvent.analyticsCollectionEnabled,
277+
eventData: {'status': reportingBool},
278+
userProperty: userProperty,
279+
);
280+
281+
_logHandler.save(data: body);
282+
283+
// Pass to the google analytics client to send
284+
return _gaClient.sendData(body);
285+
}
265286
}
266287

267288
/// This class extends [AnalyticsImpl] and subs out any methods that

pkgs/unified_analytics/lib/src/enums.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
/// The [label] for each enum value is what will be logged, the [description]
88
/// is here for documentation purposes
99
enum DashEvent {
10+
// Events that can be sent by all tools; these
11+
// events should not be tool specific; toolOwner
12+
// not necessary for these events
13+
analyticsCollectionEnabled(
14+
label: 'analytics_collection_enabled',
15+
description: 'The opt-in status for analytics collection',
16+
),
17+
1018
// Events for flutter_tools
1119
hotReloadTime(
1220
label: 'hot_reload_time',
@@ -49,11 +57,11 @@ enum DashEvent {
4957

5058
final String label;
5159
final String description;
52-
final DashTool toolOwner;
60+
final DashTool? toolOwner;
5361
const DashEvent({
5462
required this.label,
5563
required this.description,
56-
required this.toolOwner,
64+
this.toolOwner,
5765
});
5866
}
5967

pkgs/unified_analytics/test/unified_analytics_test.dart

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,38 @@ void main() {
142142
expect(analytics.telemetryEnabled, true,
143143
reason: 'Telemetry should be enabled by default '
144144
'when initialized for the first time');
145-
146145
// Use the API to disable analytics
146+
expect(logFile.readAsLinesSync().length, 0);
147147
await analytics.setTelemetry(false);
148148
expect(analytics.telemetryEnabled, false,
149149
reason: 'Analytics telemetry should be disabled');
150+
expect(logFile.readAsLinesSync().length, 1,
151+
reason: 'One event should have been logged for disabling analytics');
152+
153+
// Extract the last log item to check for the keys
154+
Map<String, Object?> lastLogItem =
155+
jsonDecode(logFile.readAsLinesSync().last);
156+
expect((lastLogItem['events'] as List).last['name'],
157+
'analytics_collection_enabled',
158+
reason: 'Check on event name');
159+
expect((lastLogItem['events'] as List).last['params']['status'], false,
160+
reason: 'Status should be false');
150161

151162
// Toggle it back to being enabled
152163
await analytics.setTelemetry(true);
153164
expect(analytics.telemetryEnabled, true,
154165
reason: 'Analytics telemetry should be enabled');
166+
expect(logFile.readAsLinesSync().length, 2,
167+
reason: 'Second event should have been logged toggling '
168+
'analytics back on');
169+
170+
// Extract the last log item to check for the keys
171+
lastLogItem = jsonDecode(logFile.readAsLinesSync().last);
172+
expect((lastLogItem['events'] as List).last['name'],
173+
'analytics_collection_enabled',
174+
reason: 'Check on event name');
175+
expect((lastLogItem['events'] as List).last['params']['status'], true,
176+
reason: 'Status should be false');
155177
});
156178

157179
test(

0 commit comments

Comments
 (0)