Skip to content

refactor(@angular/cli): improve analytics handling #30934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/angular/cli/src/command-builder/command-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI
protected readonly shouldReportAnalytics: boolean = true;
readonly scope: CommandScope = CommandScope.Both;

private readonly optionsWithAnalytics = new Map<string, string>();
private readonly optionsWithAnalytics = new Map<
string,
EventCustomDimension | EventCustomMetric
>();

constructor(protected readonly context: CommandContext) {}

Expand Down Expand Up @@ -236,12 +239,16 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI
]);

for (const [name, ua] of this.optionsWithAnalytics) {
if (!validEventCustomDimensionAndMetrics.has(ua)) {
continue;
}

const value = options[name];
if (
(typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') &&
validEventCustomDimensionAndMetrics.has(ua as EventCustomDimension | EventCustomMetric)
) {
parameters[ua as EventCustomDimension | EventCustomMetric] = value;
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
parameters[ua] = value;
} else if (Array.isArray(value)) {
// GA doesn't allow array as values.
parameters[ua] = value.sort().join(', ');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,13 @@ export abstract class SchematicsCommandModule
? {
name: item,
value: item,
checked: item === definition.default,
}
: {
...item,
name: item.label,
value: item.value,
checked: item.value === definition.default,
},
),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { json, strings } from '@angular-devkit/core';
import type { Arguments, Argv, PositionalOptions, Options as YargsOptions } from 'yargs';
import { EventCustomDimension } from '../../analytics/analytics-parameters';

/**
* An option description.
Expand Down Expand Up @@ -280,10 +281,10 @@ export function addSchemaOptionsToCommand<T>(
localYargs: Argv<T>,
options: Option[],
includeDefaultValues: boolean,
): Map<string, string> {
): Map<string, EventCustomDimension> {
const booleanOptionsWithNoPrefix = new Set<string>();
const keyValuePairOptions = new Set<string>();
const optionsWithAnalytics = new Map<string, string>();
const optionsWithAnalytics = new Map<string, EventCustomDimension>();

for (const option of options) {
const {
Expand Down Expand Up @@ -338,7 +339,7 @@ export function addSchemaOptionsToCommand<T>(

// Record option of analytics.
if (userAnalytics !== undefined) {
optionsWithAnalytics.set(name, userAnalytics);
optionsWithAnalytics.set(name, userAnalytics as EventCustomDimension);
}
}

Expand Down
Loading