Skip to content

Commit d3b54e8

Browse files
authored
[UA] Add ability to split out sub-events in the LogStatsFile (#2059)
1 parent 832c5b8 commit d3b54e8

File tree

7 files changed

+102
-4
lines changed

7 files changed

+102
-4
lines changed

pkgs/unified_analytics/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 7.0.2
2+
- Allow `LogStatsFile` to contain more granular event types (specifically `property_editor` events).
3+
14
## 7.0.1
25
- Fixed `UnsupportedError` thrown when Event.exception is called without providing a value for `args`.
36

pkgs/unified_analytics/USAGE_GUIDE.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,10 @@ Explanation of the each key above
199199
- flutterChannelCount: count of flutter channels (can be 0 if developer is a Dart dev only)
200200
- toolCount: count of the Dart and Flutter tools sending analytics
201201
- recordCount: count of the total number of events in the log file
202-
- eventCount: counts each unique event and how many times they occurred in the log file
202+
- eventCount: counts each unique event and how many times they occurred in the log file
203+
204+
Note: You can test any changes to the `LogFileStats` during development by running:
205+
206+
```shell
207+
dart run example/log_stats.dart
208+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2025, 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:unified_analytics/unified_analytics.dart';
6+
7+
final Analytics analytics = Analytics.development(
8+
tool: DashTool.flutterTool,
9+
flutterChannel: 'ey-test-channel',
10+
flutterVersion: 'Flutter 3.29.2',
11+
clientIde: 'VSCode',
12+
dartVersion: 'Dart 3.7.2',
13+
);
14+
15+
/// Simple CLI to print the logFileStats to the console.
16+
///
17+
/// Run with: dart run example/log_stats.dart
18+
void main() async {
19+
print(analytics.logFileStats());
20+
// Close the client connection on exit.
21+
await analytics.close();
22+
}

pkgs/unified_analytics/lib/src/constants.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const int kMaxLogFileSize = 25 * (1 << 20);
8787
const String kLogFileName = 'dart-flutter-telemetry.log';
8888

8989
/// The current version of the package, should be in line with pubspec version.
90-
const String kPackageVersion = '7.0.1';
90+
const String kPackageVersion = '7.0.2';
9191

9292
/// The minimum length for a session.
9393
const int kSessionDurationMinutes = 30;
@@ -117,3 +117,12 @@ Privacy Policy (https://policies.google.com/privacy).
117117
/// If the message below is altered, the version should be incremented so that
118118
/// users can be prompted with the updated messaging.
119119
const int kToolsMessageVersion = 1;
120+
121+
/// Constants for Dash tools
122+
123+
/// DevTools:
124+
const String devtoolsEventLabel = 'devtools_event';
125+
126+
const String propertyEditorId = 'propertyEditorSidebar';
127+
128+
const String propertyEditorLogStatsName = 'property_editor';

pkgs/unified_analytics/lib/src/log_handler.dart

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,10 @@ class LogItem {
397397
// a map for the one event in the value
398398
final eventProp =
399399
(record['events']! as List<Object?>).first as Map<String, Object?>;
400-
final eventName = eventProp['name'] as String;
400+
final eventName = _logItemEventName(
401+
eventProp['name'] as String,
402+
event: eventProp,
403+
);
401404

402405
// Parse the data out of the `user_properties` value
403406
final userProps = record['user_properties'] as Map<String, Object?>;
@@ -462,3 +465,28 @@ class LogItem {
462465
);
463466
}
464467
}
468+
469+
/// Logic for creating a more granular event name for the [LogItem].
470+
String _logItemEventName(
471+
String eventName, {
472+
required Map<String, Object?> event,
473+
}) {
474+
switch (eventName) {
475+
case devtoolsEventLabel:
476+
return _granularDevToolsEventName(eventName, event: event);
477+
default:
478+
return eventName;
479+
}
480+
}
481+
482+
String _granularDevToolsEventName(
483+
String eventName, {
484+
required Map<String, Object?> event,
485+
}) {
486+
final params = event['params'] as Map<String, Object?>;
487+
final screen = params['screen'];
488+
if (screen is String && screen == propertyEditorId) {
489+
return propertyEditorLogStatsName;
490+
}
491+
return eventName;
492+
}

pkgs/unified_analytics/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: >-
55
# LINT.IfChange
66
# When updating this, keep the version consistent with the changelog and the
77
# value in lib/src/constants.dart.
8-
version: 7.0.1
8+
version: 7.0.2
99
# LINT.ThenChange(lib/src/constants.dart)
1010
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics
1111
issue_tracker: https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Aunified_analytics

pkgs/unified_analytics/test/log_handler_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ void main() {
2222
late File logFile;
2323

2424
final testEvent = Event.hotReloadTime(timeMs: 10);
25+
final propertyEditorEvent = Event.devtoolsEvent(
26+
label: devtoolsEventLabel,
27+
screen: propertyEditorId,
28+
eventCategory: 'test',
29+
value: 1);
30+
final devToolsEvent = Event.devtoolsEvent(
31+
label: devtoolsEventLabel,
32+
screen: 'inspector',
33+
eventCategory: 'test',
34+
value: 1);
2535

2636
setUp(() {
2737
fs = MemoryFileSystem.test(style: FileSystemStyle.posix);
@@ -71,6 +81,26 @@ void main() {
7181
expect(analytics.logFileStats()!.recordCount, countOfEventsToSend);
7282
});
7383

84+
test('LogFileStats handles granular events', () async {
85+
final countOfPropertyEditorEventsToSend = 5;
86+
final countOfDevToolsEventsToSend = 10;
87+
88+
for (var i = 0; i < countOfPropertyEditorEventsToSend; i++) {
89+
analytics.send(propertyEditorEvent);
90+
}
91+
for (var i = 0; i < countOfDevToolsEventsToSend; i++) {
92+
analytics.send(devToolsEvent);
93+
}
94+
95+
expect(analytics.logFileStats(), isNotNull);
96+
expect(logFile.readAsLinesSync().length,
97+
countOfPropertyEditorEventsToSend + countOfDevToolsEventsToSend);
98+
expect(analytics.logFileStats()!.eventCount['property_editor'],
99+
countOfPropertyEditorEventsToSend);
100+
expect(analytics.logFileStats()!.eventCount['devtools_event'],
101+
countOfDevToolsEventsToSend);
102+
});
103+
74104
test('The only record in the log file is malformed', () async {
75105
// Write invalid json for the only log record
76106
logFile.writeAsStringSync('{{\n');

0 commit comments

Comments
 (0)