Skip to content

Add a okToSend getter to the Analytics class #79

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

Merged
merged 4 commits into from
Apr 14, 2023
Merged
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
4 changes: 4 additions & 0 deletions pkgs/unified_analytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
36 changes: 24 additions & 12 deletions pkgs/unified_analytics/lib/src/analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ToolInfo> get parsedTools;
Expand Down Expand Up @@ -374,6 +379,20 @@ class AnalyticsImpl implements Analytics {
String get getConsentMessage =>
kToolsMessage.replaceAll('[tool name]', tool.description);

/// 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.
@override
bool get okToSend =>
telemetryEnabled && !_showMessage && _clientShowedMessage;

@override
Map<String, ToolInfo> get parsedTools => _configHandler.parsedTools;

Expand Down Expand Up @@ -418,17 +437,7 @@ class AnalyticsImpl implements Analytics {
required DashEvent eventName,
Map<String, Object?> 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<String, Object?> body = generateRequestBody(
Expand Down Expand Up @@ -479,6 +488,9 @@ class NoOpAnalytics implements Analytics {
@override
final String getConsentMessage = '';

@override
final bool okToSend = false;

@override
final Map<String, ToolInfo> parsedTools = const <String, ToolInfo>{};

Expand Down Expand Up @@ -536,7 +548,7 @@ class TestAnalytics extends AnalyticsImpl {
required DashEvent eventName,
Map<String, Object?> 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
Expand Down
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down