Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dart-lang/tools
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 28b7be8
Choose a base ref
..
head repository: dart-lang/tools
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a1c3506
Choose a head ref
2 changes: 1 addition & 1 deletion .github/workflows/unified_analytics.yml
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ jobs:
run-tests: true
steps:
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c
- uses: dart-lang/setup-dart@a57a6c04cf7d4840e88432aad6281d1e125f0d46
- uses: dart-lang/setup-dart@929ed5f8bae55086c6fac456ba5acb9763c9e3e2
with:
sdk: ${{matrix.sdk}}

25 changes: 23 additions & 2 deletions pkgs/unified_analytics/lib/src/analytics.dart
Original file line number Diff line number Diff line change
@@ -130,6 +130,9 @@ abstract class Analytics {

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

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

@override
Future<void> setTelemetry(bool reportingBool) =>
_configHandler.setTelemetry(reportingBool);
Future<void> setTelemetry(bool reportingBool) {
_configHandler.setTelemetry(reportingBool);

// Construct the body of the request to signal
// telemetry status toggling
//
// We use don't use the sendEvent method because it may
// be blocked by the [telemetryEnabled] getter
final Map<String, Object?> body = generateRequestBody(
clientId: _clientId,
eventName: DashEvent.analyticsCollectionEnabled,
eventData: {'status': reportingBool},
userProperty: userProperty,
);

_logHandler.save(data: body);

// Pass to the google analytics client to send
return _gaClient.sendData(body);
}
}

/// This class extends [AnalyticsImpl] and subs out any methods that
12 changes: 10 additions & 2 deletions pkgs/unified_analytics/lib/src/enums.dart
Original file line number Diff line number Diff line change
@@ -7,6 +7,14 @@
/// The [label] for each enum value is what will be logged, the [description]
/// is here for documentation purposes
enum DashEvent {
// Events that can be sent by all tools; these
// events should not be tool specific; toolOwner
// not necessary for these events
analyticsCollectionEnabled(
label: 'analytics_collection_enabled',
description: 'The opt-in status for analytics collection',
),

// Events for flutter_tools
hotReloadTime(
label: 'hot_reload_time',
@@ -49,11 +57,11 @@ enum DashEvent {

final String label;
final String description;
final DashTool toolOwner;
final DashTool? toolOwner;
const DashEvent({
required this.label,
required this.description,
required this.toolOwner,
this.toolOwner,
});
}

2 changes: 1 addition & 1 deletion pkgs/unified_analytics/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ version: 0.1.0-dev
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics

environment:
sdk: '>=2.19.0 <3.0.0'
sdk: '>=2.19.0 <4.0.0'

dependencies:
clock: ^1.1.1
24 changes: 23 additions & 1 deletion pkgs/unified_analytics/test/unified_analytics_test.dart
Original file line number Diff line number Diff line change
@@ -142,16 +142,38 @@ void main() {
expect(analytics.telemetryEnabled, true,
reason: 'Telemetry should be enabled by default '
'when initialized for the first time');

// Use the API to disable analytics
expect(logFile.readAsLinesSync().length, 0);
await analytics.setTelemetry(false);
expect(analytics.telemetryEnabled, false,
reason: 'Analytics telemetry should be disabled');
expect(logFile.readAsLinesSync().length, 1,
reason: 'One event should have been logged for disabling analytics');

// Extract the last log item to check for the keys
Map<String, Object?> lastLogItem =
jsonDecode(logFile.readAsLinesSync().last);
expect((lastLogItem['events'] as List).last['name'],
'analytics_collection_enabled',
reason: 'Check on event name');
expect((lastLogItem['events'] as List).last['params']['status'], false,
reason: 'Status should be false');

// Toggle it back to being enabled
await analytics.setTelemetry(true);
expect(analytics.telemetryEnabled, true,
reason: 'Analytics telemetry should be enabled');
expect(logFile.readAsLinesSync().length, 2,
reason: 'Second event should have been logged toggling '
'analytics back on');

// Extract the last log item to check for the keys
lastLogItem = jsonDecode(logFile.readAsLinesSync().last);
expect((lastLogItem['events'] as List).last['name'],
'analytics_collection_enabled',
reason: 'Check on event name');
expect((lastLogItem['events'] as List).last['params']['status'], true,
reason: 'Status should be false');
});

test(