diff --git a/packages/flutter_tools/lib/runner.dart b/packages/flutter_tools/lib/runner.dart index ae80be0628261..e8014321b6e1e 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); } + // Send an event to GA3 for any users that are opted into GA3 + // analytics but have opted out of GA4 (package:unified_analytics) + // 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 0b22f065c9654..81ca66fadb3db 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 202af75d6ff2e..cef3c71904475 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 8954cca4c23fe..3bb02b8ccfe9d 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 legacy analytics + 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 {