@@ -18,6 +18,11 @@ import { StorageController } from '../storage';
18
18
const log = createLogger ( 'telemetry' ) ;
19
19
const { version } = require ( '../../package.json' ) ;
20
20
21
+ interface AnalyticsNodeIdentity {
22
+ userId ?: string ;
23
+ anonymousId : string ;
24
+ }
25
+
21
26
type PlaygroundTelemetryEventProperties = {
22
27
type : string | null ;
23
28
partial : boolean ;
@@ -26,7 +31,8 @@ type PlaygroundTelemetryEventProperties = {
26
31
27
32
export type SegmentProperties = {
28
33
event : string ;
29
- userId : string ;
34
+ userId ?: string ;
35
+ anonymousId : string ;
30
36
properties : unknown ;
31
37
} ;
32
38
@@ -85,7 +91,8 @@ export enum TelemetryEventTypes {
85
91
*/
86
92
export default class TelemetryService {
87
93
_segmentAnalytics ?: SegmentAnalytics ;
88
- _segmentUserID : string ; // The user uuid from the global storage.
94
+ _segmentUserId ?: string ; // Should exist only for users prior v0.9.0.
95
+ _segmentAnonymousId : string ; // The randomly generated uuid.
89
96
_segmentKey ?: string ; // The segment API write key.
90
97
91
98
private _context : vscode . ExtensionContext ;
@@ -96,9 +103,11 @@ export default class TelemetryService {
96
103
context : vscode . ExtensionContext ,
97
104
shouldTrackTelemetry ?: boolean
98
105
) {
106
+ const { userId, anonymousId } = storageController . getUserIdentity ( ) ;
99
107
this . _context = context ;
100
108
this . _shouldTrackTelemetry = shouldTrackTelemetry || false ;
101
- this . _segmentUserID = storageController . getUserID ( ) ;
109
+ this . _segmentUserId = userId ;
110
+ this . _segmentAnonymousId = anonymousId ;
102
111
this . _segmentKey = this . _readSegmentKey ( ) ;
103
112
104
113
vscode . workspace . onDidOpenTextDocument ( ( document ) => {
@@ -153,7 +162,13 @@ export default class TelemetryService {
153
162
flushInterval : 10000 // 10 seconds is the default libraries' value.
154
163
} ) ;
155
164
156
- this . _segmentAnalytics . identify ( { userId : this . _segmentUserID } ) ;
165
+ const identity : AnalyticsNodeIdentity = {
166
+ anonymousId : this . _segmentAnonymousId
167
+ } ;
168
+ if ( this . _segmentUserId ) {
169
+ identity . userId = this . _segmentUserId ;
170
+ }
171
+ this . _segmentAnalytics . identify ( identity ) ;
157
172
}
158
173
}
159
174
@@ -190,7 +205,8 @@ export default class TelemetryService {
190
205
if ( this . _isTelemetryFeatureEnabled ( ) ) {
191
206
const segmentProperties : SegmentProperties = {
192
207
event : eventType ,
193
- userId : this . _segmentUserID ,
208
+ userId : this . _segmentUserId ,
209
+ anonymousId : this . _segmentAnonymousId ,
194
210
properties : {
195
211
...properties ,
196
212
extension_version : `${ version } `
@@ -260,8 +276,8 @@ export default class TelemetryService {
260
276
return 'other' ;
261
277
}
262
278
263
- getSegmentUserId ( ) : string {
264
- return this . _segmentUserID ;
279
+ getSegmentAnonymousId ( ) : string {
280
+ return this . _segmentAnonymousId ;
265
281
}
266
282
267
283
trackPlaygroundCodeExecuted (
0 commit comments