Skip to content

Commit

Permalink
✨ Feature: add baidu tongji for analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Apr 5, 2021
1 parent 3fd6e4e commit f536391
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import VueLazyLoad from 'vue-lazyload'
import axios from 'axios'
import mainMixin from './renderer/utils/mainMixin'
import bus from '@/utils/bus'
import { initBaiduTongJi } from './renderer/utils/analytics'

webFrame.setVisualZoomLevelLimits(1, 1)
webFrame.setLayoutZoomLevelLimits(0, 0)
Expand Down Expand Up @@ -36,3 +37,5 @@ new Vue({
router,
render: h => h(App)
}).$mount('#app')

initBaiduTongJi()
24 changes: 24 additions & 0 deletions src/main/apis/app/uploader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 27 additions & 1 deletion src/main/events/ipcList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {}
}
26 changes: 26 additions & 0 deletions src/renderer/utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable camelcase */
import { ipcRenderer } from 'electron'
import {
BAIDU_TONGJI_INIT,
BAIDU_TONGJI_INIT_RES,
BAIDU_TONGJI_EVENT
} from '~/universal/events/constants'
import { handleBaiduTongJiEvent } from './common'

ipcRenderer.on(BAIDU_TONGJI_INIT_RES, (_, scriptContent) => {
window._hmt = window._hmt || []
const hm = document.createElement('script')
hm.text = scriptContent
const head = document.getElementsByTagName('head')[0]
head.appendChild(hm)
})

ipcRenderer.on(BAIDU_TONGJI_EVENT, (_, data: IBaiduTongJiOptions) => {
handleBaiduTongJiEvent(data)
})

export const initBaiduTongJi = () => {
setTimeout(() => {
ipcRenderer.send(BAIDU_TONGJI_INIT)
}, 0)
}
9 changes: 9 additions & 0 deletions src/renderer/utils/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const isDevelopment = process.env.NODE_ENV !== 'production'
/* eslint-disable camelcase */
export const handleBaiduTongJiEvent = (data: IBaiduTongJiOptions) => {
const { category, action, opt_label = '', opt_value = Date.now() } = data
window._hmt.push(['_trackEvent', category, action, opt_label, opt_value])
if (isDevelopment) {
console.log('baidu tongji', data)
}
}
4 changes: 4 additions & 0 deletions src/universal/events/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const SHOW_INPUT_BOX = 'SHOW_INPUT_BOX'
export const SHOW_INPUT_BOX_RESPONSE = 'SHOW_INPUT_BOX_RESPONSE'
export const TOGGLE_SHORTKEY_MODIFIED_MODE = 'TOGGLE_SHORTKEY_MODIFIED_MODE'
export const BAIDU_TONGJI_INIT = 'BAIDU_TONDJI_INIT'
export const BAIDU_TONGJI_INIT_RES = 'BAIDU_TONDJI_INIT_RES'
export const BAIDU_TONGJI_CODE = '19a7ebdbb87f2403773c7ab0cae16d21'
export const BAIDU_TONGJI_EVENT = 'BAIDU_TONGJI_EVENT'
4 changes: 4 additions & 0 deletions src/universal/types/shims-tsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ declare global {
[elem: string]: any
}
}

interface Window {
_hmt: any[]
}
}
16 changes: 16 additions & 0 deletions src/universal/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,19 @@ interface IAppNotification {
body: string
icon?: string
}

interface IBaiduTongJiOptions {
category: string
action: string
// eslint-disable-next-line camelcase
opt_label?: string
// eslint-disable-next-line camelcase
opt_value?: number
}

interface IAnalyticsData {
fromClipboard: boolean
type: string
count: number
duration?: number // 耗时
}

0 comments on commit f536391

Please sign in to comment.