Opt out users from GA3 if opted out of GA4 (#146453)

This PR will opt out users from legacy analytics if they have already been opted out from package:unified_analytics.

After successfully merging into main, this will be CP'd into beta and stable channels
This commit is contained in:
Elias Yishak
2024-04-18 18:03:15 -07:00
committed by GitHub
parent 8c891ba5ba
commit 1c3372fa38
4 changed files with 98 additions and 4 deletions

View File

@@ -92,7 +92,7 @@ Future<int> run(
// 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
// fully transitioned to using the new package, https://github.com/flutter/flutter/issues/128251
await globals.analytics.setTelemetry(false);
}
@@ -111,10 +111,21 @@ Future<int> run(
// 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
// fully transitioned to using the new package, https://github.com/flutter/flutter/issues/128251
await globals.analytics.setTelemetry(true);
}
// Ensure that anyone opted out of package:unified_analytics is also
// opted out of legacy analytics and that analytics is not being suppressed
// TODO(eliasyishak): remove once GA3 sunset, https://github.com/flutter/flutter/issues/128251
if (!globals.analytics.telemetryEnabled &&
globals.flutterUsage.enabled) {
UsageEvent(
'ga4_and_ga3_status_mismatch',
'opted_out_of_ga4',
flutterUsage: globals.flutterUsage,
).send();
}
await runner.run(args);

View File

@@ -133,7 +133,8 @@ class ConfigCommand extends FlutterCommand {
// 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
// fully transitioned to using the new package,
// https://github.com/flutter/flutter/issues/128251
await globals.analytics.setTelemetry(value);
}

View File

@@ -453,7 +453,8 @@ class Doctor {
doctorInvocationId: analyticsTimestamp,
));
}
// TODO(eliasyishak): remove this after migrating from package:usage
// TODO(eliasyishak): remove this after migrating from package:usage,
// https://github.com/flutter/flutter/issues/128251
DoctorResultEvent(validator: validator, result: result).send();
}

View File

@@ -353,6 +353,7 @@ void main() {
group('unified_analytics', () {
late FakeAnalytics fakeAnalytics;
late MemoryFileSystem fs;
late TestUsage testUsage;
setUp(() {
fs = MemoryFileSystem.test();
@@ -361,6 +362,7 @@ void main() {
fs: fs,
fakeFlutterVersion: FakeFlutterVersion(),
);
testUsage = TestUsage();
});
testUsingContext(
@@ -387,6 +389,85 @@ void main() {
},
);
testUsingContext(
'runner sends mismatch event to ga3 if user opted in to ga3 but out of ga4 analytics',
() async {
io.setExitFunctionForTests((int exitCode) {});
// Begin by opting out of telemetry for package:unified_analytics
// and leaving legacy analytics opted in
await fakeAnalytics.setTelemetry(false);
expect(fakeAnalytics.telemetryEnabled, false);
expect(testUsage.enabled, true);
await runner.run(
<String>[],
() => <FlutterCommand>[],
// This flutterVersion disables crash reporting.
flutterVersion: '[user-branch]/',
shutdownHooks: ShutdownHooks(),
);
expect(
testUsage.events,
contains(const TestUsageEvent(
'ga4_and_ga3_status_mismatch',
'opted_out_of_ga4',
)),
);
expect(fakeAnalytics.telemetryEnabled, false);
expect(testUsage.enabled, true);
expect(fakeAnalytics.sentEvents, isEmpty);
},
overrides: <Type, Generator>{
Analytics: () => fakeAnalytics,
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
Usage: () => testUsage,
},
);
testUsingContext(
'runner does not send mismatch event to ga3 if user opted out of ga3 & ga4 analytics',
() async {
io.setExitFunctionForTests((int exitCode) {});
// Begin by opting out of telemetry for package:unified_analytics
// and leaving legacy analytics opted in
await fakeAnalytics.setTelemetry(false);
testUsage.enabled = false;
expect(fakeAnalytics.telemetryEnabled, false);
expect(testUsage.enabled, false);
await runner.run(
<String>[],
() => <FlutterCommand>[],
// This flutterVersion disables crash reporting.
flutterVersion: '[user-branch]/',
shutdownHooks: ShutdownHooks(),
);
expect(
testUsage.events,
isNot(contains(const TestUsageEvent(
'ga4_and_ga3_status_mismatch',
'opted_out_of_ga4',
))),
);
expect(fakeAnalytics.telemetryEnabled, false);
expect(testUsage.enabled, false);
expect(fakeAnalytics.sentEvents, isEmpty);
},
overrides: <Type, Generator>{
Analytics: () => fakeAnalytics,
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
Usage: () => testUsage,
},
);
testUsingContext(
'runner enabling analytics with flag',
() async {