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
+26
View File
@@ -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
View File
@@ -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)
}
}