-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·67 lines (55 loc) · 1.77 KB
/
index.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
import path from 'path';
import { templateInitScripts, templateNoscriptInit } from "./utils";
export default function yandexMetrika (moduleOptions) {
// don't include on dev mode
if (!moduleOptions.development && process.env.NODE_ENV !== 'production') {
return;
}
const {
useCDN = false,
staticCounters,
noscript = true,
} = moduleOptions;
const libURL = !useCDN
? 'https://mc.yandex.ru/metrika/tag.js'
: 'https://cdn.jsdelivr.net/npm/yandex-metrica-watch/tag.js';
const bootCounters = [].concat( staticCounters || [] );
const date = new Date();
// yandex metrika init script
let metrikaContent = `
// @meta
// @rabota/yandex-metrika
// https://github.com/RabotaRu/yandex-metrika
// ${date.toISOString()}
(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", "${libURL}", "ym");
`;
// include counters init script
metrikaContent += templateInitScripts( bootCounters );
this.options.head.__dangerouslyDisableSanitizers = [ 'script', 'noscript' ];
this.options.head.script.unshift({
innerHTML: metrikaContent.trim()
});
// include noscript
if (noscript) {
const noscripts = bootCounters.map(counter => {
return {
innerHTML: templateNoscriptInit( counter ),
body: true
};
});
this.options.head.noscript = []
.concat( this.options.head.noscript || [] )
.concat( noscripts );
}
// register plugin
this.addPlugin({
src: path.resolve(__dirname, 'plugin.js'),
ssr: true,
options: moduleOptions
});
};
module.exports.meta = require( './package.json' );