From 7f154cc77cef8aad577bae0eb428f551562474a1 Mon Sep 17 00:00:00 2001 From: Janice Collins Date: Thu, 13 Apr 2023 14:43:01 -0700 Subject: [PATCH 1/4] initial implementation --- pkgs/unified_analytics/lib/src/analytics.dart | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index f17f4cf1d..29e25f2e8 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -206,6 +206,11 @@ abstract class Analytics { /// run or when the message has been updated String get getConsentMessage; + /// Returns true if it is OK to send an analytics message. Do not cache, + /// as this depends on factors that can change, such as the configuration + /// file contents. + bool get okToSend; + /// Returns a map object with all of the tools that have been parsed /// out of the configuration file Map get parsedTools; @@ -374,6 +379,19 @@ class AnalyticsImpl implements Analytics { String get getConsentMessage => kToolsMessage.replaceAll('[tool name]', tool.description); + @override + /// 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 `clientShowedMessage`, + /// then no events shall be sent. + bool get okToSend => telemetryEnabled && !_showMessage && _clientShowedMessage; + @override Map get parsedTools => _configHandler.parsedTools; @@ -418,17 +436,7 @@ class AnalyticsImpl implements Analytics { required DashEvent eventName, Map eventData = const {}, }) { - // 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 `clientShowedMessage`, - // then no events shall be sent - if (!telemetryEnabled || _showMessage || !_clientShowedMessage) return null; + if (!okToSend) return null; // Construct the body of the request final Map body = generateRequestBody( @@ -479,6 +487,9 @@ class NoOpAnalytics implements Analytics { @override final String getConsentMessage = ''; + @override + final bool okToSend = false; + @override final Map parsedTools = const {}; @@ -536,7 +547,7 @@ class TestAnalytics extends AnalyticsImpl { required DashEvent eventName, Map eventData = const {}, }) { - if (!telemetryEnabled || _showMessage || !_clientShowedMessage) return null; + if (!okToSend) return null; // Calling the [generateRequestBody] method will ensure that the // session file is getting updated without actually making any From 374bbf71675879d956ffaf965917a485b9a0d89c Mon Sep 17 00:00:00 2001 From: Janice Collins Date: Thu, 13 Apr 2023 14:45:57 -0700 Subject: [PATCH 2/4] dart format --- pkgs/unified_analytics/lib/src/analytics.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index 29e25f2e8..c7b08ce9b 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -379,7 +379,6 @@ class AnalyticsImpl implements Analytics { String get getConsentMessage => kToolsMessage.replaceAll('[tool name]', tool.description); - @override /// Checking the [telemetryEnabled] boolean reflects what the /// config file reflects /// @@ -390,7 +389,9 @@ class AnalyticsImpl implements Analytics { /// /// Additionally, if the client has not invoked `clientShowedMessage`, /// then no events shall be sent. - bool get okToSend => telemetryEnabled && !_showMessage && _clientShowedMessage; + @override + bool get okToSend => + telemetryEnabled && !_showMessage && _clientShowedMessage; @override Map get parsedTools => _configHandler.parsedTools; From 81c427ae443ae68cb1de63d93eb2f1fe7870f3f8 Mon Sep 17 00:00:00 2001 From: Janice Collins Date: Thu, 13 Apr 2023 14:55:12 -0700 Subject: [PATCH 3/4] pubspec --- pkgs/unified_analytics/CHANGELOG.md | 4 ++++ pkgs/unified_analytics/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkgs/unified_analytics/CHANGELOG.md b/pkgs/unified_analytics/CHANGELOG.md index 001a4605d..43aac7df3 100644 --- a/pkgs/unified_analytics/CHANGELOG.md +++ b/pkgs/unified_analytics/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.1.0 + +- Added a `okToSend` getter so that clients can easily and accurately check the state of the consent mechanism. + ## 1.0.1 - Error handling on the `analytics.sendEvent(...)` method to silently error out and return a `500` http status code to let tools using this package know Google Analytics did not receive the event (all successful requests will have a status code of `2xx` provided by Google Analytics) diff --git a/pkgs/unified_analytics/pubspec.yaml b/pkgs/unified_analytics/pubspec.yaml index 1d1dab08e..08de497fa 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: 1.0.1 +version: 1.1.0 repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics environment: From 006ef7949d67319be893ba0d1249e9d583b1335b Mon Sep 17 00:00:00 2001 From: Janice Collins Date: Thu, 13 Apr 2023 14:56:58 -0700 Subject: [PATCH 4/4] constants.dart, oops --- pkgs/unified_analytics/lib/src/constants.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/unified_analytics/lib/src/constants.dart b/pkgs/unified_analytics/lib/src/constants.dart index 5a0041289..fecd3ad00 100644 --- a/pkgs/unified_analytics/lib/src/constants.dart +++ b/pkgs/unified_analytics/lib/src/constants.dart @@ -70,7 +70,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 = '1.0.1'; +const String kPackageVersion = '1.1.0'; /// The minimum length for a session const int kSessionDurationMinutes = 30;