Feature: add baidu tongji for analytics

This commit is contained in:
PiEgg
2021-04-05 17:47:06 +08:00
parent 3fd6e4e4c8
commit f53639153e
8 changed files with 113 additions and 1 deletions
+24
View File
@@ -12,6 +12,7 @@ import { IWindowList } from 'apis/app/window/constants'
import util from 'util'
import { IPicGo } from 'picgo/dist/src/types'
import { showNotification } from '~/main/utils/common'
import { BAIDU_TONGJI_EVENT } from '~/universal/events/constants'
const waitForShow = (webcontent: WebContents) => {
return new Promise<void>((resolve, reject) => {
@@ -36,6 +37,20 @@ const waitForRename = (window: BrowserWindow, id: number): Promise<string|null>
})
}
const handleBaiduTongJi = (webContents: WebContents, options: IAnalyticsData) => {
const data: IBaiduTongJiOptions = {
category: 'upload',
action: options.fromClipboard ? 'clipboard' : 'files', // 上传剪贴板图片还是选择的文件
opt_label: JSON.stringify({
type: options.type, // 上传的图床种类
count: options.count, // 上传的图片数量
timestamp: dayjs().format('YYYYMMDDHHmmss'), // 上传完成的时间戳
duration: options.duration // 耗时
})
}
webContents.send(BAIDU_TONGJI_EVENT, data)
}
class Uploader {
private webContents: WebContents | null = null
private uploading: boolean = false
@@ -102,11 +117,20 @@ class Uploader {
}
return new Promise((resolve) => {
try {
const startTime = Date.now()
this.uploading = true
picgo.upload(img)
picgo.once('finished', ctx => {
this.uploading = false
if (ctx.output.every((item: ImgInfo) => item.imgUrl)) {
if (this.webContents) {
handleBaiduTongJi(this.webContents, {
fromClipboard: !img,
type: db.get('picBed.current') || 'smms',
count: img ? img.length : 1,
duration: Date.now() - startTime
} as IAnalyticsData)
}
resolve(ctx.output)
} else {
resolve(false)
+27 -1
View File
@@ -13,13 +13,19 @@ import server from '~/main/server'
import getPicBeds from '~/main/utils/getPicBeds'
import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler'
import bus from '@core/bus'
import { TOGGLE_SHORTKEY_MODIFIED_MODE } from '#/events/constants'
import {
TOGGLE_SHORTKEY_MODIFIED_MODE,
BAIDU_TONGJI_INIT,
BAIDU_TONGJI_CODE,
BAIDU_TONGJI_INIT_RES
} from '#/events/constants'
import {
uploadClipboardFiles,
uploadChoosedFiles
} from '~/main/apis/app/uploader/apis'
import picgoCoreIPC from './picgoCoreIPC'
import { handleCopyUrl } from '~/main/utils/common'
import axios from 'axios'
export default {
listen () {
@@ -139,6 +145,26 @@ export default {
ipcMain.on('updateServer', () => {
server.restart()
})
ipcMain.on(BAIDU_TONGJI_INIT, (evt: IpcMainEvent) => {
axios.get(`https://hm.baidu.com/hm.js?${BAIDU_TONGJI_CODE}`, {
headers: {
referer: 'https://molunerfinn.com/'
}
}).then(res => {
if (res.status === 200) {
let scriptContent: string = res.data
// thanks to https://github.com/joehecn/electron-baidu-tongji/blob/master/index.js
const source = '(h.c.b.su=h.c.b.u||document.location.href),h.c.b.u=f.protocol+"//"+document.location.host+'
if (scriptContent.includes(source)) {
const target = '(h.c.b.su=h.c.b.u||"https://"+c.dm[0]+a[1]),h.c.b.u="https://"+c.dm[0]+'
const target2 = '"https://"+c.dm[0]+window.location.pathname+window.location.hash'
scriptContent = scriptContent.replace(source, target).replace(/window.location.href/g, target2)
}
evt.sender.send(BAIDU_TONGJI_INIT_RES, scriptContent)
}
})
})
},
dispose () {}
}