From 262f1b8c94337566c931b52b8127b9f4df70a960 Mon Sep 17 00:00:00 2001 From: Ventsislav Georgiev Date: Thu, 2 Nov 2017 16:35:21 +0200 Subject: [PATCH] Expose getUserAgentString from the analyticsSettingsService --- PublicAPI.md | 18 +++++++++ lib/common | 2 +- lib/services/analytics-settings-service.ts | 36 ++++++++++++++++++ .../analytics/google-analytics-provider.ts | 38 +------------------ 4 files changed, 57 insertions(+), 37 deletions(-) diff --git a/PublicAPI.md b/PublicAPI.md index 70542d1b3a..0b3b3547bd 100644 --- a/PublicAPI.md +++ b/PublicAPI.md @@ -878,6 +878,24 @@ tns.analyticsSettingsService.getClientId() .then(clientId => console.log(clientId)); ``` +### getUserAgentString +The `getUserAgentString` method allows retrieving a user agent string identifying the current system + +* Definition: +```TypeScript +/** + * Gets user agent string identifing the current system in the following format: `${identifier} (${systemInfo}) ${osArch}` + * @param {string} identifier The product identifier. + * @returns {string} The user agent string. + */ +getUserAgentString(identifier: string): string; +``` + +* Usage: +```JavaScript +const userAgentString = tns.analyticsSettingsService.getUserAgentString("tns/3.3.0"); +``` + ## How to add a new method to Public API CLI is designed as command line tool and when it is used as a library, it does not give you access to all of the methods. This is mainly implementation detail. Most of the CLI's code is created to work in command line, not as a library, so before adding method to public API, most probably it will require some modification. For example the `$options` injected module contains information about all `--` options passed on the terminal. When the CLI is used as a library, the options are not populated. Before adding method to public API, make sure its implementation does not rely on `$options`. diff --git a/lib/common b/lib/common index c8a450bf11..eac6e6bec9 160000 --- a/lib/common +++ b/lib/common @@ -1 +1 @@ -Subproject commit c8a450bf114c67125ebbedbd073f4ff5475a095c +Subproject commit eac6e6bec934f92edae1bbb8b12e845ba8aa4bb0 diff --git a/lib/services/analytics-settings-service.ts b/lib/services/analytics-settings-service.ts index 250e4b0db0..8930796165 100644 --- a/lib/services/analytics-settings-service.ts +++ b/lib/services/analytics-settings-service.ts @@ -6,6 +6,8 @@ class AnalyticsSettingsService implements IAnalyticsSettingsService { constructor(private $userSettingsService: UserSettings.IUserSettingsService, private $staticConfig: IStaticConfig, + private $hostInfo: IHostInfo, + private $osInfo: IOsInfo, private $logger: ILogger) { } public async canDoRequest(): Promise { @@ -38,6 +40,40 @@ class AnalyticsSettingsService implements IAnalyticsSettingsService { return this.$userSettingsService.saveSetting(this.getSessionsProjectKey(projectName), count); } + @exported("analyticsSettingsService") + public getUserAgentString(identifier: string): string { + let osString = ""; + const osRelease = this.$osInfo.release(); + + if (this.$hostInfo.isWindows) { + osString = `Windows NT ${osRelease}`; + } else if (this.$hostInfo.isDarwin) { + osString = `Macintosh`; + const macRelease = this.getMacOSReleaseVersion(osRelease); + if (macRelease) { + osString += `; Intel Mac OS X ${macRelease}`; + } + } else { + osString = `Linux x86`; + if (this.$osInfo.arch() === "x64") { + osString += "_64"; + } + } + + const userAgent = `${identifier} (${osString}; ${this.$osInfo.arch()})`; + + return userAgent; + } + + private getMacOSReleaseVersion(osRelease: string): string { + // https://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history + // Each macOS version is labeled 10., where it looks like is taken from the major version returned by os.release() (16.x.x for example) and subtracting 4 from it. + // So the version becomes "10.12" in this case. + // Could be improved by spawning `system_profiler SPSoftwareDataType` and getting the System Version line from the result. + const majorVersion = osRelease && _.first(osRelease.split(".")); + return majorVersion && `10.${+majorVersion - 4}`; + } + private getSessionsProjectKey(projectName: string): string { return `${AnalyticsSettingsService.SESSIONS_STARTED_KEY_PREFIX}${projectName}`; } diff --git a/lib/services/analytics/google-analytics-provider.ts b/lib/services/analytics/google-analytics-provider.ts index 8f93ab4388..0fefd753b2 100644 --- a/lib/services/analytics/google-analytics-provider.ts +++ b/lib/services/analytics/google-analytics-provider.ts @@ -9,8 +9,7 @@ export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider { constructor(private clientId: string, private $staticConfig: IStaticConfig, - private $hostInfo: IHostInfo, - private $osInfo: IOsInfo, + private $analyticsSettingsService: IAnalyticsSettingsService, private $logger: ILogger) { } @@ -32,7 +31,7 @@ export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider { tid: gaTrackingId, cid: this.clientId, headers: { - ["User-Agent"]: this.getUserAgentString() + ["User-Agent"]: this.$analyticsSettingsService.getUserAgentString(`tnsCli/${this.$staticConfig.version}`) } }); @@ -116,39 +115,6 @@ export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider { }); }); } - - private getUserAgentString(): string { - let osString = ""; - const osRelease = this.$osInfo.release(); - - if (this.$hostInfo.isWindows) { - osString = `Windows NT ${osRelease}`; - } else if (this.$hostInfo.isDarwin) { - osString = `Macintosh`; - const macRelease = this.getMacOSReleaseVersion(osRelease); - if (macRelease) { - osString += `; Intel Mac OS X ${macRelease}`; - } - } else { - osString = `Linux x86`; - if (this.$osInfo.arch() === "x64") { - osString += "_64"; - } - } - - const userAgent = `tnsCli/${this.$staticConfig.version} (${osString}; ${this.$osInfo.arch()})`; - - return userAgent; - } - - private getMacOSReleaseVersion(osRelease: string): string { - // https://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history - // Each macOS version is labeled 10., where it looks like is taken from the major version returned by os.release() (16.x.x for example) and subtracting 4 from it. - // So the version becomes "10.12" in this case. - // Could be improved by spawning `system_profiler SPSoftwareDataType` and getting the System Version line from the result. - const majorVersion = osRelease && _.first(osRelease.split(".")); - return majorVersion && `10.${+majorVersion - 4}`; - } } $injector.register("googleAnalyticsProvider", GoogleAnalyticsProvider);