From 7aea4342c1a33570ca477f19985b5e8de95c956a Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Mon, 24 Apr 2023 09:08:26 -0400 Subject: [PATCH 1/2] Update `setTelemetry` to delete `CLIENT_ID` on opt out --- pkgs/unified_analytics/lib/src/analytics.dart | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index da6786158e..fb5033963f 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -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; @@ -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 @@ -464,6 +464,12 @@ class AnalyticsImpl implements Analytics { Future setTelemetry(bool reportingBool) { _configHandler.setTelemetry(reportingBool); + // If telemetry is being set to false, then delete the + // CLIENT_ID file as suggested in internal reviews + if (!reportingBool && _clientIdFile.existsSync()) { + _clientIdFile.deleteSync(); + } + // Construct the body of the request to signal // telemetry status toggling // From d4e742d6c8192d43ede59c75dd968d3d957f5272 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Mon, 24 Apr 2023 13:52:42 -0400 Subject: [PATCH 2/2] Refactoring to refresh the CLIENT_ID on opt out instead of deleting --- pkgs/unified_analytics/lib/src/analytics.dart | 7 ++--- .../lib/src/initializer.dart | 2 +- .../unified_analytics/test/workflow_test.dart | 27 +++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index fb5033963f..2ddaf9eb70 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -464,10 +464,11 @@ class AnalyticsImpl implements Analytics { Future setTelemetry(bool reportingBool) { _configHandler.setTelemetry(reportingBool); - // If telemetry is being set to false, then delete the - // CLIENT_ID file as suggested in internal reviews + // 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()) { - _clientIdFile.deleteSync(); + Initializer.createClientIdFile(clientFile: _clientIdFile); } // Construct the body of the request to signal diff --git a/pkgs/unified_analytics/lib/src/initializer.dart b/pkgs/unified_analytics/lib/src/initializer.dart index 82faec07c2..2bcb8cec1e 100644 --- a/pkgs/unified_analytics/lib/src/initializer.dart +++ b/pkgs/unified_analytics/lib/src/initializer.dart @@ -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()); } diff --git a/pkgs/unified_analytics/test/workflow_test.dart b/pkgs/unified_analytics/test/workflow_test.dart index ca21f47105..c870badc07 100644 --- a/pkgs/unified_analytics/test/workflow_test.dart +++ b/pkgs/unified_analytics/test/workflow_test.dart @@ -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); + }); }