Feature(custom): add tk analytic

This commit is contained in:
Kuingsmile
2023-10-13 10:11:49 +08:00
parent 567af4588b
commit e8ea905a94
7 changed files with 94 additions and 2 deletions

View File

@@ -18,7 +18,7 @@ import {
import picgo from '@core/picgo'
import db from '~/main/apis/core/datastore'
import windowManager from 'apis/app/window/windowManager'
import { showNotification, getClipboardFilePath } from '~/main/utils/common'
import { showNotification, getClipboardFilePath, calcDurationRange } from '~/main/utils/common'
import logger from '@core/picgo/logger'
import { T } from '~/main/i18n'
import { CLIPBOARD_IMAGE_FOLDER } from '~/universal/utils/static'
@@ -30,7 +30,8 @@ import { IWindowList } from '#/types/enum'
import { IPicGo } from 'piclist'
import {
GET_RENAME_FILE_NAME,
RENAME_FILE_NAME
RENAME_FILE_NAME,
TALKING_DATA_EVENT
} from '~/universal/events/constants'
const waitForRename = (window: BrowserWindow, id: number): Promise<string|null> => {
@@ -48,6 +49,20 @@ const waitForRename = (window: BrowserWindow, id: number): Promise<string|null>
})
}
const handleTalkingData = (webContents: WebContents, options: IAnalyticsData) => {
const data: ITalkingDataOptions = {
EventId: 'upload',
Label: options.type,
MapKv: {
by: options.fromClipboard ? 'clipboard' : 'files', // 上传剪贴板图片还是选择的文文件
count: options.count, // 上传的数量
duration: calcDurationRange(options.duration || 0), // 上传耗时
type: options.type
}
}
webContents.send(TALKING_DATA_EVENT, data)
}
class Uploader {
private webContents: WebContents | null = null
// private uploading: boolean = false
@@ -142,8 +157,17 @@ class Uploader {
async upload (img?: IUploadOption): Promise<ImgInfo[]|false> {
try {
const startTime = Date.now()
const output = await picgo.upload(img)
if (Array.isArray(output) && output.some((item: ImgInfo) => item.imgUrl)) {
if (this.webContents) {
handleTalkingData(this.webContents, {
fromClipboard: !img,
type: db.get('picBed.uploader') || db.get('picBed.current') || 'smms',
count: img ? img.length : 1,
duration: Date.now() - startTime
} as IAnalyticsData)
}
output.forEach((item: ImgInfo) => {
item.config = JSON.parse(JSON.stringify(db.get(`picBed.${item.type}`)))
})

View File

@@ -57,6 +57,30 @@ export const showMessageBox = (options: any) => {
})
}
export const calcDurationRange = (duration: number) => {
if (duration < 1000) {
return 500
} else if (duration < 1500) {
return 1000
} else if (duration < 3000) {
return 2000
} else if (duration < 5000) {
return 3000
} else if (duration < 7000) {
return 5000
} else if (duration < 10000) {
return 8000
} else if (duration < 12000) {
return 10000
} else if (duration < 20000) {
return 15000
} else if (duration < 30000) {
return 20000
}
// max range
return 100000
}
/**
* macOS public.file-url will get encoded file path,
* so we need to decode it