Skip to content

Commit ceebdf1

Browse files
eliasyishakpull[bot]
authored andcommitted
Opt out users from GA3 if opted out of GA4 (flutter#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
1 parent c31d7e9 commit ceebdf1

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

packages/flutter_tools/lib/runner.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Future<int> run(
9292

9393
// TODO(eliasyishak): Set the telemetry for the unified_analytics
9494
// package as well, the above will be removed once we have
95-
// fully transitioned to using the new package
95+
// fully transitioned to using the new package, https://github.com/flutter/flutter/issues/128251
9696
await globals.analytics.setTelemetry(false);
9797
}
9898

@@ -111,10 +111,21 @@ Future<int> run(
111111

112112
// TODO(eliasyishak): Set the telemetry for the unified_analytics
113113
// package as well, the above will be removed once we have
114-
// fully transitioned to using the new package
114+
// fully transitioned to using the new package, https://github.com/flutter/flutter/issues/128251
115115
await globals.analytics.setTelemetry(true);
116116
}
117117

118+
// Ensure that anyone opted out of package:unified_analytics is also
119+
// opted out of legacy analytics and that analytics is not being suppressed
120+
// TODO(eliasyishak): remove once GA3 sunset, https://github.com/flutter/flutter/issues/128251
121+
if (!globals.analytics.telemetryEnabled &&
122+
globals.flutterUsage.enabled) {
123+
UsageEvent(
124+
'ga4_and_ga3_status_mismatch',
125+
'opted_out_of_ga4',
126+
flutterUsage: globals.flutterUsage,
127+
).send();
128+
}
118129

119130
await runner.run(args);
120131

packages/flutter_tools/lib/src/commands/config.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ class ConfigCommand extends FlutterCommand {
133133

134134
// TODO(eliasyishak): Set the telemetry for the unified_analytics
135135
// package as well, the above will be removed once we have
136-
// fully transitioned to using the new package
136+
// fully transitioned to using the new package,
137+
// https://github.com/flutter/flutter/issues/128251
137138
await globals.analytics.setTelemetry(value);
138139
}
139140

packages/flutter_tools/lib/src/doctor.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ class Doctor {
453453
doctorInvocationId: analyticsTimestamp,
454454
));
455455
}
456-
// TODO(eliasyishak): remove this after migrating from package:usage
456+
// TODO(eliasyishak): remove this after migrating from package:usage,
457+
// https://github.com/flutter/flutter/issues/128251
457458
DoctorResultEvent(validator: validator, result: result).send();
458459
}
459460

packages/flutter_tools/test/general.shard/runner/runner_test.dart

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ void main() {
353353
group('unified_analytics', () {
354354
late FakeAnalytics fakeAnalytics;
355355
late MemoryFileSystem fs;
356+
late TestUsage testUsage;
356357

357358
setUp(() {
358359
fs = MemoryFileSystem.test();
@@ -361,6 +362,7 @@ void main() {
361362
fs: fs,
362363
fakeFlutterVersion: FakeFlutterVersion(),
363364
);
365+
testUsage = TestUsage();
364366
});
365367

366368
testUsingContext(
@@ -387,6 +389,85 @@ void main() {
387389
},
388390
);
389391

392+
testUsingContext(
393+
'runner sends mismatch event to ga3 if user opted in to ga3 but out of ga4 analytics',
394+
() async {
395+
io.setExitFunctionForTests((int exitCode) {});
396+
397+
// Begin by opting out of telemetry for package:unified_analytics
398+
// and leaving legacy analytics opted in
399+
await fakeAnalytics.setTelemetry(false);
400+
expect(fakeAnalytics.telemetryEnabled, false);
401+
expect(testUsage.enabled, true);
402+
403+
await runner.run(
404+
<String>[],
405+
() => <FlutterCommand>[],
406+
// This flutterVersion disables crash reporting.
407+
flutterVersion: '[user-branch]/',
408+
shutdownHooks: ShutdownHooks(),
409+
);
410+
411+
expect(
412+
testUsage.events,
413+
contains(const TestUsageEvent(
414+
'ga4_and_ga3_status_mismatch',
415+
'opted_out_of_ga4',
416+
)),
417+
);
418+
expect(fakeAnalytics.telemetryEnabled, false);
419+
expect(testUsage.enabled, true);
420+
expect(fakeAnalytics.sentEvents, isEmpty);
421+
422+
},
423+
overrides: <Type, Generator>{
424+
Analytics: () => fakeAnalytics,
425+
FileSystem: () => MemoryFileSystem.test(),
426+
ProcessManager: () => FakeProcessManager.any(),
427+
Usage: () => testUsage,
428+
},
429+
);
430+
431+
testUsingContext(
432+
'runner does not send mismatch event to ga3 if user opted out of ga3 & ga4 analytics',
433+
() async {
434+
io.setExitFunctionForTests((int exitCode) {});
435+
436+
// Begin by opting out of telemetry for package:unified_analytics
437+
// and leaving legacy analytics opted in
438+
await fakeAnalytics.setTelemetry(false);
439+
testUsage.enabled = false;
440+
expect(fakeAnalytics.telemetryEnabled, false);
441+
expect(testUsage.enabled, false);
442+
443+
await runner.run(
444+
<String>[],
445+
() => <FlutterCommand>[],
446+
// This flutterVersion disables crash reporting.
447+
flutterVersion: '[user-branch]/',
448+
shutdownHooks: ShutdownHooks(),
449+
);
450+
451+
expect(
452+
testUsage.events,
453+
isNot(contains(const TestUsageEvent(
454+
'ga4_and_ga3_status_mismatch',
455+
'opted_out_of_ga4',
456+
))),
457+
);
458+
expect(fakeAnalytics.telemetryEnabled, false);
459+
expect(testUsage.enabled, false);
460+
expect(fakeAnalytics.sentEvents, isEmpty);
461+
462+
},
463+
overrides: <Type, Generator>{
464+
Analytics: () => fakeAnalytics,
465+
FileSystem: () => MemoryFileSystem.test(),
466+
ProcessManager: () => FakeProcessManager.any(),
467+
Usage: () => testUsage,
468+
},
469+
);
470+
390471
testUsingContext(
391472
'runner enabling analytics with flag',
392473
() async {

0 commit comments

Comments
 (0)