Skip to content

Commit 16248fe

Browse files
Merge pull request #3518 from NativeScript/vladimirov/getting-started-analytics-2
feat: Improve Getting started - add analytics
2 parents 5253501 + 32115da commit 16248fe

9 files changed

+143
-117
lines changed

lib/commands/run.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class RunCommandBase implements ICommand {
2828
this.platform = this.$devicePlatformsConstants.Android;
2929
}
3030

31-
this.$liveSyncCommandHelper.validatePlatform(this.platform);
31+
await this.$liveSyncCommandHelper.validatePlatform(this.platform);
3232

3333
return true;
3434
}

lib/constants.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ export const enum TrackActionNames {
135135
CreateProject = "Create project",
136136
Debug = "Debug",
137137
Deploy = "Deploy",
138-
LiveSync = "LiveSync"
138+
LiveSync = "LiveSync",
139+
RunSetupScript = "Run Setup Script",
140+
CheckLocalBuildSetup = "Check Local Build Setup",
141+
CheckEnvironmentRequirements = "Check Environment Requirements"
139142
}
140143

141144
export const enum BuildStates {

lib/definitions/platform.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,5 +381,5 @@ interface IUpdateAppOptions extends IOptionalFilesToSync, IOptionalFilesToRemove
381381
}
382382

383383
interface IPlatformEnvironmentRequirements {
384-
checkEnvironmentRequirements(platform: string): Promise<boolean>;
384+
checkEnvironmentRequirements(platform?: string): Promise<boolean>;
385385
}

lib/services/analytics/analytics-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class AnalyticsService extends AnalyticsServiceBase {
7373

7474
// In some cases (like in case action is Build and platform is Android), we do not know if the deviceType is emulator or device.
7575
// Just exclude the device_type in this case.
76-
if (isForDevice !== null) {
76+
if (isForDevice !== null && isForDevice !== undefined) {
7777
const deviceType = isForDevice ? DeviceTypes.Device : (this.$mobileHelper.isAndroidPlatform(platform) ? DeviceTypes.Emulator : DeviceTypes.Simulator);
7878
label = this.addDataToLabel(label, deviceType);
7979
}

lib/services/analytics/google-analytics-provider.ts

+5-28
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { AnalyticsClients } from "../../common/constants";
44

55
export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider {
66
private static GA_TRACKING_ID = "UA-111455-44";
7-
private static GA_CROSS_CLIENT_TRACKING_ID = "UA-111455-51";
87
private currentPage: string;
98

109
constructor(private clientId: string,
@@ -15,15 +14,12 @@ export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider {
1514
}
1615

1716
public async trackHit(trackInfo: IGoogleAnalyticsData): Promise<void> {
18-
const trackingIds = [GoogleAnalyticsProvider.GA_TRACKING_ID, GoogleAnalyticsProvider.GA_CROSS_CLIENT_TRACKING_ID];
1917
const sessionId = uuid.v4();
2018

21-
for (const gaTrackingId of trackingIds) {
22-
try {
23-
await this.track(gaTrackingId, trackInfo, sessionId);
24-
} catch (e) {
25-
this.$logger.trace("Analytics exception: ", e);
26-
}
19+
try {
20+
await this.track(GoogleAnalyticsProvider.GA_TRACKING_ID, trackInfo, sessionId);
21+
} catch (e) {
22+
this.$logger.trace("Analytics exception: ", e);
2723
}
2824
}
2925

@@ -41,14 +37,7 @@ export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider {
4137
}
4238
});
4339

44-
switch (gaTrackingId) {
45-
case GoogleAnalyticsProvider.GA_CROSS_CLIENT_TRACKING_ID:
46-
this.setCrossClientCustomDimensions(visitor, sessionId);
47-
break;
48-
default:
49-
await this.setCustomDimensions(visitor, trackInfo.customDimensions, sessionId);
50-
break;
51-
}
40+
await this.setCustomDimensions(visitor, trackInfo.customDimensions, sessionId);
5241

5342
switch (trackInfo.googleAnalyticsDataType) {
5443
case GoogleAnalyticsDataType.Page:
@@ -83,18 +72,6 @@ export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider {
8372
});
8473
}
8574

86-
private async setCrossClientCustomDimensions(visitor: ua.Visitor, sessionId: string): Promise<void> {
87-
const customDimensions: IStringDictionary = {
88-
[GoogleAnalyticsCrossClientCustomDimensions.sessionId]: sessionId,
89-
[GoogleAnalyticsCrossClientCustomDimensions.clientId]: this.clientId,
90-
[GoogleAnalyticsCrossClientCustomDimensions.crossClientId]: this.clientId,
91-
};
92-
93-
_.each(customDimensions, (value, key) => {
94-
visitor.set(key, value);
95-
});
96-
}
97-
9875
private trackEvent(visitor: ua.Visitor, trackInfo: IGoogleAnalyticsEventData): Promise<void> {
9976
return new Promise<void>((resolve, reject) => {
10077
visitor.event(trackInfo.category, trackInfo.action, trackInfo.label, trackInfo.value, { p: this.currentPage }, (err: Error) => {

lib/services/doctor-service.ts

+36-36
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import { EOL } from "os";
22
import * as path from "path";
33
import * as helpers from "../common/helpers";
4+
import { TrackActionNames } from "../constants";
45
import { doctor, constants } from "nativescript-doctor";
56

67
class DoctorService implements IDoctorService {
78
private static DarwinSetupScriptLocation = path.join(__dirname, "..", "..", "setup", "mac-startup-shell-script.sh");
8-
private static DarwinSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-os-x";
99
private static WindowsSetupScriptExecutable = "powershell.exe";
1010
private static WindowsSetupScriptArguments = ["start-process", "-FilePath", "PowerShell.exe", "-NoNewWindow", "-Wait", "-ArgumentList", '"-NoProfile -ExecutionPolicy Bypass -Command iex ((new-object net.webclient).DownloadString(\'https://www.nativescript.org/setup/win\'))"'];
11-
private static WindowsSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-win";
12-
private static LinuxSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-linux";
1311

1412
constructor(private $analyticsService: IAnalyticsService,
1513
private $hostInfo: IHostInfo,
1614
private $logger: ILogger,
1715
private $childProcess: IChildProcess,
18-
private $opener: IOpener,
19-
private $prompter: IPrompter,
16+
private $injector: IInjector,
2017
private $terminalSpinnerService: ITerminalSpinnerService,
2118
private $versionsService: IVersionsService) { }
2219

@@ -41,7 +38,6 @@ class DoctorService implements IDoctorService {
4138

4239
if (hasWarnings) {
4340
this.$logger.info("There seem to be issues with your configuration.");
44-
await this.promptForHelp();
4541
} else {
4642
this.$logger.out("No issues were detected.".bold);
4743
}
@@ -51,61 +47,65 @@ class DoctorService implements IDoctorService {
5147
} catch (err) {
5248
this.$logger.error("Cannot get the latest versions information from npm. Please try again later.");
5349
}
50+
51+
await this.$injector.resolve("platformEnvironmentRequirements").checkEnvironmentRequirements(null);
5452
}
5553

56-
public runSetupScript(): Promise<ISpawnResult> {
54+
public async runSetupScript(): Promise<ISpawnResult> {
55+
await this.$analyticsService.trackEventActionInGoogleAnalytics({
56+
action: TrackActionNames.RunSetupScript,
57+
additionalData: "Starting",
58+
});
59+
5760
if (this.$hostInfo.isLinux) {
61+
await this.$analyticsService.trackEventActionInGoogleAnalytics({
62+
action: TrackActionNames.RunSetupScript,
63+
additionalData: "Skipped as OS is Linux",
64+
});
5865
return;
5966
}
6067

6168
this.$logger.out("Running the setup script to try and automatically configure your environment.");
6269

6370
if (this.$hostInfo.isDarwin) {
64-
return this.runSetupScriptCore(DoctorService.DarwinSetupScriptLocation, []);
71+
await this.runSetupScriptCore(DoctorService.DarwinSetupScriptLocation, []);
6572
}
6673

6774
if (this.$hostInfo.isWindows) {
68-
return this.runSetupScriptCore(DoctorService.WindowsSetupScriptExecutable, DoctorService.WindowsSetupScriptArguments);
75+
await this.runSetupScriptCore(DoctorService.WindowsSetupScriptExecutable, DoctorService.WindowsSetupScriptArguments);
6976
}
77+
78+
await this.$analyticsService.trackEventActionInGoogleAnalytics({
79+
action: TrackActionNames.RunSetupScript,
80+
additionalData: "Finished",
81+
});
7082
}
7183

7284
public async canExecuteLocalBuild(platform?: string): Promise<boolean> {
85+
await this.$analyticsService.trackEventActionInGoogleAnalytics({
86+
action: TrackActionNames.CheckLocalBuildSetup,
87+
additionalData: "Starting",
88+
});
7389
const infos = await doctor.getInfos({ platform });
7490

7591
const warnings = this.filterInfosByType(infos, constants.WARNING_TYPE_NAME);
76-
if (warnings.length > 0) {
92+
const hasWarnings = warnings.length > 0;
93+
if (hasWarnings) {
94+
await this.$analyticsService.trackEventActionInGoogleAnalytics({
95+
action: TrackActionNames.CheckLocalBuildSetup,
96+
additionalData: `Warnings:${warnings.map(w => w.message).join("__")}`,
97+
});
7798
this.printInfosCore(infos);
7899
} else {
79100
infos.map(info => this.$logger.trace(info.message));
80101
}
81-
return warnings.length === 0;
82-
}
83-
84-
private async promptForDocs(link: string): Promise<void> {
85-
if (await this.$prompter.confirm("Do you want to visit the official documentation?", () => helpers.isInteractive())) {
86-
this.$opener.open(link);
87-
}
88-
}
89-
90-
private async promptForSetupScript(executablePath: string, setupScriptArgs: string[]): Promise<void> {
91-
if (await this.$prompter.confirm("Do you want to run the setup script?", () => helpers.isInteractive())) {
92-
await this.runSetupScriptCore(executablePath, setupScriptArgs);
93-
}
94-
}
95102

96-
private async promptForHelp(): Promise<void> {
97-
if (this.$hostInfo.isDarwin) {
98-
await this.promptForHelpCore(DoctorService.DarwinSetupDocsLink, DoctorService.DarwinSetupScriptLocation, []);
99-
} else if (this.$hostInfo.isWindows) {
100-
await this.promptForHelpCore(DoctorService.WindowsSetupDocsLink, DoctorService.WindowsSetupScriptExecutable, DoctorService.WindowsSetupScriptArguments);
101-
} else {
102-
await this.promptForDocs(DoctorService.LinuxSetupDocsLink);
103-
}
104-
}
103+
await this.$analyticsService.trackEventActionInGoogleAnalytics({
104+
action: TrackActionNames.CheckLocalBuildSetup,
105+
additionalData: `Finished: Is setup correct: ${!hasWarnings}`,
106+
});
105107

106-
private async promptForHelpCore(link: string, setupScriptExecutablePath: string, setupScriptArgs: string[]): Promise<void> {
107-
await this.promptForDocs(link);
108-
await this.promptForSetupScript(setupScriptExecutablePath, setupScriptArgs);
108+
return !hasWarnings;
109109
}
110110

111111
private async runSetupScriptCore(executablePath: string, setupScriptArgs: string[]): Promise<ISpawnResult> {

0 commit comments

Comments
 (0)