@@ -3,44 +3,57 @@ import SegmentAnalytics from 'analytics-node';
3
3
import * as os from 'os' ;
4
4
import * as vscode from 'vscode' ;
5
5
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
- } ;
36
-
37
6
export default class TelemetryReporter extends BaseTelemetryReporter {
7
+
8
+ private _userId : string | undefined ;
9
+
38
10
constructor ( extensionId : string , extensionVersion : string , key : string ) {
39
- const appender = new BaseTelemetryAppender ( key , ( key ) => analyticsClientFactory ( key ) ) ;
11
+ let resolveTelemetryClient : ( value : Promise < BaseTelemetryClient > ) => void ;
12
+ const telemetryClientPromise = new Promise < BaseTelemetryClient > ( ( resolve , _reject ) => resolveTelemetryClient = resolve ) ;
13
+
14
+ const appender = new BaseTelemetryAppender ( key , ( ) => telemetryClientPromise ) ;
40
15
super ( extensionId , extensionVersion , appender , {
41
16
release : os . release ( ) ,
42
17
platform : os . platform ( ) ,
43
18
architecture : os . arch ( ) ,
44
19
} ) ;
20
+
21
+ resolveTelemetryClient ! ( this . analyticsClientFactory ( key ) ) ;
22
+ }
23
+
24
+ setUserId ( id : string | undefined ) {
25
+ this . _userId = id ;
26
+ }
27
+
28
+ async analyticsClientFactory ( key : string ) : Promise < BaseTelemetryClient > {
29
+ let segmentAnalyticsClient = new SegmentAnalytics ( key ) ;
30
+
31
+ // Sets the analytics client into a standardized form
32
+ const telemetryClient : BaseTelemetryClient = {
33
+ logEvent : ( eventName : string , data ?: AppenderData ) => {
34
+ try {
35
+ segmentAnalyticsClient . track ( {
36
+ userId : this . _userId ,
37
+ anonymousId : vscode . env . machineId ,
38
+ event : eventName ,
39
+ properties : data ?. properties
40
+ } ) ;
41
+ } catch ( e : any ) {
42
+ throw new Error ( 'Failed to log event to app analytics!\n' + e . message ) ;
43
+ }
44
+ } ,
45
+ logException : ( _exception : Error , _data ?: AppenderData ) => {
46
+ throw new Error ( 'Failed to log exception to app analytics!\n' ) ;
47
+ } ,
48
+ flush : async ( ) => {
49
+ try {
50
+ // Types are oudated, flush does return a promise
51
+ await segmentAnalyticsClient . flush ( ) ;
52
+ } catch ( e : any ) {
53
+ throw new Error ( 'Failed to flush app analytics!\n' + e . message ) ;
54
+ }
55
+ }
56
+ } ;
57
+ return telemetryClient ;
45
58
}
46
59
}
0 commit comments