Send screen and view analytics for VM Tools and DTD Tools (#10095)
diff --git a/packages/devtools_app/lib/src/screens/dtd/dtd_tools_screen.dart b/packages/devtools_app/lib/src/screens/dtd/dtd_tools_screen.dart
index cf74e65..000fbed 100644
--- a/packages/devtools_app/lib/src/screens/dtd/dtd_tools_screen.dart
+++ b/packages/devtools_app/lib/src/screens/dtd/dtd_tools_screen.dart
@@ -9,6 +9,8 @@
 import 'package:dtd/dtd.dart';
 import 'package:material_ui/material_ui.dart';
 
+import '../../shared/analytics/analytics.dart' as ga;
+import '../../shared/analytics/constants.dart' as gac;
 import '../../shared/development_helpers.dart';
 import '../../shared/framework/screen.dart';
 import '../../shared/globals.dart';
@@ -31,10 +33,21 @@
   Widget buildScreenBody(BuildContext _) => const DTDToolsScreenBody();
 }
 
-class DTDToolsScreenBody extends StatelessWidget {
+class DTDToolsScreenBody extends StatefulWidget {
   const DTDToolsScreenBody({super.key});
 
   @override
+  State<DTDToolsScreenBody> createState() => _DTDToolsScreenBodyState();
+}
+
+class _DTDToolsScreenBodyState extends State<DTDToolsScreenBody> {
+  @override
+  void initState() {
+    super.initState();
+    ga.screen(gac.dtdTools);
+  }
+
+  @override
   Widget build(BuildContext context) {
     final controller = screenControllers.lookup<DTDToolsController>();
     return ValueListenableBuilder(
diff --git a/packages/devtools_app/lib/src/screens/vm_developer/isolate_statistics/isolate_statistics_view.dart b/packages/devtools_app/lib/src/screens/vm_developer/isolate_statistics/isolate_statistics_view.dart
index dd59a5f..fe24652 100644
--- a/packages/devtools_app/lib/src/screens/vm_developer/isolate_statistics/isolate_statistics_view.dart
+++ b/packages/devtools_app/lib/src/screens/vm_developer/isolate_statistics/isolate_statistics_view.dart
@@ -24,7 +24,11 @@
 /// isolate.
 class IsolateStatisticsView extends VMDeveloperView {
   const IsolateStatisticsView()
-    : super(title: 'Isolates', icon: Icons.bar_chart);
+    : super(
+        title: 'Isolates',
+        icon: Icons.bar_chart,
+        gaEvent: gac.VmToolsEvents.isolateStatistics,
+      );
 
   @override
   bool get showIsolateSelector => true;
diff --git a/packages/devtools_app/lib/src/screens/vm_developer/object_inspector/object_inspector_view.dart b/packages/devtools_app/lib/src/screens/vm_developer/object_inspector/object_inspector_view.dart
index b4711b5..d321d3b 100644
--- a/packages/devtools_app/lib/src/screens/vm_developer/object_inspector/object_inspector_view.dart
+++ b/packages/devtools_app/lib/src/screens/vm_developer/object_inspector/object_inspector_view.dart
@@ -23,7 +23,11 @@
 /// information about objects in the Dart VM.
 class ObjectInspectorView extends VMDeveloperView {
   ObjectInspectorView()
-    : super(title: 'Objects', icon: Icons.data_object_outlined);
+    : super(
+        title: 'Objects',
+        icon: Icons.data_object_outlined,
+        gaEvent: gac.VmToolsEvents.objectInspector,
+      );
 
   @override
   bool get showIsolateSelector => true;
diff --git a/packages/devtools_app/lib/src/screens/vm_developer/process_memory/process_memory_view.dart b/packages/devtools_app/lib/src/screens/vm_developer/process_memory/process_memory_view.dart
index d970490..bfa11ae 100644
--- a/packages/devtools_app/lib/src/screens/vm_developer/process_memory/process_memory_view.dart
+++ b/packages/devtools_app/lib/src/screens/vm_developer/process_memory/process_memory_view.dart
@@ -31,7 +31,11 @@
 /// process's memory footprint.
 class VMProcessMemoryView extends VMDeveloperView {
   const VMProcessMemoryView()
-    : super(title: 'Process Memory', icon: Icons.memory);
+    : super(
+        title: 'Process Memory',
+        icon: Icons.memory,
+        gaEvent: gac.VmToolsEvents.processMemory,
+      );
 
   @override
   Widget build(BuildContext context) => VMProcessMemoryViewBody();
diff --git a/packages/devtools_app/lib/src/screens/vm_developer/queued_microtasks/queued_microtasks_view.dart b/packages/devtools_app/lib/src/screens/vm_developer/queued_microtasks/queued_microtasks_view.dart
index 516b349..19b3873 100644
--- a/packages/devtools_app/lib/src/screens/vm_developer/queued_microtasks/queued_microtasks_view.dart
+++ b/packages/devtools_app/lib/src/screens/vm_developer/queued_microtasks/queued_microtasks_view.dart
@@ -161,7 +161,11 @@
 
 class QueuedMicrotasksView extends VMDeveloperView {
   const QueuedMicrotasksView()
-    : super(title: 'Queued Microtasks', icon: Icons.pending_actions);
+    : super(
+        title: 'Queued Microtasks',
+        icon: Icons.pending_actions,
+        gaEvent: gac.VmToolsEvents.queuedMicrotasks,
+      );
 
   @override
   bool get showIsolateSelector => true;
diff --git a/packages/devtools_app/lib/src/screens/vm_developer/vm_developer_tools_screen.dart b/packages/devtools_app/lib/src/screens/vm_developer/vm_developer_tools_screen.dart
index ded2471..d4aa370 100644
--- a/packages/devtools_app/lib/src/screens/vm_developer/vm_developer_tools_screen.dart
+++ b/packages/devtools_app/lib/src/screens/vm_developer/vm_developer_tools_screen.dart
@@ -7,6 +7,8 @@
 import 'package:material_ui/material_ui.dart';
 
 import '../../service/vm_flags.dart' as vm_flags;
+import '../../shared/analytics/analytics.dart' as ga;
+import '../../shared/analytics/constants.dart' as gac;
 import '../../shared/framework/screen.dart';
 import '../../shared/globals.dart';
 import 'isolate_statistics/isolate_statistics_view.dart';
@@ -17,13 +19,20 @@
 import 'vm_statistics/vm_statistics_view.dart';
 
 abstract class VMDeveloperView {
-  const VMDeveloperView({required this.title, required this.icon});
+  const VMDeveloperView({
+    required this.title,
+    required this.icon,
+    required this.gaEvent,
+  });
 
   /// The user-facing name of the page.
   final String title;
 
   final IconData icon;
 
+  /// The analytics id sent when this page is selected.
+  final gac.VmToolsEvents gaEvent;
+
   /// Whether this view should display the isolate selector in the status
   /// line.
   ///
@@ -46,7 +55,7 @@
       const VMDeveloperToolsScreenBody();
 }
 
-class VMDeveloperToolsScreenBody extends StatelessWidget {
+class VMDeveloperToolsScreenBody extends StatefulWidget {
   const VMDeveloperToolsScreenBody({super.key});
 
   // The value of the `--profile-microtasks` VM flag cannot be modified once
@@ -67,8 +76,39 @@
   ];
 
   @override
+  State<VMDeveloperToolsScreenBody> createState() =>
+      _VMDeveloperToolsScreenBodyState();
+}
+
+class _VMDeveloperToolsScreenBodyState
+    extends State<VMDeveloperToolsScreenBody> {
+  late final VMDeveloperToolsController controller;
+
+  List<VMDeveloperView> get views => VMDeveloperToolsScreenBody.views;
+
+  @override
+  void initState() {
+    super.initState();
+    ga.screen(gac.vmTools);
+    controller = screenControllers.lookup<VMDeveloperToolsController>();
+    // Record the view that is visible when the screen opens, as tabbed views
+    // do (see `AnalyticsTabbedView`).
+    ga.select(
+      gac.vmTools,
+      views[controller.selectedIndex.value].gaEvent.name,
+      nonInteraction: true,
+    );
+  }
+
+  void _selectView(int index) {
+    if (index != controller.selectedIndex.value) {
+      ga.select(gac.vmTools, views[index].gaEvent.name);
+    }
+    controller.selectIndex(index);
+  }
+
+  @override
   Widget build(BuildContext context) {
-    final controller = screenControllers.lookup<VMDeveloperToolsController>();
     return ValueListenableBuilder<int>(
       valueListenable: controller.selectedIndex,
       builder: (context, selectedIndex, _) {
@@ -78,7 +118,7 @@
               NavigationRail(
                 selectedIndex: selectedIndex,
                 labelType: NavigationRailLabelType.all,
-                onDestinationSelected: controller.selectIndex,
+                onDestinationSelected: _selectView,
                 destinations: [
                   for (final view in views)
                     NavigationRailDestination(
diff --git a/packages/devtools_app/lib/src/screens/vm_developer/vm_statistics/vm_statistics_view.dart b/packages/devtools_app/lib/src/screens/vm_developer/vm_statistics/vm_statistics_view.dart
index c25fd1c..129fae6 100644
--- a/packages/devtools_app/lib/src/screens/vm_developer/vm_statistics/vm_statistics_view.dart
+++ b/packages/devtools_app/lib/src/screens/vm_developer/vm_statistics/vm_statistics_view.dart
@@ -18,7 +18,12 @@
 import 'vm_statistics_view_controller.dart';
 
 class VMStatisticsView extends VMDeveloperView {
-  const VMStatisticsView() : super(title: 'VM', icon: Icons.devices);
+  const VMStatisticsView()
+    : super(
+        title: 'VM',
+        icon: Icons.devices,
+        gaEvent: gac.VmToolsEvents.vmStatistics,
+      );
 
   @override
   Widget build(BuildContext context) => VMStatisticsViewBody();
diff --git a/packages/devtools_app/lib/src/shared/analytics/constants.dart b/packages/devtools_app/lib/src/shared/analytics/constants.dart
index a19a5f1..dba48e5 100644
--- a/packages/devtools_app/lib/src/shared/analytics/constants.dart
+++ b/packages/devtools_app/lib/src/shared/analytics/constants.dart
@@ -20,6 +20,7 @@
 part 'constants/_network_constants.dart';
 part 'constants/_performance_constants.dart';
 part 'constants/_property_editor_sidebar_constants.dart';
+part 'constants/_vm_tools_constants.dart';
 
 // Type of events (event_category):
 const screenViewEvent = 'screen'; // Active screen (tab selected).
@@ -41,6 +42,7 @@
 final logging = ScreenMetaData.logging.id;
 final appSize = ScreenMetaData.appSize.id;
 final vmTools = ScreenMetaData.vmTools.id;
+final dtdTools = ScreenMetaData.dtdTools.id;
 const console = 'console';
 final simple = ScreenMetaData.simple.id;
 final deeplink = ScreenMetaData.deepLinks.id;
diff --git a/packages/devtools_app/lib/src/shared/analytics/constants/_vm_tools_constants.dart b/packages/devtools_app/lib/src/shared/analytics/constants/_vm_tools_constants.dart
new file mode 100644
index 0000000..9dabc7d
--- /dev/null
+++ b/packages/devtools_app/lib/src/shared/analytics/constants/_vm_tools_constants.dart
@@ -0,0 +1,14 @@
+// Copyright 2026 The Flutter Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
+
+part of '../constants.dart';
+
+/// Analytics ids for the views in the VM Tools screen's navigation rail.
+enum VmToolsEvents {
+  vmStatistics,
+  isolateStatistics,
+  objectInspector,
+  processMemory,
+  queuedMicrotasks,
+}