@@ -3,44 +3,54 @@ 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
- } ;
6
+
36
7
37
8
export default class TelemetryReporter extends BaseTelemetryReporter {
9
+
10
+ private _userId : string | undefined ;
11
+
38
12
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 ) ) ;
40
14
super ( extensionId , extensionVersion , appender , {
41
15
release : os . release ( ) ,
42
16
platform : os . platform ( ) ,
43
17
architecture : os . arch ( ) ,
44
18
} ) ;
45
19
}
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
+ }
46
56
}
0 commit comments