Skip to content

Commit

Permalink
refactor: Collect new client count
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Dec 9, 2023
1 parent 90fbf8b commit 771317e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions xmcl-runtime/clientToken/clientToken.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { InjectionKey } from '~/app'

export const kClientToken: InjectionKey<string> = Symbol('ClientToken')
export const kIsNewClient: InjectionKey<boolean> = Symbol('IsNewClient')
5 changes: 4 additions & 1 deletion xmcl-runtime/clientToken/pluginClientToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import { randomUUID } from 'crypto'
import { readFile, writeFile } from 'fs/promises'
import { join } from 'path'
import { LauncherAppPlugin } from '~/app'
import { kClientToken } from '~/clientToken'
import { kClientToken, kIsNewClient } from '~/clientToken'

export const pluginClientToken: LauncherAppPlugin = async (app) => {
const clientSessionFile = join(app.appDataPath, 'client_session')
let clientSession = ''
let isNew = false
try {
const session = await readFile(clientSessionFile).then(b => b.toString())
clientSession = session
} catch {
clientSession = randomUUID()
await writeFile(clientSessionFile, clientSession)
isNew = true
}

app.registry.register(kClientToken, clientSession)
app.registry.register(kIsNewClient, isNew)
}
7 changes: 5 additions & 2 deletions xmcl-runtime/telemetry/pluginTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Contracts } from 'applicationinsights'
import { randomUUID } from 'crypto'
import { LauncherAppPlugin } from '~/app'
import { IS_DEV } from '../constant'
import { kClientToken } from '~/clientToken'
import { kClientToken, kIsNewClient } from '~/clientToken'
import { kSettings } from '~/settings'
import { APP_INSIGHT_KEY, parseStack } from './telemetry'
import { InstanceService } from '~/instance'
Expand All @@ -25,6 +25,7 @@ export const pluginTelemetry: LauncherAppPlugin = async (app) => {
const sessionId = randomUUID()

const clientSession = await app.registry.get(kClientToken)
const isNewClient = await app.registry.get(kIsNewClient)
const flights = await app.registry.get(kFlights)
const stateManager = await app.registry.get(ServiceStateManager)

Expand Down Expand Up @@ -74,7 +75,9 @@ export const pluginTelemetry: LauncherAppPlugin = async (app) => {
logger.log('Telemetry client started')
client.trackEvent({
name: 'app-start',
properties: { },
properties: {
isNewClient,
},
})

app.registryDisposer(async () => {
Expand Down

0 comments on commit 771317e

Please sign in to comment.