From 1c3372fa3825166481e814100cd15c76a4aa4574 Mon Sep 17 00:00:00 2001 From: Elias Yishak <42216813+eliasyishak@users.noreply.github.com> Date: Thu, 18 Apr 2024 18:03:15 -0700 Subject: [PATCH] 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 --- packages/flutter_tools/lib/runner.dart | 15 +++- .../lib/src/commands/config.dart | 3 +- packages/flutter_tools/lib/src/doctor.dart | 3 +- .../general.shard/runner/runner_test.dart | 81 +++++++++++++++++++ 4 files changed, 98 insertions(+), 4 deletions(-) diff --git a/packages/flutter_tools/lib/runner.dart b/packages/flutter_tools/lib/runner.dart index ae80be0628..1f3e399ed0 100644 --- a/packages/flutter_tools/lib/runner.dart +++ b/packages/flutter_tools/lib/runner.dart @@ -92,7 +92,7 @@ Future 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 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); diff --git a/packages/flutter_tools/lib/src/commands/config.dart b/packages/flutter_tools/lib/src/commands/config.dart index 0b22f065c9..81ca66fadb 100644 --- a/packages/flutter_tools/lib/src/commands/config.dart +++ b/packages/flutter_tools/lib/src/commands/config.dart @@ -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); } diff --git a/packages/flutter_tools/lib/src/doctor.dart b/packages/flutter_tools/lib/src/doctor.dart index 202af75d6f..cef3c71904 100644 --- a/packages/flutter_tools/lib/src/doctor.dart +++ b/packages/flutter_tools/lib/src/doctor.dart @@ -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(); } diff --git a/packages/flutter_tools/test/general.shard/runner/runner_test.dart b/packages/flutter_tools/test/general.shard/runner/runner_test.dart index 8954cca4c2..8753e00ddc 100644 --- a/packages/flutter_tools/test/general.shard/runner/runner_test.dart +++ b/packages/flutter_tools/test/general.shard/runner/runner_test.dart @@ -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( + [], + () => [], + // 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: { + 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( + [], + () => [], + // 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: { + Analytics: () => fakeAnalytics, + FileSystem: () => MemoryFileSystem.test(), + ProcessManager: () => FakeProcessManager.any(), + Usage: () => testUsage, + }, + ); + testUsingContext( 'runner enabling analytics with flag', () async {