Skip to content

Commit 23c233c

Browse files
alan-agius4clydin
authored andcommittedSep 23, 2022
fix(@angular/cli): add builders and schematic names as page titles in collected analytics
With this commit the builder and schematic names are added as page title to page events. Also, we address a bug where during a watch or error in some cases analytics where not flushed. Examples when the builder has a watch mode.
1 parent 609d635 commit 23c233c

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed
 

Diff for: ‎packages/angular/cli/src/command-builder/architect-base-command-module.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,15 @@ export abstract class ArchitectBaseCommandModule<T extends object>
5353
return this.onMissingTarget(e.message);
5454
}
5555

56-
await this.reportAnalytics({
57-
...(await architectHost.getOptionsForTarget(target)),
58-
...options,
59-
});
56+
await this.reportAnalytics(
57+
{
58+
...(await architectHost.getOptionsForTarget(target)),
59+
...options,
60+
},
61+
undefined /** paths */,
62+
undefined /** dimensions */,
63+
builderName,
64+
);
6065

6166
const { logger } = this.context;
6267

Diff for: ‎packages/angular/cli/src/command-builder/command-module.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI
140140

141141
// Gather and report analytics.
142142
const analytics = await this.getAnalytics();
143+
let stopPeriodicFlushes: (() => Promise<void>) | undefined;
144+
143145
if (this.shouldReportAnalytics) {
144146
await this.reportAnalytics(camelCasedOptions);
147+
stopPeriodicFlushes = this.periodicAnalyticsFlush(analytics);
145148
}
146149

147150
let exitCode: number | void | undefined;
@@ -151,7 +154,6 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI
151154
exitCode = await this.run(camelCasedOptions as Options<T> & OtherOptions);
152155
const endTime = Date.now();
153156
analytics.timing(this.commandName, 'duration', endTime - startTime);
154-
await analytics.flush();
155157
} catch (e) {
156158
if (e instanceof schema.SchemaValidationException) {
157159
this.context.logger.fatal(`Error: ${e.message}`);
@@ -160,6 +162,8 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI
160162
throw e;
161163
}
162164
} finally {
165+
await stopPeriodicFlushes?.();
166+
163167
if (typeof exitCode === 'number' && exitCode > 0) {
164168
process.exitCode = exitCode;
165169
}
@@ -170,6 +174,7 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI
170174
options: (Options<T> & OtherOptions) | OtherOptions,
171175
paths: string[] = [],
172176
dimensions: (boolean | number | string)[] = [],
177+
title?: string,
173178
): Promise<void> {
174179
for (const [name, ua] of this.optionsWithAnalytics) {
175180
const value = options[name];
@@ -183,6 +188,7 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI
183188
analytics.pageview('/command/' + [this.commandName, ...paths].join('/'), {
184189
dimensions,
185190
metrics: [],
191+
title,
186192
});
187193
}
188194

@@ -275,6 +281,26 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI
275281

276282
return workspace;
277283
}
284+
285+
/**
286+
* Flush on an interval (if the event loop is waiting).
287+
*
288+
* @returns a method that when called will terminate the periodic
289+
* flush and call flush one last time.
290+
*/
291+
private periodicAnalyticsFlush(analytics: analytics.Analytics): () => Promise<void> {
292+
let analyticsFlushPromise = Promise.resolve();
293+
const analyticsFlushInterval = setInterval(() => {
294+
analyticsFlushPromise = analyticsFlushPromise.then(() => analytics.flush());
295+
}, 2000);
296+
297+
return () => {
298+
clearInterval(analyticsFlushInterval);
299+
300+
// Flush one last time.
301+
return analyticsFlushPromise.then(() => analytics.flush());
302+
};
303+
}
278304
}
279305

280306
/**

Diff for: ‎packages/angular/cli/src/command-builder/schematics-command-module.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,13 @@ export abstract class SchematicsCommandModule
147147
workflow.engineHost.registerOptionsTransform(async (schematic, options) => {
148148
if (shouldReportAnalytics) {
149149
shouldReportAnalytics = false;
150-
// ng generate lib -> ng generate
151-
const commandName = this.command?.split(' ', 1)[0];
152-
153-
await this.reportAnalytics(options as {}, [
154-
commandName,
155-
schematic.collection.name.replace(/\//g, '_'),
156-
schematic.name.replace(/\//g, '_'),
157-
]);
150+
151+
await this.reportAnalytics(
152+
options as {},
153+
undefined /** paths */,
154+
undefined /** dimensions */,
155+
schematic.collection.name + ':' + schematic.name,
156+
);
158157
}
159158

160159
// TODO: The below should be removed in version 15 when we change 1P schematics to use the `workingDirectory smart default`.

0 commit comments

Comments
 (0)