Skip to content

Commit aace49e

Browse files
committed
refactor(@angular/cli): improve analytics handling
This commit includes minor changes to how analytics are handled, including: - Removing unnecessary type castings. - Concatenating arrays into strings for Google Analytics, as it doesn't support arrays. - Applying default values to multi-select prompts for a smoother user experience.
1 parent 1feac5f commit aace49e

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

packages/angular/cli/src/command-builder/command-module.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI
8989
protected readonly shouldReportAnalytics: boolean = true;
9090
readonly scope: CommandScope = CommandScope.Both;
9191

92-
private readonly optionsWithAnalytics = new Map<string, string>();
92+
private readonly optionsWithAnalytics = new Map<
93+
string,
94+
EventCustomDimension | EventCustomMetric
95+
>();
9396

9497
constructor(protected readonly context: CommandContext) {}
9598

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

238241
for (const [name, ua] of this.optionsWithAnalytics) {
242+
if (!validEventCustomDimensionAndMetrics.has(ua)) {
243+
continue;
244+
}
245+
239246
const value = options[name];
240-
if (
241-
(typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') &&
242-
validEventCustomDimensionAndMetrics.has(ua as EventCustomDimension | EventCustomMetric)
243-
) {
244-
parameters[ua as EventCustomDimension | EventCustomMetric] = value;
247+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
248+
parameters[ua] = value;
249+
} else if (Array.isArray(value)) {
250+
// GA doesn't allow array as values.
251+
parameters[ua] = value.sort().join(', ');
245252
}
246253
}
247254

packages/angular/cli/src/command-builder/schematics-command-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,13 @@ export abstract class SchematicsCommandModule
204204
? {
205205
name: item,
206206
value: item,
207+
checked: item === definition.default,
207208
}
208209
: {
209210
...item,
210211
name: item.label,
211212
value: item.value,
213+
checked: item.value === definition.default,
212214
},
213215
),
214216
});

packages/angular/cli/src/command-builder/utilities/json-schema.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

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

1213
/**
1314
* An option description.
@@ -278,10 +279,10 @@ export function addSchemaOptionsToCommand<T>(
278279
localYargs: Argv<T>,
279280
options: Option[],
280281
includeDefaultValues: boolean,
281-
): Map<string, string> {
282+
): Map<string, EventCustomDimension> {
282283
const booleanOptionsWithNoPrefix = new Set<string>();
283284
const keyValuePairOptions = new Set<string>();
284-
const optionsWithAnalytics = new Map<string, string>();
285+
const optionsWithAnalytics = new Map<string, EventCustomDimension>();
285286

286287
for (const option of options) {
287288
const {
@@ -336,7 +337,7 @@ export function addSchemaOptionsToCommand<T>(
336337

337338
// Record option of analytics.
338339
if (userAnalytics !== undefined) {
339-
optionsWithAnalytics.set(name, userAnalytics);
340+
optionsWithAnalytics.set(name, userAnalytics as EventCustomDimension);
340341
}
341342
}
342343

0 commit comments

Comments
 (0)