Skip to content

Commit ae9507a

Browse files
committed
Set userId for telemetry events
1 parent 4c07ba2 commit ae9507a

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

Diff for: extensions/gitpod/src/authentication.ts

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export default class GitpodAuthenticationProvider extends Disposable implements
5656
// Contains the current state of the sessions we have available.
5757
this._sessionsPromise = this.readSessions();
5858

59+
// Update telemetry userId
60+
this._sessionsPromise.then(sessions => this._telemetry.setUserId(sessions.length ? sessions[0].id : undefined));
61+
5962
this._register(vscode.authentication.registerAuthenticationProvider('gitpod', 'Gitpod', this, { supportsMultipleAccounts: false }));
6063
this._register(this.context.secrets.onDidChange(() => this.checkForUpdates()));
6164
}
@@ -106,6 +109,9 @@ export default class GitpodAuthenticationProvider extends Disposable implements
106109
if (added.length || removed.length) {
107110
this._sessionChangeEmitter.fire({ added, removed, changed: [] });
108111
}
112+
113+
// Update telemetry userId
114+
this._telemetry.setUserId(storedSessions.length ? storedSessions[0].id : undefined);
109115
}
110116

111117
private async readSessions(): Promise<vscode.AuthenticationSession[]> {

Diff for: extensions/gitpod/src/telemetryReporter.ts

+41-31
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,54 @@ import SegmentAnalytics from 'analytics-node';
33
import * as os from 'os';
44
import * as vscode from 'vscode';
55

6-
const analyticsClientFactory = async (key: string): Promise<BaseTelemetryClient> => {
7-
let segmentAnalyticsClient = new SegmentAnalytics(key);
8-
9-
// Sets the analytics client into a standardized form
10-
const telemetryClient: BaseTelemetryClient = {
11-
logEvent: (eventName: string, data?: AppenderData) => {
12-
try {
13-
segmentAnalyticsClient.track({
14-
anonymousId: vscode.env.machineId,
15-
event: eventName,
16-
properties: data?.properties
17-
});
18-
} catch (e: any) {
19-
throw new Error('Failed to log event to app analytics!\n' + e.message);
20-
}
21-
},
22-
logException: (_exception: Error, _data?: AppenderData) => {
23-
throw new Error('Failed to log exception to app analytics!\n');
24-
},
25-
flush: async () => {
26-
try {
27-
// Types are oudated, flush does return a promise
28-
await segmentAnalyticsClient.flush();
29-
} catch (e: any) {
30-
throw new Error('Failed to flush app analytics!\n' + e.message);
31-
}
32-
}
33-
};
34-
return telemetryClient;
35-
};
6+
367

378
export default class TelemetryReporter extends BaseTelemetryReporter {
9+
10+
private _userId: string | undefined;
11+
3812
constructor(extensionId: string, extensionVersion: string, key: string) {
39-
const appender = new BaseTelemetryAppender(key, (key) => analyticsClientFactory(key));
13+
const appender = new BaseTelemetryAppender(key, (key) => this.analyticsClientFactory(key));
4014
super(extensionId, extensionVersion, appender, {
4115
release: os.release(),
4216
platform: os.platform(),
4317
architecture: os.arch(),
4418
});
4519
}
20+
21+
setUserId(id: string | undefined) {
22+
this._userId = id;
23+
}
24+
25+
async analyticsClientFactory(key: string): Promise<BaseTelemetryClient> {
26+
let segmentAnalyticsClient = new SegmentAnalytics(key);
27+
28+
// Sets the analytics client into a standardized form
29+
const telemetryClient: BaseTelemetryClient = {
30+
logEvent: (eventName: string, data?: AppenderData) => {
31+
try {
32+
segmentAnalyticsClient.track({
33+
userId: this._userId,
34+
anonymousId: vscode.env.machineId,
35+
event: eventName,
36+
properties: data?.properties
37+
});
38+
} catch (e: any) {
39+
throw new Error('Failed to log event to app analytics!\n' + e.message);
40+
}
41+
},
42+
logException: (_exception: Error, _data?: AppenderData) => {
43+
throw new Error('Failed to log exception to app analytics!\n');
44+
},
45+
flush: async () => {
46+
try {
47+
// Types are oudated, flush does return a promise
48+
await segmentAnalyticsClient.flush();
49+
} catch (e: any) {
50+
throw new Error('Failed to flush app analytics!\n' + e.message);
51+
}
52+
}
53+
};
54+
return telemetryClient;
55+
}
4656
}

0 commit comments

Comments
 (0)