Fix analytics enabled/disabled event not being sent when the user enables/disables analytics (#160060)

Fixes https://github.com/flutter/flutter/issues/160058.

In addition, only send an event when the enabled-state of analytics
actually gets flipped.


<details>

<summary> Pre-launch checklist </summary> 


- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

</details>


<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
Andrew Kolos 2024-12-12 11:59:50 -08:00 committed by GitHub
parent 6966a2eef1
commit 6b12651419
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 83 additions and 40 deletions

View File

@ -73,47 +73,28 @@ Future<int> run(
exitCode: 1);
}
// Disable analytics if user passes in the `--disable-analytics` option
// "flutter --disable-analytics"
//
// Same functionality as "flutter config --no-analytics" for disabling
// except with the `value` hard coded as false
if (args.contains('--disable-analytics')) {
// The tool sends the analytics event *before* toggling the flag
// intentionally to be sure that opt-out events are sent correctly.
AnalyticsConfigEvent(enabled: false).send();
// Normally, the tool waits for the analytics to all send before the
// tool exits, but only when analytics are enabled. When reporting that
// analytics have been disable, the wait must be done here instead.
await globals.flutterUsage.ensureAnalyticsSent();
globals.flutterUsage.enabled = false;
globals.printStatus('Analytics reporting disabled.');
// TODO(eliasyishak): Set the telemetry for the unified_analytics
// package as well, the above will be removed once we have
// fully transitioned to using the new package, https://github.com/flutter/flutter/issues/128251
if (globals.analytics.telemetryEnabled) {
globals.analytics.send(
Event.analyticsCollectionEnabled(status: false),
);
// Before disablig analytics, we need to close the client to make
// sure the above collection event is sent.
await globals.analytics.close();
}
await globals.analytics.setTelemetry(false);
globals.printStatus('Analytics reporting disabled.');
}
// Enable analytics if user passes in the `--enable-analytics` option
// `flutter --enable-analytics`
//
// Same functionality as `flutter config --analytics` for enabling
// except with the `value` hard coded as true
if (args.contains('--enable-analytics')) {
// The tool sends the analytics event *before* toggling the flag
// intentionally to be sure that opt-out events are sent correctly.
AnalyticsConfigEvent(enabled: true).send();
globals.flutterUsage.enabled = true;
globals.printStatus('Analytics reporting enabled.');
// TODO(eliasyishak): Set the telemetry for the unified_analytics
// package as well, the above will be removed once we have
// fully transitioned to using the new package, https://github.com/flutter/flutter/issues/128251
final bool alreadyEnabled = globals.analytics.telemetryEnabled;
await globals.analytics.setTelemetry(true);
if (!alreadyEnabled) {
globals.analytics.send(
Event.analyticsCollectionEnabled(status: true),
);
}
globals.printStatus('Analytics reporting enabled.');
}
// Send an event to GA3 for any users that are opted into GA3

View File

@ -541,7 +541,7 @@ void main() {
);
testUsingContext(
'runner enabling analytics with flag',
'--enable-analytics and --disable-analytics enables/disables telemetry',
() async {
io.setExitFunctionForTests((int exitCode) {});
@ -550,8 +550,6 @@ void main() {
await runner.run(
<String>['--disable-analytics'],
() => <FlutterCommand>[],
// This flutterVersion disables crash reporting.
flutterVersion: '[user-branch]/',
shutdownHooks: ShutdownHooks(),
);
@ -560,8 +558,6 @@ void main() {
await runner.run(
<String>['--enable-analytics'],
() => <FlutterCommand>[],
// This flutterVersion disables crash reporting.
flutterVersion: '[user-branch]/',
shutdownHooks: ShutdownHooks(),
);
@ -574,6 +570,72 @@ void main() {
},
);
testUsingContext(
'--enable-analytics and --disable-analytics send an event when telemetry is enabled/disabled',
() async {
io.setExitFunctionForTests((int exitCode) {});
await globals.analytics.setTelemetry(true);
await runner.run(
<String>['--disable-analytics'],
() => <FlutterCommand>[],
shutdownHooks: ShutdownHooks(),
);
expect(
(globals.analytics as FakeAnalytics).sentEvents,
<Event>[Event.analyticsCollectionEnabled(status: false)],
);
(globals.analytics as FakeAnalytics).sentEvents.clear();
expect(globals.analytics.telemetryEnabled, false);
await runner.run(
<String>['--enable-analytics'],
() => <FlutterCommand>[],
shutdownHooks: ShutdownHooks(),
);
expect((globals.analytics as FakeAnalytics).sentEvents, <Event>[
Event.analyticsCollectionEnabled(status: true),
]);
},
overrides: <Type, Generator>{
Analytics: () => fakeAnalytics,
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
},
);
testUsingContext(
'--enable-analytics and --disable-analytics do not send an event when telemetry is already enabled/disabled',
() async {
io.setExitFunctionForTests((int exitCode) {});
await globals.analytics.setTelemetry(false);
await runner.run(
<String>['--disable-analytics'],
() => <FlutterCommand>[],
shutdownHooks: ShutdownHooks(),
);
expect((globals.analytics as FakeAnalytics).sentEvents, isEmpty);
await globals.analytics.setTelemetry(true);
await runner.run(
<String>['--enable-analytics'],
() => <FlutterCommand>[],
shutdownHooks: ShutdownHooks(),
);
expect((globals.analytics as FakeAnalytics).sentEvents, isEmpty);
},
overrides: <Type, Generator>{
Analytics: () => fakeAnalytics,
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
},
);
testUsingContext(
'throw error when both flags passed',
() async {