qm-bus
Version:
千米公有云业务组件库
102 lines (97 loc) • 3.13 kB
JavaScript
import GH from './gh'
import assign from 'object-assign'
window.GH_CONFIG = {
GA_ID: 'UA-62751334-4',
UMENG_ID: '1272783702',
};
let GH_MODULE = null
if (window.GH_MODULE) {
GH_MODULE = window.GH_MODULE
} else {
GH_MODULE = {}
window.GH_MODULE = GH_MODULE
let ghConfig = window.GH_CONFIG || {}
GH.init({ ga: ghConfig.GA_ID, umeng: ghConfig.UMENG_ID })
let searchNode = function (target) {
if (target == document || target == window) {
return null
}
if (target.hasAttribute && target.hasAttribute('data-gh-name')) {
return target
} else {
return searchNode(target.parentNode)
}
}
let getLocation = function (url) {
if (url) {
let _idx = url.indexOf('#')
if (_idx > -1 && _idx != url.length - 1 && url[_idx + 1] != '?') {
return url.split('?')[0]
} else {
return url
}
}
return ''
}
let collect = function (e, content) {
let reactContext = window.getReactContext ? window.getReactContext() || {} : {}
let scene = reactContext.user ? reactContext.user.sceneName : ''
const sceneEnumer = {
onlineDistribution: '云订货',
onlineMall: '云商城',
communityStore: '社区店',
cloudShop: '云小店',
o2o: '连锁总部',
cloudOrder: '云采购',
wholeSelling: '全员开店',
talentShop: '千米达人',
}
scene = sceneEnumer[scene] || ''
let menu = reactContext.currentMenu ? reactContext.currentMenu.name : ''
if (!menu) {
let _name = reactContext.route ? reactContext.route.name : ''
menu = _name || ''
}
let role = reactContext.role ? reactContext.role.name : ''
let message = content
let category = scene + '-' + role + '-' + menu
setTimeout(() => {
GH.setEvent(category, e.type, message, '', e.target ? e.target.id : '')
if (typeof __DEV__ == 'boolean' && __DEV__) {
console.log(`记录数据:category:${category},message:${message}`)
}
}, 0)
}
let events = ['click', 'mouseover', 'focus']
events.forEach(event => {
document.body['on' + event] = e => {
let node = searchNode(e.target)
if (node) {
let events = node.getAttribute('data-gh-event') || 'click'
if (events && events.indexOf(event) > -1) {
collect(e, node.getAttribute('data-gh-name'))
}
}
}
})
GH.setPageview((window.location.hash || '').replace(/^#/g, ''))
window.onhashchange = () => {
GH.setPageview((window.location.hash || '').replace(/^#/g, ''))
}
GH_MODULE = assign(GH_MODULE, GH)
GH_MODULE.setEvent = (e, content) => {
collect(e, content)
}
GH_MODULE.decorator = msg => (target, props, descriptor) => {
let origin = descriptor.initializer
descriptor.initializer = new Function(
'origin',
'collect',
'msg',
'return function(){return function(){' +
'origin.call(this).apply(this,arguments);var e = arguments[arguments.length-1];' +
'if(e.nativeEvent){e=e.nativeEvent;}if(e instanceof Event){collect(e,msg)}}}'
)(origin, collect, msg)
}
}
export default GH_MODULE