-
Notifications
You must be signed in to change notification settings - Fork 0
/
yandexMetric.js
85 lines (77 loc) · 2.12 KB
/
yandexMetric.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* global ym */
const defaultConfig = {
clickmap: true,
trackLinks: true,
accurateTrackBounce: true,
webvisor: true,
}
function ymNotLoaded(scriptSrc) {
if (scriptSrc) {
const scripts = document.querySelectorAll('script[src]')
const scriptLoaded = !!Object.keys(scripts).filter((key) => (scripts[key].src || '') === scriptSrc).length
return !scriptLoaded
}
return typeof ym === 'undefined'
}
let isYmInitialized = false
/**
* Yandex metric plugin
* @param pluginConfig
* @return {*}
*
* @example
* yandexMetric({
* ymId: 12345678
* })
*/
const yandexMetric = (pluginConfig = {}) => {
return {
name: 'yandex-metric',
config: {
...defaultConfig,
...pluginConfig,
},
initialize: ({ config }) => {
if (!config.ymId) {
throw new Error('No YM ymId defined')
}
const scriptSrc = 'https://mc.yandex.ru/metrika/tag.js'
if (ymNotLoaded(scriptSrc)) {
/* eslint-disable */
(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
m[i].l=1*new Date();k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
(window, document, "script", scriptSrc, "ym");
/* eslint-enable */
}
if (!isYmInitialized) {
const { ymId, id, ...rest } = config
ym(ymId, 'init', rest)
}
isYmInitialized = true
},
page: ({ payload, config }) => {
if (ymNotLoaded()) {
return
}
const { properties } = payload
ym(config.ymId, 'hit', properties.url, { title: properties.title, referer: properties.referrer })
},
track: ({ payload, config }) => {
if (ymNotLoaded()) {
return
}
const { event } = payload
ym(config.ymId, 'reachGoal', event)
},
identify: ({ payload, config }) => {
if (ymNotLoaded()) {
return
}
const { id, ...rest } = payload.traits
ym(config.ymId, 'userParams', { UserID: id, ...rest })
ym(config.ymId, 'params', { UserID: id, ...rest })
},
loaded: () => !ymNotLoaded(),
}
}
export { yandexMetric }