Skip to content

Commit c791040

Browse files
Expose getUserAgentString from the analyticsSettingsService (#3194)
1 parent 666c9b2 commit c791040

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

PublicAPI.md

+18
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,24 @@ tns.analyticsSettingsService.getClientId()
878878
.then(clientId => console.log(clientId));
879879
```
880880
881+
### getUserAgentString
882+
The `getUserAgentString` method allows retrieving a user agent string identifying the current system
883+
884+
* Definition:
885+
```TypeScript
886+
/**
887+
* Gets user agent string identifing the current system in the following format: `${identifier} (${systemInfo}) ${osArch}`
888+
* @param {string} identifier The product identifier.
889+
* @returns {string} The user agent string.
890+
*/
891+
getUserAgentString(identifier: string): string;
892+
```
893+
894+
* Usage:
895+
```JavaScript
896+
const userAgentString = tns.analyticsSettingsService.getUserAgentString("tns/3.3.0");
897+
```
898+
881899
## How to add a new method to Public API
882900
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.
883901
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`.

lib/services/analytics-settings-service.ts

+36
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class AnalyticsSettingsService implements IAnalyticsSettingsService {
66

77
constructor(private $userSettingsService: UserSettings.IUserSettingsService,
88
private $staticConfig: IStaticConfig,
9+
private $hostInfo: IHostInfo,
10+
private $osInfo: IOsInfo,
911
private $logger: ILogger) { }
1012

1113
public async canDoRequest(): Promise<boolean> {
@@ -38,6 +40,40 @@ class AnalyticsSettingsService implements IAnalyticsSettingsService {
3840
return this.$userSettingsService.saveSetting<number>(this.getSessionsProjectKey(projectName), count);
3941
}
4042

43+
@exported("analyticsSettingsService")
44+
public getUserAgentString(identifier: string): string {
45+
let osString = "";
46+
const osRelease = this.$osInfo.release();
47+
48+
if (this.$hostInfo.isWindows) {
49+
osString = `Windows NT ${osRelease}`;
50+
} else if (this.$hostInfo.isDarwin) {
51+
osString = `Macintosh`;
52+
const macRelease = this.getMacOSReleaseVersion(osRelease);
53+
if (macRelease) {
54+
osString += `; Intel Mac OS X ${macRelease}`;
55+
}
56+
} else {
57+
osString = `Linux x86`;
58+
if (this.$osInfo.arch() === "x64") {
59+
osString += "_64";
60+
}
61+
}
62+
63+
const userAgent = `${identifier} (${osString}; ${this.$osInfo.arch()})`;
64+
65+
return userAgent;
66+
}
67+
68+
private getMacOSReleaseVersion(osRelease: string): string {
69+
// https://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history
70+
// 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.
71+
// So the version becomes "10.12" in this case.
72+
// Could be improved by spawning `system_profiler SPSoftwareDataType` and getting the System Version line from the result.
73+
const majorVersion = osRelease && _.first(osRelease.split("."));
74+
return majorVersion && `10.${+majorVersion - 4}`;
75+
}
76+
4177
private getSessionsProjectKey(projectName: string): string {
4278
return `${AnalyticsSettingsService.SESSIONS_STARTED_KEY_PREFIX}${projectName}`;
4379
}

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

+2-36
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider {
99

1010
constructor(private clientId: string,
1111
private $staticConfig: IStaticConfig,
12-
private $hostInfo: IHostInfo,
13-
private $osInfo: IOsInfo,
12+
private $analyticsSettingsService: IAnalyticsSettingsService,
1413
private $logger: ILogger) {
1514
}
1615

@@ -32,7 +31,7 @@ export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider {
3231
tid: gaTrackingId,
3332
cid: this.clientId,
3433
headers: {
35-
["User-Agent"]: this.getUserAgentString()
34+
["User-Agent"]: this.$analyticsSettingsService.getUserAgentString(`tnsCli/${this.$staticConfig.version}`)
3635
}
3736
});
3837

@@ -116,39 +115,6 @@ export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider {
116115
});
117116
});
118117
}
119-
120-
private getUserAgentString(): string {
121-
let osString = "";
122-
const osRelease = this.$osInfo.release();
123-
124-
if (this.$hostInfo.isWindows) {
125-
osString = `Windows NT ${osRelease}`;
126-
} else if (this.$hostInfo.isDarwin) {
127-
osString = `Macintosh`;
128-
const macRelease = this.getMacOSReleaseVersion(osRelease);
129-
if (macRelease) {
130-
osString += `; Intel Mac OS X ${macRelease}`;
131-
}
132-
} else {
133-
osString = `Linux x86`;
134-
if (this.$osInfo.arch() === "x64") {
135-
osString += "_64";
136-
}
137-
}
138-
139-
const userAgent = `tnsCli/${this.$staticConfig.version} (${osString}; ${this.$osInfo.arch()})`;
140-
141-
return userAgent;
142-
}
143-
144-
private getMacOSReleaseVersion(osRelease: string): string {
145-
// https://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history
146-
// 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.
147-
// So the version becomes "10.12" in this case.
148-
// Could be improved by spawning `system_profiler SPSoftwareDataType` and getting the System Version line from the result.
149-
const majorVersion = osRelease && _.first(osRelease.split("."));
150-
return majorVersion && `10.${+majorVersion - 4}`;
151-
}
152118
}
153119

154120
$injector.register("googleAnalyticsProvider", GoogleAnalyticsProvider);

0 commit comments

Comments
 (0)