From a650cc658d61aadf516ddeb9d6ef4cda3ff2f293 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Thu, 25 Jan 2024 10:02:22 -0500 Subject: [PATCH 1/8] Adding new `_firstRun` --- pkgs/unified_analytics/lib/src/analytics.dart | 23 ++++++++++++++--- .../test/unified_analytics_test.dart | 9 +++++-- .../unified_analytics/test/workflow_test.dart | 25 +++++++++++++++++++ 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index 5d6df92b..1180ad6d 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -353,6 +353,9 @@ class AnalyticsImpl implements Analytics { /// Telemetry suppression flag that is set via [Analytics.suppressTelemetry]. bool _telemetrySuppressed = false; + /// Indicates if this is the first run for a given tool. + bool _firstRun = false; + /// The list of futures that will contain all of the send events /// from the [GAClient]. final _futures = >[]; @@ -388,7 +391,13 @@ class AnalyticsImpl implements Analytics { toolsMessageVersion: toolsMessageVersion, ); initializer.run(); - _showMessage = initializer.firstRun; + if (initializer.firstRun) { + _showMessage = true; + _firstRun = true; + } else { + _showMessage = false; + _firstRun = false; + } // Create the config handler that will parse the config file _configHandler = ConfigHandler( @@ -414,6 +423,11 @@ class AnalyticsImpl implements Analytics { _configHandler.parsedTools[tool.label]?.versionNumber ?? -1; if (currentVersion < toolsMessageVersion) { _showMessage = true; + + // If the message version has been updated, it will be considered + // as if it was a first run and any events attempting to get sent + // will be blocked + _firstRun = true; } _clientIdFile = fs.file( @@ -479,7 +493,8 @@ class AnalyticsImpl implements Analytics { telemetryEnabled && !_showMessage && _clientShowedMessage && - !_telemetrySuppressed; + !_telemetrySuppressed && + !_firstRun; @override Map get parsedTools => _configHandler.parsedTools; @@ -501,7 +516,7 @@ class AnalyticsImpl implements Analytics { tool: tool.label, versionNumber: toolsMessageVersion, ); - _showMessage = true; + _showMessage = false; } if (_configHandler.parsedTools[tool.label]!.versionNumber < toolsMessageVersion) { @@ -509,7 +524,7 @@ class AnalyticsImpl implements Analytics { tool: tool.label, newVersionNumber: toolsMessageVersion, ); - _showMessage = true; + _showMessage = false; } _clientShowedMessage = true; } diff --git a/pkgs/unified_analytics/test/unified_analytics_test.dart b/pkgs/unified_analytics/test/unified_analytics_test.dart index 659af513..22e08859 100644 --- a/pkgs/unified_analytics/test/unified_analytics_test.dart +++ b/pkgs/unified_analytics/test/unified_analytics_test.dart @@ -74,7 +74,9 @@ void main() { fs: fs, platform: platform, ); + expect(initializationAnalytics.shouldShowMessage, true); initializationAnalytics.clientShowedMessage(); + expect(initializationAnalytics.shouldShowMessage, false); // The main analytics instance, other instances can be spawned within tests // to test how to instances running together work @@ -143,8 +145,6 @@ void main() { expect(dartToolDirectory.listSync().length, equals(5), reason: 'There should only be 5 files in the $kDartToolDirectoryName directory'); - expect(initializationAnalytics.shouldShowMessage, true, - reason: 'For the first run, the message should be shown'); expect(configFile.readAsLinesSync().length, kConfigString.split('\n').length + 1, reason: 'The number of lines should equal lines in constant value + 1 ' @@ -430,7 +430,12 @@ void main() { fs: fs, platform: platform, ); + expect(secondAnalytics.shouldShowMessage, true); + expect(secondAnalytics.okToSend, false); secondAnalytics.clientShowedMessage(); + expect(secondAnalytics.shouldShowMessage, false); + expect(secondAnalytics.okToSend, false, + reason: 'New version for the message will be treated as a first run'); expect(secondAnalytics.parsedTools[initialTool.label]?.versionNumber, toolsMessageVersion + 1, diff --git a/pkgs/unified_analytics/test/workflow_test.dart b/pkgs/unified_analytics/test/workflow_test.dart index 844e3a21..184252d0 100644 --- a/pkgs/unified_analytics/test/workflow_test.dart +++ b/pkgs/unified_analytics/test/workflow_test.dart @@ -59,6 +59,31 @@ void main() { .childFile(kDismissedSurveyFileName); }); + test('Confirm workflow for first run', () { + final 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, + ); + + expect(firstAnalytics.shouldShowMessage, true); + expect(firstAnalytics.okToSend, false); + + firstAnalytics.clientShowedMessage(); + expect(firstAnalytics.shouldShowMessage, false); + expect(firstAnalytics.okToSend, false, + reason: 'On the first run, we should not be ok ' + 'to send any events, even if the user accepts'); + }); + test('Confirm workflow for checking tools into the config file', () { final firstAnalytics = Analytics.test( tool: initialTool, From 42a7b70169dceb5d86dc86f2da1fe5c1b00468c7 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Thu, 25 Jan 2024 10:04:01 -0500 Subject: [PATCH 2/8] Remove redundant `_clientShowedMessage` var --- pkgs/unified_analytics/lib/src/analytics.dart | 31 +++---------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index 1180ad6d..3b0b041b 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -333,19 +333,6 @@ class AnalyticsImpl implements Analytics { /// message has been updated by the package. late bool _showMessage; - /// This will be switch to true once it has been confirmed by the - /// client using this package that they have shown the - /// consent message to the developer. - /// - /// If the tool using this package as already shown the consent message - /// and it has been added to the config file, it will be set as true. - /// - /// It will also be set to true once the tool using this package has - /// invoked [clientShowedMessage]. - /// - /// If this is false, all events will be blocked from being sent. - bool _clientShowedMessage = false; - /// When set to `true`, various assert statements will be enabled /// to ensure usage of this class is within GA4 limitations. final bool _enableAsserts; @@ -406,13 +393,6 @@ class AnalyticsImpl implements Analytics { initializer: initializer, ); - // If the tool has already been added to the config file - // we can assume that the client has successfully shown - // the consent message - if (_configHandler.parsedTools.containsKey(tool.label)) { - _clientShowedMessage = true; - } - // Check if the tool has already been onboarded, and if it // has, check if the latest message version is greater to // prompt the client to show a message @@ -490,11 +470,7 @@ class AnalyticsImpl implements Analytics { /// return `true` to prevent events from being sent for current invocation. @override bool get okToSend => - telemetryEnabled && - !_showMessage && - _clientShowedMessage && - !_telemetrySuppressed && - !_firstRun; + telemetryEnabled && !_showMessage && !_telemetrySuppressed && !_firstRun; @override Map get parsedTools => _configHandler.parsedTools; @@ -511,6 +487,7 @@ class AnalyticsImpl implements Analytics { @override void clientShowedMessage() { + // Check the tool needs to be added to the config file if (!_configHandler.parsedTools.containsKey(tool.label)) { _configHandler.addTool( tool: tool.label, @@ -518,6 +495,9 @@ class AnalyticsImpl implements Analytics { ); _showMessage = false; } + + // When the tool already exists but the consent message version + // has been updated if (_configHandler.parsedTools[tool.label]!.versionNumber < toolsMessageVersion) { _configHandler.incrementToolVersion( @@ -526,7 +506,6 @@ class AnalyticsImpl implements Analytics { ); _showMessage = false; } - _clientShowedMessage = true; } @override From a01e24175433b1d9d356f0acf2a87cb7c9e358af Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Thu, 25 Jan 2024 10:21:08 -0500 Subject: [PATCH 3/8] Enhance test to add incrementing + new tool --- .../unified_analytics/test/workflow_test.dart | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/pkgs/unified_analytics/test/workflow_test.dart b/pkgs/unified_analytics/test/workflow_test.dart index 184252d0..e38c4ac2 100644 --- a/pkgs/unified_analytics/test/workflow_test.dart +++ b/pkgs/unified_analytics/test/workflow_test.dart @@ -84,6 +84,74 @@ void main() { 'to send any events, even if the user accepts'); }); + test('Confirm workflow for updated tools message version', () { + // Helper function to check the state of the instance + void checkAnalyticsInstance(Analytics instance) { + expect(instance.shouldShowMessage, true); + expect(instance.okToSend, false); + + instance.clientShowedMessage(); + expect(instance.shouldShowMessage, false); + expect(instance.okToSend, false, + reason: 'On the first run, we should not be ok ' + 'to send any events, even if the user accepts'); + } + + final 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, + ); + + checkAnalyticsInstance(firstAnalytics); + + // Instance where we increment the version of the message + final secondAnalytics = Analytics.test( + tool: initialTool, + homeDirectory: home, + measurementId: measurementId, + apiSecret: apiSecret, + flutterChannel: flutterChannel, + toolsMessageVersion: toolsMessageVersion + 1, // Incrementing version + toolsMessage: toolsMessage, + flutterVersion: flutterVersion, + dartVersion: dartVersion, + fs: fs, + platform: platform, + ); + + // Running the same checks for the second instance, it should + // behave the same as if it was a first run + checkAnalyticsInstance(secondAnalytics); + + // Instance for a different tool with the incremented version + final thirdAnalytics = Analytics.test( + tool: secondTool, // Different tool + homeDirectory: home, + measurementId: measurementId, + apiSecret: apiSecret, + flutterChannel: flutterChannel, + toolsMessageVersion: toolsMessageVersion + 1, // Incrementing version + toolsMessage: toolsMessage, + flutterVersion: flutterVersion, + dartVersion: dartVersion, + fs: fs, + platform: platform, + ); + + // The instance with a new tool getting onboarded should be + // treated the same as the 2 previous instances + checkAnalyticsInstance(thirdAnalytics); + }); + test('Confirm workflow for checking tools into the config file', () { final firstAnalytics = Analytics.test( tool: initialTool, From 350c59fdfba5321ca39521279ca450de493a6a2a Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Thu, 25 Jan 2024 10:25:05 -0500 Subject: [PATCH 4/8] Update version --- pkgs/unified_analytics/CHANGELOG.md | 4 ++++ pkgs/unified_analytics/lib/src/constants.dart | 2 +- pkgs/unified_analytics/pubspec.yaml | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pkgs/unified_analytics/CHANGELOG.md b/pkgs/unified_analytics/CHANGELOG.md index aba5c821..3cebc128 100644 --- a/pkgs/unified_analytics/CHANGELOG.md +++ b/pkgs/unified_analytics/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.8.1 + +- Refactor logic for `okToSend` and `shouldShowMessage` + ## 5.8.0 - Fix template string for consent message diff --git a/pkgs/unified_analytics/lib/src/constants.dart b/pkgs/unified_analytics/lib/src/constants.dart index 24639343..48606f03 100644 --- a/pkgs/unified_analytics/lib/src/constants.dart +++ b/pkgs/unified_analytics/lib/src/constants.dart @@ -82,7 +82,7 @@ const int kLogFileLength = 2500; const String kLogFileName = 'dart-flutter-telemetry.log'; /// The current version of the package, should be in line with pubspec version. -const String kPackageVersion = '5.8.0'; +const String kPackageVersion = '5.8.1'; /// The minimum length for a session. const int kSessionDurationMinutes = 30; diff --git a/pkgs/unified_analytics/pubspec.yaml b/pkgs/unified_analytics/pubspec.yaml index 85ce4a59..ef8808f6 100644 --- a/pkgs/unified_analytics/pubspec.yaml +++ b/pkgs/unified_analytics/pubspec.yaml @@ -4,7 +4,7 @@ description: >- to Google Analytics. # When updating this, keep the version consistent with the changelog and the # value in lib/src/constants.dart. -version: 5.8.0 +version: 5.8.1 repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics environment: From d0245fb28d549dc794731b17ec4ef091a2fa5bd7 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:44:26 -0500 Subject: [PATCH 5/8] Clean up dartdoc --- pkgs/unified_analytics/lib/src/analytics.dart | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index 3b0b041b..164500e3 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -458,16 +458,16 @@ class AnalyticsImpl implements Analytics { /// Checking the [telemetryEnabled] boolean reflects what the /// config file reflects. /// - /// Checking the [_showMessage] boolean indicates if this the first - /// time the tool is using analytics or if there has been an update - /// the messaging found in constants.dart - in both cases, analytics - /// will not be sent until the second time the tool is used. - /// - /// Additionally, if the client has not invoked - /// [Analytics.clientShowedMessage], then no events shall be sent. + /// Checking the [_showMessage] boolean indicates if the consent + /// message has been shown for the user, this boolean is set to `true` + /// when the tool using this package invokes the [clientShowedMessage] + /// method. /// /// If the user has suppressed telemetry [_telemetrySuppressed] will /// return `true` to prevent events from being sent for current invocation. + /// + /// Checking if it is the first time a tool is running with this package + /// as indicated by [_firstRun]. @override bool get okToSend => telemetryEnabled && !_showMessage && !_telemetrySuppressed && !_firstRun; From ec8654fa29e152f4795e9c66a72b70971bf21c92 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Thu, 25 Jan 2024 17:03:19 -0500 Subject: [PATCH 6/8] Update workflow_test.dart --- pkgs/unified_analytics/test/workflow_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/unified_analytics/test/workflow_test.dart b/pkgs/unified_analytics/test/workflow_test.dart index e38c4ac2..45ca62c3 100644 --- a/pkgs/unified_analytics/test/workflow_test.dart +++ b/pkgs/unified_analytics/test/workflow_test.dart @@ -84,7 +84,7 @@ void main() { 'to send any events, even if the user accepts'); }); - test('Confirm workflow for updated tools message version', () { + test('Confirm workflow for updated tools message version + new tool', () { // Helper function to check the state of the instance void checkAnalyticsInstance(Analytics instance) { expect(instance.shouldShowMessage, true); From 60296fdd0068bb522724242803d9405c6675dcfd Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Mon, 29 Jan 2024 12:22:39 -0500 Subject: [PATCH 7/8] Set private _showMessage in method scope --- pkgs/unified_analytics/lib/src/analytics.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index 164500e3..56ea0859 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -493,7 +493,6 @@ class AnalyticsImpl implements Analytics { tool: tool.label, versionNumber: toolsMessageVersion, ); - _showMessage = false; } // When the tool already exists but the consent message version @@ -504,8 +503,8 @@ class AnalyticsImpl implements Analytics { tool: tool.label, newVersionNumber: toolsMessageVersion, ); - _showMessage = false; } + _showMessage = false; } @override From ab9ff60e88452025e7cf382cacd9c1cd8cd2efc0 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Mon, 29 Jan 2024 12:23:38 -0500 Subject: [PATCH 8/8] Format fix --- pkgs/unified_analytics/lib/src/analytics.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index 56ea0859..90fefb5e 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -504,7 +504,7 @@ class AnalyticsImpl implements Analytics { newVersionNumber: toolsMessageVersion, ); } - _showMessage = false; + _showMessage = false; } @override