-
Notifications
You must be signed in to change notification settings - Fork 35
/
install.js
57 lines (51 loc) · 1.64 KB
/
install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import autoPageview from './directives/auto-pageview'
import trackEvent from '././directives/track-event'
import trackPageview from '././directives/track-pageview'
export default function install (Vue, options) {
if (this.install.installed) return
if (options.debug) {
this.debug = console.log
} else {
this.debug = () => {}
}
let siteId = null
if (typeof options === 'object') {
siteId = options.siteId
if (options.autoPageview !== false) {
options.autoPageview = true
}
} else {
siteId = options
}
if (!siteId) {
return console.error(' siteId is missing')
}
this.install.installed = true
// insert baidu analystics scripts
const script = document.createElement('script')
const src = `https://hm.baidu.com/hm.js?` + siteId
const realSrc = options.src || src
script.innerHTML = 'var _hmt = _hmt || []; (function(){var hm = document.createElement(\'script\');hm.src="' +
realSrc +
'";var s = document.getElementsByTagName("script")[0];s.parentNode.insertBefore(hm, s);})()'
script.onload = () => {
// if the global object is exist, resolve the promise, otherwise reject it
if (window._hmt) {
this._resolve()
} else {
console.error('loading ba statistics script failed, please check src and siteId')
return this._reject()
}
this._cache.forEach((cache) => {
window._hmt.push(cache)
})
this._cache = []
}
document.body.appendChild(script)
Object.defineProperty(Vue.prototype, '$ba', {
get: () => this
})
Vue.directive('auto-pageview', autoPageview)
Vue.directive('track-event', trackEvent)
Vue.directive('track-pageview', trackPageview)
}