Skip to content

Commit b97bd5c

Browse files
authored
Checking for devtools config file for opt out (#227)
* Checking for devtools config file for opt out * Update version * Correct mislabeled tests
1 parent 8ffc077 commit b97bd5c

File tree

5 files changed

+155
-5
lines changed

5 files changed

+155
-5
lines changed

pkgs/unified_analytics/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.8.1
2+
3+
- Check devtools config file for legacy opt out status
4+
15
## 5.8.0
26

37
- Fix template string for consent message

pkgs/unified_analytics/lib/src/constants.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const int kLogFileLength = 2500;
8282
const String kLogFileName = 'dart-flutter-telemetry.log';
8383

8484
/// The current version of the package, should be in line with pubspec version.
85-
const String kPackageVersion = '5.8.0';
85+
const String kPackageVersion = '5.8.1';
8686

8787
/// The minimum length for a session.
8888
const int kSessionDurationMinutes = 30;

pkgs/unified_analytics/lib/src/utils.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,17 @@ Directory? getHomeDirectory(FileSystem fs) {
137137
/// Dart: `$HOME/.dart/dartdev.json`
138138
///
139139
/// Flutter: `$HOME/.flutter`
140+
///
141+
/// Devtools: `$HOME/.flutter-devtools/.devtools`
140142
bool legacyOptOut({
141143
required FileSystem fs,
142144
required Directory home,
143145
}) {
144146
final dartLegacyConfigFile =
145147
fs.file(p.join(home.path, '.dart', 'dartdev.json'));
146148
final flutterLegacyConfigFile = fs.file(p.join(home.path, '.flutter'));
149+
final devtoolsLegacyConfigFile =
150+
fs.file(p.join(home.path, '.flutter-devtools', '.devtools'));
147151

148152
// Example of what the file looks like for dart
149153
//
@@ -202,6 +206,38 @@ bool legacyOptOut({
202206
}
203207
}
204208

209+
// Example of what the file looks like for devtools
210+
//
211+
// {
212+
// "analyticsEnabled": false, <-- THIS USER HAS OPTED OUT
213+
// "isFirstRun": false,
214+
// "lastReleaseNotesVersion": "2.31.0",
215+
// "2023-Q4": {
216+
// "surveyActionTaken": false,
217+
// "surveyShownCount": 0
218+
// }
219+
// }
220+
if (devtoolsLegacyConfigFile.existsSync()) {
221+
try {
222+
final devtoolsObj =
223+
jsonDecode(devtoolsLegacyConfigFile.readAsStringSync())
224+
as Map<String, Object?>;
225+
if (devtoolsObj.containsKey('analyticsEnabled') &&
226+
devtoolsObj['analyticsEnabled'] == false) {
227+
return true;
228+
}
229+
} on FormatException {
230+
// In the case of an error when parsing the json file, return true
231+
// which will result in the user being opted out of unified_analytics
232+
//
233+
// A corrupted file could mean they opted out previously but for some
234+
// reason, the file was written incorrectly
235+
return true;
236+
} on FileSystemException {
237+
return true;
238+
}
239+
}
240+
205241
return false;
206242
}
207243

pkgs/unified_analytics/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: >-
44
to Google Analytics.
55
# When updating this, keep the version consistent with the changelog and the
66
# value in lib/src/constants.dart.
7-
version: 5.8.0
7+
version: 5.8.1
88
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics
99

1010
environment:

pkgs/unified_analytics/test/legacy_analytics_test.dart

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void main() {
102102
});
103103

104104
test('Honor legacy flutter analytics opt out', () {
105-
// Create the file for the dart legacy opt out
105+
// Create the file for the flutter legacy opt out
106106
final flutterLegacyConfigFile =
107107
home.childDirectory('.dart').childFile('dartdev.json');
108108
flutterLegacyConfigFile.createSync(recursive: true);
@@ -134,7 +134,7 @@ void main() {
134134
});
135135

136136
test('Telemetry enabled if legacy flutter analytics is enabled', () {
137-
// Create the file for the dart legacy opt out
137+
// Create the file for the flutter legacy opt out
138138
final flutterLegacyConfigFile =
139139
home.childDirectory('.dart').childFile('dartdev.json');
140140
flutterLegacyConfigFile.createSync(recursive: true);
@@ -165,6 +165,78 @@ void main() {
165165
expect(analytics.telemetryEnabled, true);
166166
});
167167

168+
test('Honor legacy devtools analytics opt out', () {
169+
// Create the file for the devtools legacy opt out
170+
final devtoolsLegacyConfigFile =
171+
home.childDirectory('.flutter-devtools').childFile('.devtools');
172+
devtoolsLegacyConfigFile.createSync(recursive: true);
173+
devtoolsLegacyConfigFile.writeAsStringSync('''
174+
{
175+
"analyticsEnabled": false,
176+
"isFirstRun": false,
177+
"lastReleaseNotesVersion": "2.31.0",
178+
"2023-Q4": {
179+
"surveyActionTaken": false,
180+
"surveyShownCount": 0
181+
}
182+
}
183+
''');
184+
185+
// The main analytics instance, other instances can be spawned within tests
186+
// to test how to instances running together work
187+
analytics = Analytics.test(
188+
tool: initialTool,
189+
homeDirectory: home,
190+
measurementId: measurementId,
191+
apiSecret: apiSecret,
192+
flutterChannel: flutterChannel,
193+
toolsMessageVersion: toolsMessageVersion,
194+
toolsMessage: toolsMessage,
195+
flutterVersion: flutterVersion,
196+
dartVersion: dartVersion,
197+
fs: fs,
198+
platform: platform,
199+
);
200+
201+
expect(analytics.telemetryEnabled, false);
202+
});
203+
204+
test('Telemetry enabled if legacy devtools analytics is enabled', () {
205+
// Create the file for the devtools legacy opt out
206+
final devtoolsLegacyConfigFile =
207+
home.childDirectory('.flutter-devtools').childFile('.devtools');
208+
devtoolsLegacyConfigFile.createSync(recursive: true);
209+
devtoolsLegacyConfigFile.writeAsStringSync('''
210+
{
211+
"analyticsEnabled": true,
212+
"isFirstRun": false,
213+
"lastReleaseNotesVersion": "2.31.0",
214+
"2023-Q4": {
215+
"surveyActionTaken": false,
216+
"surveyShownCount": 0
217+
}
218+
}
219+
''');
220+
221+
// The main analytics instance, other instances can be spawned within tests
222+
// to test how to instances running together work
223+
analytics = Analytics.test(
224+
tool: initialTool,
225+
homeDirectory: home,
226+
measurementId: measurementId,
227+
apiSecret: apiSecret,
228+
flutterChannel: flutterChannel,
229+
toolsMessageVersion: toolsMessageVersion,
230+
toolsMessage: toolsMessage,
231+
flutterVersion: flutterVersion,
232+
dartVersion: dartVersion,
233+
fs: fs,
234+
platform: platform,
235+
);
236+
237+
expect(analytics.telemetryEnabled, true);
238+
});
239+
168240
test('Telemetry disabled if dart config file corrupted', () {
169241
// Create the file for the dart legacy opt out with text that
170242
// is not valid JSON
@@ -199,8 +271,46 @@ NOT VALID JSON
199271
expect(analytics.telemetryEnabled, false);
200272
});
201273

274+
test('Telemetry disabled if devtools config file corrupted', () {
275+
// Create the file for the devtools legacy opt out with text that
276+
// is not valid JSON
277+
final devtoolsLegacyConfigFile =
278+
home.childDirectory('.flutter-devtools').childFile('.devtools');
279+
devtoolsLegacyConfigFile.createSync(recursive: true);
280+
devtoolsLegacyConfigFile.writeAsStringSync('''
281+
NOT VALID JSON
282+
{
283+
"analyticsEnabled": true,
284+
"isFirstRun": false,
285+
"lastReleaseNotesVersion": "2.31.0",
286+
"2023-Q4": {
287+
"surveyActionTaken": false,
288+
"surveyShownCount": 0
289+
}
290+
}
291+
''');
292+
293+
// The main analytics instance, other instances can be spawned within tests
294+
// to test how to instances running together work
295+
analytics = Analytics.test(
296+
tool: initialTool,
297+
homeDirectory: home,
298+
measurementId: measurementId,
299+
apiSecret: apiSecret,
300+
flutterChannel: flutterChannel,
301+
toolsMessageVersion: toolsMessageVersion,
302+
toolsMessage: toolsMessage,
303+
flutterVersion: flutterVersion,
304+
dartVersion: dartVersion,
305+
fs: fs,
306+
platform: platform,
307+
);
308+
309+
expect(analytics.telemetryEnabled, false);
310+
});
311+
202312
test('Telemetry disabled if flutter config file corrupted', () {
203-
// Create the file for the dart legacy opt out with text that
313+
// Create the file for the flutter legacy opt out with text that
204314
// is not valid JSON
205315
final fluttterLegacyConfigFile =
206316
home.childDirectory('.dart').childFile('dartdev.json');

0 commit comments

Comments
 (0)