update deviceId code

This commit is contained in:
Andrew Pareles 2025-01-22 18:13:46 -08:00
parent 6327c584cc
commit da6636a948

View file

@ -9,7 +9,8 @@ import { generateUuid } from '../../../base/common/uuid.js';
import { IEnvironmentMainService } from '../../environment/electron-main/environmentMainService.js';
import { IProductService } from '../../product/common/productService.js';
import { IStorageMainService } from '../../storage/electron-main/storageMainService.js';
import { StorageScope, StorageTarget } from '../../storage/common/storage.js';
import { IApplicationStorageMainService, IStorageMainService } from '../../storage/electron-main/storageMainService.js';
import { IMetricsService } from '../common/metricsService.js';
import { PostHog } from 'posthog-node'
@ -17,7 +18,6 @@ import { PostHog } from 'posthog-node'
const os = isWindows ? 'windows' : isMacintosh ? 'mac' : isLinux ? 'linux' : null
const VOID_MACHINE_STORAGE_KEY = 'void.machineId'
export class MetricsMainService extends Disposable implements IMetricsService {
_serviceBrand: undefined;
@ -27,20 +27,52 @@ export class MetricsMainService extends Disposable implements IMetricsService {
private readonly _initProperties: object
// TODO we should eventually identify people based on email
private get machineId() {
const currVal = this._storageService.applicationStorage.get(VOID_MACHINE_STORAGE_KEY)
// helper - looks like this is stored in a .vscdb file in ~/Library/Application Support/Void
private _memoStorage(key: string, target: StorageTarget, setValIfNotExist?: string) {
const currVal = this._appStorage.get(key, StorageScope.APPLICATION)
if (currVal !== undefined) return currVal
const newVal = generateUuid()
this._storageService.applicationStorage.set(VOID_MACHINE_STORAGE_KEY, newVal)
const newVal = setValIfNotExist ?? generateUuid()
this._appStorage.store(key, newVal, StorageScope.APPLICATION, target)
return newVal
}
// this is old, eventually we can just delete this since all the keys will have been transferred over
// returns 'NULL' or the old key
private get oldId() {
// check new storage key first
const newKey = 'void.app.oldMachineId'
const newOldId = this._appStorage.get(newKey, StorageScope.APPLICATION)
if (newOldId) return newOldId
// put old key into new key if didn't already
const oldValue = this._storageService.applicationStorage.get('void.machineId') ?? 'NULL' // the old way of getting the key
this._appStorage.store(newKey, oldValue, StorageScope.APPLICATION, StorageTarget.MACHINE)
return oldValue
}
// eventually we can replace above with this
// private get oldId() {
// return this._memoStorage('void.app.oldMachineId', StorageTarget.MACHINE, 'NULL')
// }
// the main id
private get distinctId() {
const oldId = this.oldId
const setValIfNotExist = oldId === 'NULL' ? undefined : oldId
return this._memoStorage('void.app.machineId', StorageTarget.MACHINE, setValIfNotExist)
}
// just to see if there are ever multiple machineIDs per userID (instead of this, we should just track by the user's email)
private get userId() {
return this._memoStorage('void.app.userMachineId', StorageTarget.USER)
}
constructor(
@IProductService private readonly _productService: IProductService,
@IStorageMainService private readonly _storageService: IStorageMainService,
@IEnvironmentMainService private readonly _envMainService: IEnvironmentMainService,
@IApplicationStorageMainService private readonly _appStorage: IApplicationStorageMainService,
) {
super()
this.client = new PostHog('phc_UanIdujHiLp55BkUTjB1AuBXcasVkdqRwgnwRlWESH2', {
@ -53,20 +85,21 @@ export class MetricsMainService extends Disposable implements IMetricsService {
const isDevMode = !this._envMainService.isBuilt // found in abstractUpdateService.ts
// custom properties we identify
this._initProperties = {
commit,
version,
os,
quality,
distinctId: this.machineId,
distinctId: this.distinctId,
distinctIdUser: this.userId,
oldId: this.oldId,
isDevMode,
...this._getOSInfo(),
}
const identifyMessage = {
distinctId: this.machineId,
distinctId: this.distinctId,
properties: this._initProperties,
}
this.client.identify(identifyMessage)
@ -86,7 +119,7 @@ export class MetricsMainService extends Disposable implements IMetricsService {
}
capture: IMetricsService['capture'] = (event, params) => {
const capture = { distinctId: this.machineId, event, properties: params } as const
const capture = { distinctId: this.distinctId, event, properties: params } as const
// console.log('full capture:', capture)
this.client.capture(capture)
}