Skip to content

Commit 0892588

Browse files
authored
chore: move device_id to common properties (#1038)
1 parent deb0a41 commit 0892588

File tree

4 files changed

+60
-48
lines changed

4 files changed

+60
-48
lines changed

src/explorer/helpTree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ export default class HelpTree
116116
iconName: 'report',
117117
});
118118

119-
const telemetryUserIdentity = this._telemetryService?.userIdentity;
119+
const anonymousId = this._telemetryService?.anonymousId;
120120

121121
const atlas = new HelpLinkTreeItem({
122122
title: 'Create Free Atlas Cluster',
123-
url: LINKS.createAtlasCluster(telemetryUserIdentity?.anonymousId ?? ''),
123+
url: LINKS.createAtlasCluster(anonymousId ?? ''),
124124
linkId: 'freeClusterCTA',
125125
iconName: 'atlas',
126126
useRedirect: true,

src/telemetry/telemetryService.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ export class TelemetryService {
4242
public _segmentKey?: string; // The segment API write key.
4343
private eventBuffer: TelemetryEvent[] = [];
4444
private isBufferingEvents = true;
45-
public userIdentity: {
46-
anonymousId: string;
47-
deviceId?: string;
48-
};
49-
private resolveDeviceId: ((value: string) => void) | undefined;
45+
public readonly anonymousId: string;
5046
private readonly _context: vscode.ExtensionContext;
5147
private readonly _shouldTrackTelemetry: boolean; // When tests run the extension, we don't want to track telemetry.
5248

49+
public deviceId: string | undefined;
50+
private resolveDeviceId: ((value: string) => void) | undefined;
51+
5352
constructor(
5453
storageController: StorageController,
5554
context: vscode.ExtensionContext,
@@ -58,9 +57,11 @@ export class TelemetryService {
5857
const { anonymousId } = storageController.getUserIdentity();
5958
this._context = context;
6059
this._shouldTrackTelemetry = shouldTrackTelemetry || false;
61-
this.userIdentity = {
62-
anonymousId,
63-
};
60+
this.anonymousId = anonymousId;
61+
}
62+
63+
public getCommonProperties(): Record<string, string | undefined> {
64+
return { extension_version: `${version}`, device_id: this.deviceId };
6465
}
6566

6667
private async readSegmentKey(): Promise<string | undefined> {
@@ -97,10 +98,18 @@ export class TelemetryService {
9798
flushInterval: 10000, // 10 seconds is the default libraries' value.
9899
});
99100

100-
this.userIdentity = await this.getTelemetryUserIdentity();
101-
this._segmentAnalytics.identify(this.userIdentity);
101+
this.deviceId = await this.getDeviceId();
102+
103+
const userIdentity = {
104+
anonymousId: this.anonymousId,
105+
traits: {
106+
device_id: this.deviceId,
107+
},
108+
};
109+
110+
this._segmentAnalytics.identify(userIdentity);
102111
this.isBufferingEvents = false;
103-
log.info('Segment analytics activated', this.userIdentity);
112+
log.info('Segment analytics activated', userIdentity);
104113

105114
// Process buffered events
106115
let event: TelemetryEvent | undefined;
@@ -158,11 +167,11 @@ export class TelemetryService {
158167
}
159168

160169
this._segmentAnalyticsTrack({
161-
...this.userIdentity,
170+
anonymousId: this.anonymousId,
162171
event: event.type,
163172
properties: {
173+
...this.getCommonProperties(),
164174
...event.properties,
165-
extension_version: `${version}`,
166175
},
167176
});
168177
} catch (e) {
@@ -180,18 +189,15 @@ export class TelemetryService {
180189
this.track(new NewConnectionTelemetryEvent(connectionTelemetryProperties));
181190
}
182191

183-
private async getTelemetryUserIdentity(): Promise<typeof this.userIdentity> {
192+
private async getDeviceId(): Promise<string> {
184193
const { value: deviceId, resolve: resolveDeviceId } = getDeviceId({
185194
getMachineId: (): Promise<string> => nodeMachineId.machineId(true),
186195
isNodeMachineId: true,
187196
});
188197

189198
this.resolveDeviceId = resolveDeviceId;
190199

191-
return {
192-
anonymousId: this.userIdentity.anonymousId,
193-
deviceId: await deviceId,
194-
};
200+
return deviceId;
195201
}
196202

197203
trackParticipantError(err: any, command: ParticipantResponseType): void {

src/test/suite/explorer/helpExplorer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ suite('Help Explorer Test Suite', function () {
4242
assert.strictEqual(atlasHelpItem.label, 'Create Free Atlas Cluster');
4343
assert.strictEqual(atlasHelpItem.url.includes('mongodb.com'), true);
4444
const { anonymousId } =
45-
mdbTestExtension.testExtensionController._telemetryService.userIdentity;
45+
mdbTestExtension.testExtensionController._telemetryService;
4646
assert.strictEqual(
4747
new URL(atlasHelpItem.url).searchParams.get('ajs_aid'),
4848
anonymousId,

src/test/suite/telemetry/telemetryService.test.ts

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ suite('Telemetry Controller Test Suite', () => {
4040
mdbTestExtension.testExtensionController._telemetryService;
4141

4242
let dataServiceStub: DataService;
43+
let fakeSegmentAnalyticsTrack: SinonSpy;
44+
45+
const testDeviceId = 'test-device-id';
4346
const telemetryIdentity = {
44-
...testTelemetryService.userIdentity,
45-
deviceId: 'testDeviceId',
47+
anonymousId: testTelemetryService.anonymousId,
48+
};
49+
const commonProperties = {
50+
extension_version: version,
51+
device_id: testDeviceId,
4652
};
47-
48-
let fakeSegmentAnalyticsTrack: SinonSpy;
4953

5054
const sandbox = sinon.createSandbox();
5155

@@ -70,9 +74,9 @@ suite('Telemetry Controller Test Suite', () => {
7074
sandbox.replace(
7175
mdbTestExtension.testExtensionController._telemetryService,
7276
// @ts-expect-error This is a private method
73-
'getTelemetryUserIdentity',
77+
'getDeviceId',
7478
() => {
75-
return Promise.resolve(telemetryIdentity);
79+
return Promise.resolve(testDeviceId);
7680
},
7781
);
7882
sandbox.replace(
@@ -143,7 +147,7 @@ suite('Telemetry Controller Test Suite', () => {
143147
event: 'Command Run',
144148
properties: {
145149
command: 'mdb.addConnection',
146-
extension_version: version,
150+
...commonProperties,
147151
},
148152
}),
149153
);
@@ -164,7 +168,7 @@ suite('Telemetry Controller Test Suite', () => {
164168
is_used_command_palette: true,
165169
is_used_saved_connection: false,
166170
vscode_mdb_extension_version: version,
167-
extension_version: version,
171+
...commonProperties,
168172
},
169173
}),
170174
);
@@ -185,7 +189,7 @@ suite('Telemetry Controller Test Suite', () => {
185189
is_used_command_palette: false,
186190
is_used_saved_connection: false,
187191
vscode_mdb_extension_version: version,
188-
extension_version: version,
192+
...commonProperties,
189193
},
190194
}),
191195
);
@@ -206,7 +210,7 @@ suite('Telemetry Controller Test Suite', () => {
206210
is_used_command_palette: false,
207211
is_used_saved_connection: true,
208212
vscode_mdb_extension_version: version,
209-
extension_version: version,
213+
...commonProperties,
210214
},
211215
}),
212216
);
@@ -225,7 +229,7 @@ suite('Telemetry Controller Test Suite', () => {
225229
properties: {
226230
source: 'treeview',
227231
success: true,
228-
extension_version: version,
232+
...commonProperties,
229233
},
230234
}),
231235
);
@@ -263,7 +267,7 @@ suite('Telemetry Controller Test Suite', () => {
263267
type: 'other',
264268
partial: false,
265269
error: false,
266-
extension_version: version,
270+
...commonProperties,
267271
},
268272
}),
269273
);
@@ -283,7 +287,7 @@ suite('Telemetry Controller Test Suite', () => {
283287
event: 'Playground Loaded',
284288
properties: {
285289
file_type: 'mongodb',
286-
extension_version: version,
290+
...commonProperties,
287291
},
288292
}),
289293
);
@@ -302,7 +306,7 @@ suite('Telemetry Controller Test Suite', () => {
302306
event: 'Playground Loaded',
303307
properties: {
304308
file_type: 'mongodbjs',
305-
extension_version: version,
309+
...commonProperties,
306310
},
307311
}),
308312
);
@@ -321,7 +325,7 @@ suite('Telemetry Controller Test Suite', () => {
321325
event: 'Playground Saved',
322326
properties: {
323327
file_type: 'mongodbjs',
324-
extension_version: version,
328+
...commonProperties,
325329
},
326330
}),
327331
);
@@ -339,7 +343,7 @@ suite('Telemetry Controller Test Suite', () => {
339343
properties: {
340344
screen: 'helpPanel',
341345
link_id: 'linkId',
342-
extension_version: version,
346+
...commonProperties,
343347
},
344348
}),
345349
);
@@ -358,7 +362,7 @@ suite('Telemetry Controller Test Suite', () => {
358362
properties: {
359363
language: 'java',
360364
with_driver_syntax: false,
361-
extension_version: version,
365+
...commonProperties,
362366
},
363367
}),
364368
);
@@ -377,7 +381,7 @@ suite('Telemetry Controller Test Suite', () => {
377381
event: 'Playground Created',
378382
properties: {
379383
playground_type: 'search',
380-
extension_version: version,
384+
...commonProperties,
381385
},
382386
}),
383387
);
@@ -402,7 +406,7 @@ suite('Telemetry Controller Test Suite', () => {
402406
event: 'Playground Created',
403407
properties: {
404408
playground_type: 'createCollection',
405-
extension_version: version,
409+
...commonProperties,
406410
},
407411
}),
408412
);
@@ -419,7 +423,7 @@ suite('Telemetry Controller Test Suite', () => {
419423
event: 'Playground Created',
420424
properties: {
421425
playground_type: 'createDatabase',
422-
extension_version: version,
426+
...commonProperties,
423427
},
424428
}),
425429
);
@@ -437,7 +441,7 @@ suite('Telemetry Controller Test Suite', () => {
437441
event: 'Playground Created',
438442
properties: {
439443
playground_type: 'index',
440-
extension_version: version,
444+
...commonProperties,
441445
},
442446
}),
443447
);
@@ -474,7 +478,7 @@ suite('Telemetry Controller Test Suite', () => {
474478
event: 'Playground Created',
475479
properties: {
476480
playground_type: 'cloneDocument',
477-
extension_version: version,
481+
...commonProperties,
478482
},
479483
}),
480484
);
@@ -489,7 +493,7 @@ suite('Telemetry Controller Test Suite', () => {
489493
event: 'Playground Created',
490494
properties: {
491495
playground_type: 'crud',
492-
extension_version: version,
496+
...commonProperties,
493497
},
494498
}),
495499
);
@@ -506,7 +510,7 @@ suite('Telemetry Controller Test Suite', () => {
506510
event: 'Playground Created',
507511
properties: {
508512
playground_type: 'crud',
509-
extension_version: version,
513+
...commonProperties,
510514
},
511515
}),
512516
);
@@ -523,7 +527,7 @@ suite('Telemetry Controller Test Suite', () => {
523527
event: 'Playground Created',
524528
properties: {
525529
playground_type: 'crud',
526-
extension_version: version,
530+
...commonProperties,
527531
},
528532
}),
529533
);
@@ -669,8 +673,10 @@ suite('Telemetry Controller Test Suite', () => {
669673
const verifyEvent = (call: sinon.SinonSpyCall): void => {
670674
const event = call.args[0] as SegmentProperties;
671675
expect(event.event).to.equal('Side Panel Opened');
672-
expect(event.properties).to.have.keys(['extension_version']);
673-
expect(Object.keys(event.properties)).to.have.length(1);
676+
expect(event.properties).to.have.keys(commonProperties);
677+
expect(Object.keys(event.properties)).to.have.length(
678+
Object.keys(commonProperties).length,
679+
);
674680
};
675681

676682
expect(fakeSegmentAnalyticsTrack.getCalls()).has.length(0);

0 commit comments

Comments
 (0)