-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
92 lines (83 loc) · 2.5 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/// Yandex Metrika for Next.JS
function convertParam(boolValue, defaultValue) {
return (boolValue === undefined ? defaultValue : boolValue)
? "true"
: "false";
}
function YandexMetrikaTag({
yid,
clickmap=true,
trackLinks=true,
accurateTrackBounce=true,
webvisor=true,
}) {
/// Tag version of the Yandex Metrika.
/// Used when there is support for the JavaScript to fully track user.
/// Will cause loading and injecting tag script, and activate Yandex Metrika by constructor.
// Tag options, see your Yandex Metrika recommendations for setup.
clickmap = convertParam(clickmap, true);
trackLinks = convertParam(trackLinks, true);
accurateTrackBounce = convertParam(accurateTrackBounce, true);
webvisor = convertParam(webvisor, true);
// Script that injects Yandex Metrika tag script.
const scriptInjectorHTML = `
(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", "https://mc.yandex.ru/metrika/tag.js", "ym");
`;
return (
<script
dangerouslySetInnerHTML={{
__html: `
${scriptInjectorHTML}
ym(${yid}, "init", {
clickmap:${clickmap},
trackLinks:${trackLinks},
accurateTrackBounce:${accurateTrackBounce},
webvisor:${webvisor}
});
`,
}}
/>
);
}
function YandexMetrikaPixel({ yid }) {
/// Pixel version of the Yandex Metrika.
/// Used when there is no JavaScript on the target browser.
/// This will cause the Yandex Metrika to track user by calling loading of the pixel image (with target params).
// Target source to load pixel from.
const pixelSource = `https://mc.yandex.ru/watch/${yid}`;
/* eslint-disable @next/next/no-img-element */
return (
<noscript>
<div>
<img
src={pixelSource}
style={{ position: "absolute", left: "-9999px" }}
alt=""
/>
</div>
</noscript>
);
}
export default function YandexMetrika({
yid,
clickmap=true,
trackLinks=true,
accurateTrackBounce=true,
webvisor=true,
}) {
/// Yandex Metrika service.
return (
<>
<YandexMetrikaTag
yid={yid}
clickmap={clickmap}
trackLinks={trackLinks}
accuracyTrackBounce={accurateTrackBounce}
webvisor={webvisor}
/>
<YandexMetrikaPixel yid={yid} />
</>
);
}