Skip to content

Expose getUserAgentString from the analyticsSettingsService #3194

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 1 commit into from
Nov 2, 2017
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
18 changes: 18 additions & 0 deletions PublicAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion lib/common
Submodule common updated 1 files
+7 −0 declarations.d.ts
36 changes: 36 additions & 0 deletions lib/services/analytics-settings-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
Expand Down Expand Up @@ -38,6 +40,40 @@ class AnalyticsSettingsService implements IAnalyticsSettingsService {
return this.$userSettingsService.saveSetting<number>(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.<version>, where it looks like <versions> 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}`;
}
Expand Down
38 changes: 2 additions & 36 deletions lib/services/analytics/google-analytics-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

Expand All @@ -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}`)
}
});

Expand Down Expand Up @@ -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.<version>, where it looks like <versions> 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);