Skip to content

Commit 2308c67

Browse files
authored
Add a okToSend getter to the Analytics class (#79)
1 parent 924e9ed commit 2308c67

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
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+
## 1.1.0
2+
3+
- Added a `okToSend` getter so that clients can easily and accurately check the state of the consent mechanism.
4+
15
## 1.0.1
26

37
- 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)

pkgs/unified_analytics/lib/src/analytics.dart

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ abstract class Analytics {
206206
/// run or when the message has been updated
207207
String get getConsentMessage;
208208

209+
/// Returns true if it is OK to send an analytics message. Do not cache,
210+
/// as this depends on factors that can change, such as the configuration
211+
/// file contents.
212+
bool get okToSend;
213+
209214
/// Returns a map object with all of the tools that have been parsed
210215
/// out of the configuration file
211216
Map<String, ToolInfo> get parsedTools;
@@ -374,6 +379,20 @@ class AnalyticsImpl implements Analytics {
374379
String get getConsentMessage =>
375380
kToolsMessage.replaceAll('[tool name]', tool.description);
376381

382+
/// Checking the [telemetryEnabled] boolean reflects what the
383+
/// config file reflects
384+
///
385+
/// Checking the [_showMessage] boolean indicates if this the first
386+
/// time the tool is using analytics or if there has been an update
387+
/// the messaging found in constants.dart - in both cases, analytics
388+
/// will not be sent until the second time the tool is used
389+
///
390+
/// Additionally, if the client has not invoked `clientShowedMessage`,
391+
/// then no events shall be sent.
392+
@override
393+
bool get okToSend =>
394+
telemetryEnabled && !_showMessage && _clientShowedMessage;
395+
377396
@override
378397
Map<String, ToolInfo> get parsedTools => _configHandler.parsedTools;
379398

@@ -418,17 +437,7 @@ class AnalyticsImpl implements Analytics {
418437
required DashEvent eventName,
419438
Map<String, Object?> eventData = const {},
420439
}) {
421-
// Checking the [telemetryEnabled] boolean reflects what the
422-
// config file reflects
423-
//
424-
// Checking the [_showMessage] boolean indicates if this the first
425-
// time the tool is using analytics or if there has been an update
426-
// the messaging found in constants.dart - in both cases, analytics
427-
// will not be sent until the second time the tool is used
428-
//
429-
// Additionally, if the client has not invoked `clientShowedMessage`,
430-
// then no events shall be sent
431-
if (!telemetryEnabled || _showMessage || !_clientShowedMessage) return null;
440+
if (!okToSend) return null;
432441

433442
// Construct the body of the request
434443
final Map<String, Object?> body = generateRequestBody(
@@ -479,6 +488,9 @@ class NoOpAnalytics implements Analytics {
479488
@override
480489
final String getConsentMessage = '';
481490

491+
@override
492+
final bool okToSend = false;
493+
482494
@override
483495
final Map<String, ToolInfo> parsedTools = const <String, ToolInfo>{};
484496

@@ -536,7 +548,7 @@ class TestAnalytics extends AnalyticsImpl {
536548
required DashEvent eventName,
537549
Map<String, Object?> eventData = const {},
538550
}) {
539-
if (!telemetryEnabled || _showMessage || !_clientShowedMessage) return null;
551+
if (!okToSend) return null;
540552

541553
// Calling the [generateRequestBody] method will ensure that the
542554
// session file is getting updated without actually making any

pkgs/unified_analytics/lib/src/constants.dart

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

7272
/// The current version of the package, should be in line with pubspec version.
73-
const String kPackageVersion = '1.0.1';
73+
const String kPackageVersion = '1.1.0';
7474

7575
/// The minimum length for a session
7676
const int kSessionDurationMinutes = 30;

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: 1.0.1
7+
version: 1.1.0
88
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics
99

1010
environment:

0 commit comments

Comments
 (0)