Skip to content

Commit b9d97ee

Browse files
committed
review comments
1 parent 995a1a7 commit b9d97ee

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

pkgs/unified_analytics/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 7.0.0
22
- Added a required parameter `screen` to the `Event.devtoolsEvent` constructor.
33
- Added an optional parameter `additionalMetrics` to the `Event.devtoolsEvent` constructor.
4+
- Added `CustomMetrics` class for unified_analytics clients to define custom event metrics.
45
- Removed parameters `uiDurationMicros`, `rasterDurationMicros`, `shaderCompilationDurationMicros`,
56
`traceEventCount`, `cpuSampleCount`, `cpuStackDepth`, `heapDiffObjectsBefore`, `heapDiffObjectsAfter`,
67
`heapObjectsTotal`, `rootSetCount`, `rowCount`, `inspectorTreeControllerId`, `androidAppId`, `iosBundleId`

pkgs/unified_analytics/lib/src/event.dart

+18-4
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,10 @@ final class Event {
358358

359359
/// Event that is sent from DevTools for various different actions as
360360
/// indicated by the [eventCategory].
361-
///
361+
///
362362
/// The optional parameters in the parameter list contain metadata that is
363363
/// sent with each event, when available.
364-
///
364+
///
365365
/// [additionalMetrics] may contain any additional data for the event being
366366
/// sent. This often looks like metrics that are unique to the event or to a
367367
/// specific screen.
@@ -387,7 +387,7 @@ final class Event {
387387
String? isEmbedded,
388388
String? ideLaunchedFeature,
389389
String? isWasm,
390-
Map<String, Object?> additionalMetrics = const {},
390+
CustomMetrics? additionalMetrics,
391391
}) : eventName = DashEvent.devtoolsEvent,
392392
eventData = {
393393
'screen': screen,
@@ -411,7 +411,9 @@ final class Event {
411411
if (ideLaunchedFeature != null)
412412
'ideLaunchedFeature': ideLaunchedFeature,
413413
if (isWasm != null) 'isWasm': isWasm,
414-
...additionalMetrics..removeWhere((key, value) => value == null),
414+
if (additionalMetrics != null)
415+
...additionalMetrics.toMap()
416+
..removeWhere((key, value) => value == null),
415417
};
416418

417419
/// Event that contains the results for a specific doctor validator.
@@ -846,3 +848,15 @@ final class Event {
846848
}
847849
}
848850
}
851+
852+
/// A base class for custom metrics that will be defined by a unified_analytics
853+
/// client.
854+
///
855+
/// This base type can be used as a parameter in any event constructor that
856+
/// allows custom metrics to be added by a unified_analytics client.
857+
abstract class CustomMetrics {
858+
/// Converts the custom metrics data to a [Map] object.
859+
///
860+
/// This must be a JSON encodable [Map].
861+
Map<String, Object?> toMap();
862+
}

pkgs/unified_analytics/test/event_test.dart

+36-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:mirrors';
66

77
import 'package:test/test.dart';
88
import 'package:unified_analytics/src/enums.dart';
9+
import 'package:unified_analytics/src/event.dart';
910
import 'package:unified_analytics/unified_analytics.dart';
1011

1112
void main() {
@@ -576,11 +577,14 @@ void main() {
576577
isEmbedded: 'isEmbedded',
577578
ideLaunchedFeature: 'ideLaunchedFeature',
578579
isWasm: 'true',
579-
additionalMetrics: {
580-
'someMetric': 100,
581-
'otherMetric': false,
582-
'shouldBeRemoved': null,
583-
},
580+
additionalMetrics: _TestMetrics(
581+
stringField: 'test',
582+
// Since this value is null, it should not be included in the event
583+
// JSON below.
584+
nullableField: null,
585+
intField: 100,
586+
boolField: false,
587+
),
584588
);
585589

586590
final constructedEvent = generateEvent();
@@ -606,9 +610,11 @@ void main() {
606610
'ideLaunchedFeature',
607611
);
608612
expect(constructedEvent.eventData['isWasm'], 'true');
609-
expect(constructedEvent.eventData['someMetric'], 100);
610-
expect(constructedEvent.eventData['otherMetric'], false);
611-
expect(constructedEvent.eventData.length, 19);
613+
expect(constructedEvent.eventData['stringField'], 'test');
614+
expect(constructedEvent.eventData['intField'], 100);
615+
expect(constructedEvent.eventData['boolField'], false);
616+
expect(constructedEvent.eventData.containsKey('nullableField'), false);
617+
expect(constructedEvent.eventData.length, 20);
612618
});
613619

614620
test('Confirm all constructors were checked', () {
@@ -682,3 +688,25 @@ void main() {
682688
expect(eventConstructed, isNull);
683689
});
684690
}
691+
692+
class _TestMetrics extends CustomMetrics {
693+
_TestMetrics({
694+
required this.stringField,
695+
required this.nullableField,
696+
required this.intField,
697+
required this.boolField,
698+
});
699+
700+
final String stringField;
701+
final String? nullableField;
702+
final int intField;
703+
final bool boolField;
704+
705+
@override
706+
Map<String, Object?> toMap() => {
707+
'stringField': stringField,
708+
'nullableField': nullableField,
709+
'intField': intField,
710+
'boolField': boolField,
711+
};
712+
}

0 commit comments

Comments
 (0)