Skip to content

Commit 58d9147

Browse files
[unified_analytics] Add NoOpAnalytics (#77)
Fixes #76
1 parent cd799be commit 58d9147

File tree

4 files changed

+105
-12
lines changed

4 files changed

+105
-12
lines changed

pkgs/unified_analytics/lib/src/analytics.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,51 @@ class AnalyticsImpl implements Analytics {
462462
}
463463
}
464464

465+
/// An implementation that will never send events.
466+
///
467+
/// This is for clients that opt to either not send analytics, or will migrate
468+
/// to use [AnalyticsImpl] at a later time.
469+
class NoOpAnalytics implements Analytics {
470+
const NoOpAnalytics._();
471+
472+
factory NoOpAnalytics() => const NoOpAnalytics._();
473+
474+
@override
475+
final String getConsentMessage = '';
476+
477+
@override
478+
final Map<String, ToolInfo> parsedTools = const <String, ToolInfo>{};
479+
480+
@override
481+
final bool shouldShowMessage = false;
482+
483+
@override
484+
final bool telemetryEnabled = false;
485+
486+
@override
487+
final Map<String, Map<String, Object?>> userPropertyMap =
488+
const <String, Map<String, Object?>>{};
489+
490+
@override
491+
void clientShowedMessage() {}
492+
493+
@override
494+
void close() {}
495+
496+
@override
497+
LogFileStats? logFileStats() => null;
498+
499+
@override
500+
Future<Response>? sendEvent({
501+
required DashEvent eventName,
502+
Map<String, Object?> eventData = const {},
503+
}) =>
504+
null;
505+
506+
@override
507+
Future<void> setTelemetry(bool reportingBool) async {}
508+
}
509+
465510
/// This class extends [AnalyticsImpl] and subs out any methods that
466511
/// are not suitable for tests; the following have been altered from the
467512
/// default implementation. All other methods are included

pkgs/unified_analytics/lib/src/ga_client.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ class GAClient {
4747

4848
/// Receive the payload in Map form and parse
4949
/// into JSON to send to GA
50-
Future<http.Response> sendData(Map<String, Object?> body) {
51-
return _client
52-
.post(
53-
Uri.parse(postUrl),
54-
headers: <String, String>{
55-
'Content-Type': 'application/json; charset=UTF-8',
56-
},
57-
body: jsonEncode(body),
58-
)
59-
.catchError((error) {
50+
Future<http.Response> sendData(Map<String, Object?> body) async {
51+
try {
52+
return await _client.post(
53+
Uri.parse(postUrl),
54+
headers: <String, String>{
55+
'Content-Type': 'application/json; charset=UTF-8',
56+
},
57+
body: jsonEncode(body),
58+
);
59+
} catch (error) {
6060
return Future<http.Response>.value(http.Response(error.toString(), 500));
61-
});
61+
}
6262
}
6363
}

pkgs/unified_analytics/lib/unified_analytics.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
export 'src/analytics.dart' show Analytics;
5+
export 'src/analytics.dart' show Analytics, NoOpAnalytics;
6+
export 'src/config_handler.dart' show ToolInfo;
67
export 'src/enums.dart';
78
export 'src/log_handler.dart' show LogFileStats;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test/test.dart';
6+
import 'package:unified_analytics/unified_analytics.dart';
7+
8+
void main() {
9+
test('NoOpAnalytics.telemetryEnabled is always false', () async {
10+
final NoOpAnalytics analytics = NoOpAnalytics();
11+
12+
expect(analytics.telemetryEnabled, isFalse);
13+
await analytics.setTelemetry(true);
14+
expect(analytics.telemetryEnabled, isFalse);
15+
});
16+
17+
test('NoOpAnalytics.shouldShowMessage is always false', () async {
18+
final NoOpAnalytics analytics = NoOpAnalytics();
19+
20+
expect(analytics.shouldShowMessage, isFalse);
21+
analytics.clientShowedMessage();
22+
expect(analytics.shouldShowMessage, isFalse);
23+
});
24+
25+
test('NoOpAnalytics.sendEvent() always returns null', () async {
26+
final NoOpAnalytics analytics = NoOpAnalytics();
27+
28+
await analytics.setTelemetry(true);
29+
analytics.clientShowedMessage();
30+
expect(
31+
analytics.sendEvent(eventName: DashEvent.analyticsCollectionEnabled),
32+
isNull,
33+
);
34+
});
35+
36+
test('NoOpAnalytics.logFileStats() always returns null', () async {
37+
final NoOpAnalytics analytics = NoOpAnalytics();
38+
39+
expect(analytics.logFileStats(), isNull);
40+
41+
await analytics.setTelemetry(true);
42+
analytics.clientShowedMessage();
43+
await analytics.sendEvent(eventName: DashEvent.analyticsCollectionEnabled);
44+
45+
expect(analytics.logFileStats(), isNull);
46+
});
47+
}

0 commit comments

Comments
 (0)