-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
114 lines (95 loc) · 2.94 KB
/
index.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* eslint-disable import/no-unused-modules */
import waitForTarget from 'wait-for-target'
import {createDebug} from '@rambler-tech/debug'
const debug = createDebug('common:top-100')
/** Top 100 options */
export interface Top100Options {
/** Project ID */
project: number
/** Blocks data attributes */
dataset: string[]
/** Counter parameters */
params?: Record<string, any>
}
/** Top 100 counter */
export class Top100 {
/** Counter options */
options: Top100Options
/** Promise that resolves when counter is ready */
ready: Promise<void>
/** Top 100 counter constructor */
constructor(options: Top100Options) {
this.options = options
this.ready = new Promise((resolve) => {
if (process.env.NODE_ENV === 'development') {
resolve()
return
}
/* eslint-disable */
// prettier-ignore
;(function (w, d, c) {
// @ts-ignore
(w[c] = w[c] || []).push(function () {
try {
// @ts-ignore
const counter = new top100({
project: options.project,
attributes_dataset: options.dataset,
...options.params
})
// @ts-ignore
w[`top100Counter${options.project}`] = counter
// @ts-ignore
w.top100Counter = w.top100Counter || counter
} catch {}
});
// @ts-ignore
var n = d.getElementsByTagName("script")[0], s = d.createElement("script"), f = function () { n.parentNode.insertBefore(s, n); };
s.type = "text/javascript";
s.async = true;
s.src =
(d.location.protocol == "https:" ? "https:" : "http:") +
"//st.top100.ru/top100/top100.js";
s.onload = () => resolve()
s.onerror = () => resolve()
// @ts-ignore
if (w.opera == "[object Opera]") {
d.addEventListener("DOMContentLoaded", f, false);
} else { f(); }
})(window, document, "_top100q");
/* eslint-enable */
})
}
async run(method: string, ...args: any[]) {
const {project} = this.options
if (process.env.NODE_ENV === 'development') {
debug(`${method} ${project} %o`, args)
return
}
try {
const counter = await waitForTarget(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
() => window[`top100Counter${project}`],
1000
)
counter[method](...args)
} catch {}
}
/** Send custom vars */
async sendCustomVars(params: Record<string, any>) {
await this.run('sendCustomVars', params)
}
/** Track page view */
async trackPageview() {
await this.run('trackPageview')
}
/** Track event */
async trackEvent(eventName: string, eventData: Record<string, any>) {
await this.run('trackEvent', eventName, eventData)
}
/** Update counter options */
async updateOptions(options: Record<string, any>) {
await this.run('updateOptions', options)
}
}