Skip to content

Commit

Permalink
Move analytics factories to constructors (#8399)
Browse files Browse the repository at this point in the history
Fix an old TODO RE fixed dart-lang/sdk#46967
  • Loading branch information
kevmoo authored Oct 3, 2024
1 parent f5e84f9 commit 77eae8f
Showing 1 changed file with 155 additions and 157 deletions.
312 changes: 155 additions & 157 deletions packages/devtools_app/lib/src/shared/analytics/_analytics_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,84 @@ extension type GtagEventDevTools._(JSObject _) implements GtagEvent {
String? ios_bundle_id, //metric14
});

factory GtagEventDevTools._create({
required String event_category,
required String event_label,
String? send_to,
bool non_interaction = false,
int value = 0,
ScreenAnalyticsMetrics? screenMetrics,
}) {
return GtagEventDevTools(
event_category: event_category,
event_label: event_label,
send_to: send_to,
non_interaction: non_interaction,
value: value,
user_app: userAppType,
user_build: userBuildType,
user_platform: userPlatformType,
devtools_platform: devtoolsPlatformType,
devtools_chrome: devtoolsChrome,
devtools_version: devtoolsVersion,
ide_launched: ideLaunched,
flutter_client_id: flutterClientId,
is_external_build: isExternalBuild.toString(),
is_embedded: isEmbedded().toString(),
g3_username: devToolsEnvironmentParameters.username(),
ide_launched_feature: ideLaunchedFeature,
is_wasm: kIsWasm.toString(),
// [PerformanceScreenMetrics]
ui_duration_micros: screenMetrics is PerformanceScreenMetrics
? screenMetrics.uiDuration?.inMicroseconds
: null,
raster_duration_micros: screenMetrics is PerformanceScreenMetrics
? screenMetrics.rasterDuration?.inMicroseconds
: null,
shader_compilation_duration_micros:
screenMetrics is PerformanceScreenMetrics
? screenMetrics.shaderCompilationDuration?.inMicroseconds
: null,
trace_event_count: screenMetrics is PerformanceScreenMetrics
? screenMetrics.traceEventCount
: null,
// [ProfilerScreenMetrics]
cpu_sample_count: screenMetrics is ProfilerScreenMetrics
? screenMetrics.cpuSampleCount
: null,
cpu_stack_depth: screenMetrics is ProfilerScreenMetrics
? screenMetrics.cpuStackDepth
: null,
// [MemoryScreenMetrics]
heap_diff_objects_before: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapDiffObjectsBefore
: null,
heap_diff_objects_after: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapDiffObjectsAfter
: null,
heap_objects_total: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapObjectsTotal
: null,
// [InspectorScreenMetrics]
root_set_count: screenMetrics is InspectorScreenMetrics
? screenMetrics.rootSetCount
: null,
row_count: screenMetrics is InspectorScreenMetrics
? screenMetrics.rowCount
: null,
inspector_tree_controller_id: screenMetrics is InspectorScreenMetrics
? screenMetrics.inspectorTreeControllerId
: null,
// [DeepLinkScreenMetrics]
android_app_id: screenMetrics is DeepLinkScreenMetrics
? screenMetrics.androidAppId
: null,
ios_bundle_id: screenMetrics is DeepLinkScreenMetrics
? screenMetrics.iosBundleId
: null,
);
}

// Custom dimensions:
external String? get user_app;
external String? get user_build;
Expand Down Expand Up @@ -199,6 +277,78 @@ extension type GtagExceptionDevTools._(JSObject _) implements GtagException {
String? ios_bundle_id, //metric14
});

factory GtagExceptionDevTools._create(
String errorMessage, {
bool fatal = false,
ScreenAnalyticsMetrics? screenMetrics,
}) {
return GtagExceptionDevTools(
description: errorMessage,
fatal: fatal,
user_app: userAppType,
user_build: userBuildType,
user_platform: userPlatformType,
devtools_platform: devtoolsPlatformType,
devtools_chrome: devtoolsChrome,
devtools_version: devtoolsVersion,
ide_launched: _ideLaunched,
flutter_client_id: flutterClientId,
is_external_build: isExternalBuild.toString(),
is_embedded: isEmbedded().toString(),
g3_username: devToolsEnvironmentParameters.username(),
ide_launched_feature: ideLaunchedFeature,
is_wasm: kIsWasm.toString(),
// [PerformanceScreenMetrics]
ui_duration_micros: screenMetrics is PerformanceScreenMetrics
? screenMetrics.uiDuration?.inMicroseconds
: null,
raster_duration_micros: screenMetrics is PerformanceScreenMetrics
? screenMetrics.rasterDuration?.inMicroseconds
: null,
trace_event_count: screenMetrics is PerformanceScreenMetrics
? screenMetrics.traceEventCount
: null,
shader_compilation_duration_micros:
screenMetrics is PerformanceScreenMetrics
? screenMetrics.shaderCompilationDuration?.inMicroseconds
: null,
// [ProfilerScreenMetrics]
cpu_sample_count: screenMetrics is ProfilerScreenMetrics
? screenMetrics.cpuSampleCount
: null,
cpu_stack_depth: screenMetrics is ProfilerScreenMetrics
? screenMetrics.cpuStackDepth
: null,
// [MemoryScreenMetrics]
heap_diff_objects_before: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapDiffObjectsBefore
: null,
heap_diff_objects_after: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapDiffObjectsAfter
: null,
heap_objects_total: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapObjectsTotal
: null,
// [InspectorScreenMetrics]
root_set_count: screenMetrics is InspectorScreenMetrics
? screenMetrics.rootSetCount
: null,
row_count: screenMetrics is InspectorScreenMetrics
? screenMetrics.rowCount
: null,
inspector_tree_controller_id: screenMetrics is InspectorScreenMetrics
? screenMetrics.inspectorTreeControllerId
: null,
// [DeepLinkScreenMetrics]
android_app_id: screenMetrics is DeepLinkScreenMetrics
? screenMetrics.androidAppId
: null,
ios_bundle_id: screenMetrics is DeepLinkScreenMetrics
? screenMetrics.iosBundleId
: null,
);
}

// Custom dimensions:
external String? get user_app;
external String? get user_build;
Expand Down Expand Up @@ -231,158 +381,6 @@ extension type GtagExceptionDevTools._(JSObject _) implements GtagException {
external String? get ios_bundle_id;
}

// This cannot be a factory constructor in the [GtagEventDevTools] class due to
// https://github.com/dart-lang/sdk/issues/46967.
GtagEventDevTools _gtagEvent({
required String event_category,
required String event_label,
String? send_to,
bool non_interaction = false,
int value = 0,
ScreenAnalyticsMetrics? screenMetrics,
}) {
return GtagEventDevTools(
event_category: event_category,
event_label: event_label,
send_to: send_to,
non_interaction: non_interaction,
value: value,
user_app: userAppType,
user_build: userBuildType,
user_platform: userPlatformType,
devtools_platform: devtoolsPlatformType,
devtools_chrome: devtoolsChrome,
devtools_version: devtoolsVersion,
ide_launched: ideLaunched,
flutter_client_id: flutterClientId,
is_external_build: isExternalBuild.toString(),
is_embedded: isEmbedded().toString(),
g3_username: devToolsEnvironmentParameters.username(),
ide_launched_feature: ideLaunchedFeature,
is_wasm: kIsWasm.toString(),
// [PerformanceScreenMetrics]
ui_duration_micros: screenMetrics is PerformanceScreenMetrics
? screenMetrics.uiDuration?.inMicroseconds
: null,
raster_duration_micros: screenMetrics is PerformanceScreenMetrics
? screenMetrics.rasterDuration?.inMicroseconds
: null,
shader_compilation_duration_micros:
screenMetrics is PerformanceScreenMetrics
? screenMetrics.shaderCompilationDuration?.inMicroseconds
: null,
trace_event_count: screenMetrics is PerformanceScreenMetrics
? screenMetrics.traceEventCount
: null,
// [ProfilerScreenMetrics]
cpu_sample_count: screenMetrics is ProfilerScreenMetrics
? screenMetrics.cpuSampleCount
: null,
cpu_stack_depth: screenMetrics is ProfilerScreenMetrics
? screenMetrics.cpuStackDepth
: null,
// [MemoryScreenMetrics]
heap_diff_objects_before: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapDiffObjectsBefore
: null,
heap_diff_objects_after: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapDiffObjectsAfter
: null,
heap_objects_total: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapObjectsTotal
: null,
// [InspectorScreenMetrics]
root_set_count: screenMetrics is InspectorScreenMetrics
? screenMetrics.rootSetCount
: null,
row_count:
screenMetrics is InspectorScreenMetrics ? screenMetrics.rowCount : null,
inspector_tree_controller_id: screenMetrics is InspectorScreenMetrics
? screenMetrics.inspectorTreeControllerId
: null,
// [DeepLinkScreenMetrics]
android_app_id: screenMetrics is DeepLinkScreenMetrics
? screenMetrics.androidAppId
: null,
ios_bundle_id: screenMetrics is DeepLinkScreenMetrics
? screenMetrics.iosBundleId
: null,
);
}

// This cannot be a factory constructor in the [GtagExceptionDevTools] class due to
// https://github.com/dart-lang/sdk/issues/46967.
GtagExceptionDevTools _gtagException(
String errorMessage, {
bool fatal = false,
ScreenAnalyticsMetrics? screenMetrics,
}) {
return GtagExceptionDevTools(
description: errorMessage,
fatal: fatal,
user_app: userAppType,
user_build: userBuildType,
user_platform: userPlatformType,
devtools_platform: devtoolsPlatformType,
devtools_chrome: devtoolsChrome,
devtools_version: devtoolsVersion,
ide_launched: _ideLaunched,
flutter_client_id: flutterClientId,
is_external_build: isExternalBuild.toString(),
is_embedded: isEmbedded().toString(),
g3_username: devToolsEnvironmentParameters.username(),
ide_launched_feature: ideLaunchedFeature,
is_wasm: kIsWasm.toString(),
// [PerformanceScreenMetrics]
ui_duration_micros: screenMetrics is PerformanceScreenMetrics
? screenMetrics.uiDuration?.inMicroseconds
: null,
raster_duration_micros: screenMetrics is PerformanceScreenMetrics
? screenMetrics.rasterDuration?.inMicroseconds
: null,
trace_event_count: screenMetrics is PerformanceScreenMetrics
? screenMetrics.traceEventCount
: null,
shader_compilation_duration_micros:
screenMetrics is PerformanceScreenMetrics
? screenMetrics.shaderCompilationDuration?.inMicroseconds
: null,
// [ProfilerScreenMetrics]
cpu_sample_count: screenMetrics is ProfilerScreenMetrics
? screenMetrics.cpuSampleCount
: null,
cpu_stack_depth: screenMetrics is ProfilerScreenMetrics
? screenMetrics.cpuStackDepth
: null,
// [MemoryScreenMetrics]
heap_diff_objects_before: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapDiffObjectsBefore
: null,
heap_diff_objects_after: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapDiffObjectsAfter
: null,
heap_objects_total: screenMetrics is MemoryScreenMetrics
? screenMetrics.heapObjectsTotal
: null,
// [InspectorScreenMetrics]
root_set_count: screenMetrics is InspectorScreenMetrics
? screenMetrics.rootSetCount
: null,
row_count:
screenMetrics is InspectorScreenMetrics ? screenMetrics.rowCount : null,
inspector_tree_controller_id: screenMetrics is InspectorScreenMetrics
? screenMetrics.inspectorTreeControllerId
: null,
// [DeepLinkScreenMetrics]
android_app_id: screenMetrics is DeepLinkScreenMetrics
? screenMetrics.androidAppId
: null,
ios_bundle_id: screenMetrics is DeepLinkScreenMetrics
? screenMetrics.iosBundleId
: null,
);
}

/// Whether google analytics are enabled.
Future<bool> isAnalyticsEnabled() async {
bool enabled = false;
Expand Down Expand Up @@ -425,7 +423,7 @@ void screen(
int value = 0,
]) {
_log.fine('Event: Screen(screenName:$screenName, value:$value)');
final gtagEvent = _gtagEvent(
final gtagEvent = GtagEventDevTools._create(
event_category: gac.screenViewEvent,
event_label: gac.init,
value: value,
Expand Down Expand Up @@ -570,7 +568,7 @@ void _timing(
'timedOperation:$timedOperation, '
'durationMicros:$durationMicros)',
);
final gtagEvent = _gtagEvent(
final gtagEvent = GtagEventDevTools._create(
event_category: gac.timingEvent,
event_label: timedOperation,
value: durationMicros,
Expand All @@ -595,7 +593,7 @@ void select(
'value:$value, '
'nonInteraction:$nonInteraction)',
);
final gtagEvent = _gtagEvent(
final gtagEvent = GtagEventDevTools._create(
event_category: gac.selectEvent,
event_label: selectedItem,
value: value,
Expand All @@ -620,7 +618,7 @@ void impression(
'screenName:$screenName, '
'item:$item)',
);
final gtagEvent = _gtagEvent(
final gtagEvent = GtagEventDevTools._create(
event_category: gac.impressionEvent,
event_label: item,
non_interaction: true,
Expand All @@ -641,7 +639,7 @@ void reportError(
if (_lastGaError == errorMessage) return;
_lastGaError = errorMessage;

final gTagException = _gtagException(
final gTagException = GtagExceptionDevTools._create(
errorMessage,
fatal: fatal,
);
Expand Down

0 comments on commit 77eae8f

Please sign in to comment.