Skip to content

[wip] Clear all persisted files of contents on opt out #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions pkgs/unified_analytics/lib/src/analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class AnalyticsImpl implements Analytics {
late final ConfigHandler _configHandler;
final GAClient _gaClient;
late final String _clientId;
late final File _clientIdFile;
late final UserProperty userProperty;
late final LogHandler _logHandler;
final int toolsMessageVersion;
Expand Down Expand Up @@ -340,10 +341,9 @@ class AnalyticsImpl implements Analytics {
_showMessage = true;
}

_clientId = fs
.file(p.join(
homeDirectory.path, kDartToolDirectoryName, kClientIdFileName))
.readAsStringSync();
_clientIdFile = fs.file(
p.join(homeDirectory.path, kDartToolDirectoryName, kClientIdFileName));
_clientId = _clientIdFile.readAsStringSync();

// Create the session instance that will be responsible for managing
// all the sessions across every client tool using this pakage
Expand Down Expand Up @@ -464,6 +464,13 @@ class AnalyticsImpl implements Analytics {
Future<void> setTelemetry(bool reportingBool) {
_configHandler.setTelemetry(reportingBool);

// If telemetry is being set to false, then overwrite the
// CLIENT_ID file with a new id so that if the user chooses
// to opt back in at a later time, they will be a fresh new user
if (!reportingBool && _clientIdFile.existsSync()) {
Initializer.createClientIdFile(clientFile: _clientIdFile);
}

// Construct the body of the request to signal
// telemetry status toggling
//
Expand Down
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/lib/src/initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Initializer {
/// Creates the text file that will contain the client ID
/// which will be used across all related tools for analytics
/// reporting in GA
void createClientIdFile({required File clientFile}) {
static void createClientIdFile({required File clientFile}) {
clientFile.createSync(recursive: true);
clientFile.writeAsStringSync(Uuid().generateV4());
}
Expand Down
27 changes: 27 additions & 0 deletions pkgs/unified_analytics/test/workflow_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,31 @@ void main() {
.endsWith('${secondTool.label}=$dateStamp,$secondVersion\n'),
true);
});

test('Opting out will reset the CLIENT_ID file', () async {
final Analytics firstAnalytics = Analytics.test(
tool: initialTool,
homeDirectory: home,
measurementId: measurementId,
apiSecret: apiSecret,
flutterChannel: flutterChannel,
toolsMessageVersion: toolsMessageVersion,
toolsMessage: toolsMessage,
flutterVersion: flutterVersion,
dartVersion: dartVersion,
fs: fs,
platform: platform,
);

// Grab the first client ID
final String firstClientId = clientIdFile.readAsStringSync();
expect(firstClientId.isNotEmpty, true);

// Opting out here will reset the CLIENT ID file
await firstAnalytics.setTelemetry(false);
final String secondClientId = clientIdFile.readAsStringSync();

expect(firstClientId != secondClientId, true);
expect(firstAnalytics.telemetryEnabled, false);
});
}