Skip to content

feat: add tracking in analytics for company using NativeScript CLI #4984

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 8 commits into from
Aug 29, 2019
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: 2 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"DISABLE_HOOKS": false,
"UPLOAD_PLAYGROUND_FILES_ENDPOINT": "https://play.nativescript.org/api/files",
"SHORTEN_URL_ENDPOINT": "https://play.nativescript.org/api/shortenurl?longUrl=%s",
"INSIGHTS_URL_ENDPOINT": "https://play-server.nativescript.org/api/insights?ipAddress=%s",
"WHOAMI_URL_ENDPOINT": "https://play.nativescript.org/api/whoami",
"PREVIEW_APP_ENVIRONMENT": "live",
"GA_TRACKING_ID": "UA-111455-51"
}
3 changes: 3 additions & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ $injector.require("userSettingsService", "./services/user-settings-service");
$injector.requirePublic("analyticsSettingsService", "./services/analytics-settings-service");
$injector.require("analyticsService", "./services/analytics/analytics-service");
$injector.require("googleAnalyticsProvider", "./services/analytics/google-analytics-provider");
$injector.requirePublicClass("companyInsightsController", "./controllers/company-insights-controller");

$injector.require("platformCommandParameter", "./platform-command-param");
$injector.requireCommand("create", "./commands/create-project");
Expand Down Expand Up @@ -228,3 +229,5 @@ $injector.require("watchIgnoreListService", "./services/watch-ignore-list-servic
$injector.requirePublicClass("initializeService", "./services/initialize-service");

$injector.require("npmConfigService", "./services/npm-config-service");
$injector.require("ipService", "./services/ip-service");
$injector.require("jsonFileSettingsService", "./common/services/json-file-settings-service");
5 changes: 2 additions & 3 deletions lib/common/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1229,9 +1229,8 @@ interface IPlistParser {
parseFileSync(plistFilePath: string): any;
}

interface IUserSettingsService extends UserSettings.IUserSettingsService {
loadUserSettingsFile(): Promise<void>;
saveSettings(data: IDictionary<{}>): Promise<void>;
interface IUserSettingsService extends IJsonFileSettingsService {
// keep for backwards compatibility
}

/**
Expand Down
15 changes: 15 additions & 0 deletions lib/common/definitions/json-file-settings-service.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
interface ICacheTimeoutOpts {
cacheTimeout: number;
}

interface IUseCacheOpts {
useCaching: boolean;
}

interface IJsonFileSettingsService {
getSettingValue<T>(settingName: string, cacheOpts?: ICacheTimeoutOpts): Promise<T>;
saveSetting<T>(key: string, value: T, cacheOpts?: IUseCacheOpts): Promise<void>;
removeSetting(key: string): Promise<void>;
loadUserSettingsFile(): Promise<void>;
saveSettings(data: IDictionary<{}>, cacheOpts?: IUseCacheOpts): Promise<void>;
}
7 changes: 0 additions & 7 deletions lib/common/definitions/user-settings.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ declare const enum GoogleAnalyticsCustomDimensions {
nodeVersion = "cd6",
playgroundId = "cd7",
usedTutorial = "cd8",
isShared = "cd9"
isShared = "cd9",
companyName = "cd10",
companyCountry = "cd11",
companyRevenue = "cd12",
companyIndustries = "cd13",
companyEmployeeCount = "cd14",
}
132 changes: 132 additions & 0 deletions lib/common/services/json-file-settings-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import * as path from "path";
import { parseJson } from "../helpers";

export class JsonFileSettingsService implements IJsonFileSettingsService {
private jsonSettingsFilePath: string = null;
protected jsonSettingsData: any = null;
private get lockFilePath(): string {
return `${this.jsonSettingsFilePath}.lock`;
}

constructor(jsonFileSettingsPath: string,
private $fs: IFileSystem,
private $lockService: ILockService,
private $logger: ILogger) {
this.jsonSettingsFilePath = jsonFileSettingsPath;
}

public async getSettingValue<T>(settingName: string, cacheOpts?: { cacheTimeout: number }): Promise<T> {
const action = async (): Promise<T> => {
await this.loadUserSettingsFile();

if (this.jsonSettingsData && _.has(this.jsonSettingsData, settingName)) {
const data = this.jsonSettingsData[settingName];
const dataToReturn = data.modifiedByCacheMechanism ? data.value : data;
if (cacheOpts && cacheOpts.cacheTimeout) {
if (!data.modifiedByCacheMechanism) {
// If data has no cache, but we want to check the timeout, consider the data as outdated.
// this should be a really rare case
return null;
}

const currentTime = Date.now();
if ((currentTime - data.time) > cacheOpts.cacheTimeout) {
return null;
}
}

return dataToReturn;
}

return null;
};

return this.$lockService.executeActionWithLock<T>(action, this.lockFilePath);
}

public async saveSetting<T>(key: string, value: T, cacheOpts?: { useCaching: boolean }): Promise<void> {
const settingObject: any = {};
settingObject[key] = value;

return this.saveSettings(settingObject, cacheOpts);
}

public async removeSetting(key: string): Promise<void> {
const action = async (): Promise<void> => {
await this.loadUserSettingsFile();

delete this.jsonSettingsData[key];
await this.saveSettings();
};

return this.$lockService.executeActionWithLock<void>(action, this.lockFilePath);
}

public saveSettings(data?: any, cacheOpts?: { useCaching: boolean }): Promise<void> {
const action = async (): Promise<void> => {
await this.loadUserSettingsFile();
this.jsonSettingsData = this.jsonSettingsData || {};

_(data)
.keys()
.each(propertyName => {
this.jsonSettingsData[propertyName] = cacheOpts && cacheOpts.useCaching && !data[propertyName].modifiedByCacheMechanism ? {
time: Date.now(),
value: data[propertyName],
modifiedByCacheMechanism: true
} : data[propertyName];
});

this.$fs.writeJson(this.jsonSettingsFilePath, this.jsonSettingsData);
};

return this.$lockService.executeActionWithLock<void>(action, this.lockFilePath);
}

public async loadUserSettingsFile(): Promise<void> {
if (!this.jsonSettingsData) {
await this.loadUserSettingsData();
}
}

private async loadUserSettingsData(): Promise<void> {
if (!this.$fs.exists(this.jsonSettingsFilePath)) {
const unexistingDirs = this.getUnexistingDirectories(this.jsonSettingsFilePath);

this.$fs.writeFile(this.jsonSettingsFilePath, null);

// when running under 'sudo' we create the <path to home dir>/.local/share/.nativescript-cli dir with root as owner
// and other Applications cannot access this directory anymore. (bower/heroku/etc)
if (process.env.SUDO_USER) {
for (const dir of unexistingDirs) {
await this.$fs.setCurrentUserAsOwner(dir, process.env.SUDO_USER);
}
}
}

const data = this.$fs.readText(this.jsonSettingsFilePath);

try {
this.jsonSettingsData = parseJson(data);
} catch (err) {
this.$logger.trace(`Error while trying to parseJson ${data} data from ${this.jsonSettingsFilePath} file. Err is: ${err}`);
this.$fs.deleteFile(this.jsonSettingsFilePath);
}
}

private getUnexistingDirectories(filePath: string): Array<string> {
const unexistingDirs: Array<string> = [];
let currentDir = path.join(filePath, "..");
while (true) {
// this directory won't be created.
if (this.$fs.exists(currentDir)) {
break;
}
unexistingDirs.push(currentDir);
currentDir = path.join(currentDir, "..");
}
return unexistingDirs;
}
}

$injector.register("jsonFileSettingsService", JsonFileSettingsService, false);
107 changes: 0 additions & 107 deletions lib/common/services/user-settings-service.ts

This file was deleted.

Loading