Skip to content

feat: Improve Getting started - add analytics #3518

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 4 commits into from
Apr 4, 2018
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
2 changes: 1 addition & 1 deletion lib/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class RunCommandBase implements ICommand {
this.platform = this.$devicePlatformsConstants.Android;
}

this.$liveSyncCommandHelper.validatePlatform(this.platform);
await this.$liveSyncCommandHelper.validatePlatform(this.platform);

return true;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ export const enum TrackActionNames {
CreateProject = "Create project",
Debug = "Debug",
Deploy = "Deploy",
LiveSync = "LiveSync"
LiveSync = "LiveSync",
RunSetupScript = "Run Setup Script",
CheckLocalBuildSetup = "Check Local Build Setup",
CheckEnvironmentRequirements = "Check Environment Requirements"
}

export const enum BuildStates {
Expand Down
2 changes: 1 addition & 1 deletion lib/definitions/platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,5 +381,5 @@ interface IUpdateAppOptions extends IOptionalFilesToSync, IOptionalFilesToRemove
}

interface IPlatformEnvironmentRequirements {
checkEnvironmentRequirements(platform: string): Promise<boolean>;
checkEnvironmentRequirements(platform?: string): Promise<boolean>;
}
2 changes: 1 addition & 1 deletion lib/services/analytics/analytics-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class AnalyticsService extends AnalyticsServiceBase {

// In some cases (like in case action is Build and platform is Android), we do not know if the deviceType is emulator or device.
// Just exclude the device_type in this case.
if (isForDevice !== null) {
if (isForDevice !== null && isForDevice !== undefined) {
const deviceType = isForDevice ? DeviceTypes.Device : (this.$mobileHelper.isAndroidPlatform(platform) ? DeviceTypes.Emulator : DeviceTypes.Simulator);
label = this.addDataToLabel(label, deviceType);
}
Expand Down
33 changes: 5 additions & 28 deletions lib/services/analytics/google-analytics-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { AnalyticsClients } from "../../common/constants";

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

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

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

for (const gaTrackingId of trackingIds) {
try {
await this.track(gaTrackingId, trackInfo, sessionId);
} catch (e) {
this.$logger.trace("Analytics exception: ", e);
}
try {
await this.track(GoogleAnalyticsProvider.GA_TRACKING_ID, trackInfo, sessionId);
} catch (e) {
this.$logger.trace("Analytics exception: ", e);
}
}

Expand All @@ -41,14 +37,7 @@ export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider {
}
});

switch (gaTrackingId) {
case GoogleAnalyticsProvider.GA_CROSS_CLIENT_TRACKING_ID:
this.setCrossClientCustomDimensions(visitor, sessionId);
break;
default:
await this.setCustomDimensions(visitor, trackInfo.customDimensions, sessionId);
break;
}
await this.setCustomDimensions(visitor, trackInfo.customDimensions, sessionId);

switch (trackInfo.googleAnalyticsDataType) {
case GoogleAnalyticsDataType.Page:
Expand Down Expand Up @@ -83,18 +72,6 @@ export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider {
});
}

private async setCrossClientCustomDimensions(visitor: ua.Visitor, sessionId: string): Promise<void> {
const customDimensions: IStringDictionary = {
[GoogleAnalyticsCrossClientCustomDimensions.sessionId]: sessionId,
[GoogleAnalyticsCrossClientCustomDimensions.clientId]: this.clientId,
[GoogleAnalyticsCrossClientCustomDimensions.crossClientId]: this.clientId,
};

_.each(customDimensions, (value, key) => {
visitor.set(key, value);
});
}

private trackEvent(visitor: ua.Visitor, trackInfo: IGoogleAnalyticsEventData): Promise<void> {
return new Promise<void>((resolve, reject) => {
visitor.event(trackInfo.category, trackInfo.action, trackInfo.label, trackInfo.value, { p: this.currentPage }, (err: Error) => {
Expand Down
72 changes: 36 additions & 36 deletions lib/services/doctor-service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { EOL } from "os";
import * as path from "path";
import * as helpers from "../common/helpers";
import { TrackActionNames } from "../constants";
import { doctor, constants } from "nativescript-doctor";

class DoctorService implements IDoctorService {
private static DarwinSetupScriptLocation = path.join(__dirname, "..", "..", "setup", "mac-startup-shell-script.sh");
private static DarwinSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-os-x";
private static WindowsSetupScriptExecutable = "powershell.exe";
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\'))"'];
private static WindowsSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-win";
private static LinuxSetupDocsLink = "https://docs.nativescript.org/start/ns-setup-linux";

constructor(private $analyticsService: IAnalyticsService,
private $hostInfo: IHostInfo,
private $logger: ILogger,
private $childProcess: IChildProcess,
private $opener: IOpener,
private $prompter: IPrompter,
private $injector: IInjector,
private $terminalSpinnerService: ITerminalSpinnerService,
private $versionsService: IVersionsService) { }

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

if (hasWarnings) {
this.$logger.info("There seem to be issues with your configuration.");
await this.promptForHelp();
} else {
this.$logger.out("No issues were detected.".bold);
}
Expand All @@ -51,61 +47,65 @@ class DoctorService implements IDoctorService {
} catch (err) {
this.$logger.error("Cannot get the latest versions information from npm. Please try again later.");
}

await this.$injector.resolve("platformEnvironmentRequirements").checkEnvironmentRequirements(null);
}

public runSetupScript(): Promise<ISpawnResult> {
public async runSetupScript(): Promise<ISpawnResult> {
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.RunSetupScript,
additionalData: "Starting",
});

if (this.$hostInfo.isLinux) {
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.RunSetupScript,
additionalData: "Skipped as OS is Linux",
});
return;
}

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

if (this.$hostInfo.isDarwin) {
return this.runSetupScriptCore(DoctorService.DarwinSetupScriptLocation, []);
await this.runSetupScriptCore(DoctorService.DarwinSetupScriptLocation, []);
}

if (this.$hostInfo.isWindows) {
return this.runSetupScriptCore(DoctorService.WindowsSetupScriptExecutable, DoctorService.WindowsSetupScriptArguments);
await this.runSetupScriptCore(DoctorService.WindowsSetupScriptExecutable, DoctorService.WindowsSetupScriptArguments);
}

await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.RunSetupScript,
additionalData: "Finished",
});
}

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

const warnings = this.filterInfosByType(infos, constants.WARNING_TYPE_NAME);
if (warnings.length > 0) {
const hasWarnings = warnings.length > 0;
if (hasWarnings) {
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.CheckLocalBuildSetup,
additionalData: `Warnings:${warnings.map(w => w.message).join("__")}`,
});
this.printInfosCore(infos);
} else {
infos.map(info => this.$logger.trace(info.message));
}
return warnings.length === 0;
}

private async promptForDocs(link: string): Promise<void> {
if (await this.$prompter.confirm("Do you want to visit the official documentation?", () => helpers.isInteractive())) {
this.$opener.open(link);
}
}

private async promptForSetupScript(executablePath: string, setupScriptArgs: string[]): Promise<void> {
if (await this.$prompter.confirm("Do you want to run the setup script?", () => helpers.isInteractive())) {
await this.runSetupScriptCore(executablePath, setupScriptArgs);
}
}

private async promptForHelp(): Promise<void> {
if (this.$hostInfo.isDarwin) {
await this.promptForHelpCore(DoctorService.DarwinSetupDocsLink, DoctorService.DarwinSetupScriptLocation, []);
} else if (this.$hostInfo.isWindows) {
await this.promptForHelpCore(DoctorService.WindowsSetupDocsLink, DoctorService.WindowsSetupScriptExecutable, DoctorService.WindowsSetupScriptArguments);
} else {
await this.promptForDocs(DoctorService.LinuxSetupDocsLink);
}
}
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.CheckLocalBuildSetup,
additionalData: `Finished: Is setup correct: ${!hasWarnings}`,
});

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

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