|
8 | 8 | (function () { |
9 | 9 | 'use strict'; |
10 | 10 |
|
11 | | - var HOST = 'mc.yandex.ru'; |
12 | | - var PATH = '/watch/'; |
13 | | - var PORT = 80; |
| 11 | + var HOST = 'mc.yandex.ru'; |
| 12 | + var PATH = '/watch/'; |
| 13 | + var PORT = 80; |
14 | 14 |
|
15 | | - var querystring = require('querystring'); |
16 | | - var http = require('http'); |
| 15 | + var querystring = require('querystring'); |
| 16 | + var http = require('http'); |
17 | 17 |
|
18 | 18 | /** |
19 | 19 | * Конструктор счётчика Метрики |
20 | 20 | * @constructor |
21 | 21 | * |
22 | 22 | * @param {Object} settings - настройки счётчика |
23 | 23 | */ |
24 | | - var Counter = function (settings) { |
| 24 | + var Counter = function (settings) { |
25 | 25 | // Номер счётчика |
26 | | - this._id = settings.id; |
| 26 | + this._id = settings.id; |
27 | 27 |
|
28 | 28 | // Тип счётчика: 0 - обычный счётчик, 1 - РСЯ-счётчик |
29 | | - this._type = settings.type || 0; |
| 29 | + this._type = settings.type || 0; |
30 | 30 |
|
31 | | - this._encoding = settings.encoding || 'utf-8'; |
| 31 | + this._encoding = settings.encoding || 'utf-8'; |
32 | 32 |
|
33 | 33 | this._request = { |
34 | 34 | host: null, |
|
37 | 37 | 'user-agent': null, |
38 | 38 | ip: null |
39 | 39 | }; |
40 | | - }; |
| 40 | + }; |
41 | 41 |
|
42 | | - Counter.prototype = { |
| 42 | + Counter.prototype = { |
43 | 43 | /** |
44 | 44 | * Отправка хита |
45 | 45 | * |
|
53 | 53 | * @example |
54 | 54 | * counter.hit('http://mysite.org', 'Main page', 'http://google.com/...'); |
55 | 55 | */ |
56 | | - hit: function (pageUrl, pageTitle, pageRef, userParams, ut) { |
| 56 | + hit: function (pageUrl, pageTitle, pageRef, userParams, ut) { |
57 | 57 | if (!pageUrl) { |
58 | 58 | pageUrl = this._request.url; |
59 | 59 | } |
|
65 | 65 | this._hitExt(pageUrl, pageTitle, pageRef, userParams, {ut: ut}); |
66 | 66 |
|
67 | 67 | return this; |
68 | | - }, |
| 68 | + }, |
69 | 69 | /** |
70 | 70 | * Достижение цели |
71 | 71 | * |
|
75 | 75 | * @example |
76 | 76 | * counter.reachGoal('goalName'); |
77 | 77 | */ |
78 | | - reachGoal: function (target, userParams) { |
79 | | - var referer; |
80 | | - if (target) { |
81 | | - target = 'goal://' + this._request.host + '/' + target; |
82 | | - referer = this._request.url; |
83 | | - } else { |
84 | | - target = this._request.url; |
85 | | - referer = this._request.referer; |
86 | | - } |
87 | | - |
88 | | - this._hitExt(target, null, referer, userParams, null); |
| 78 | + reachGoal: function (target, userParams) { |
| 79 | + var referer; |
| 80 | + if (target) { |
| 81 | + target = 'goal://' + this._request.host + '/' + target; |
| 82 | + referer = this._request.url; |
| 83 | + } else { |
| 84 | + target = this._request.url; |
| 85 | + referer = this._request.referer; |
| 86 | + } |
| 87 | + |
| 88 | + this._hitExt(target, null, referer, userParams, null); |
89 | 89 |
|
90 | 90 | return this; |
91 | 91 | }, |
|
99 | 99 | * @example |
100 | 100 | * counter.extLink('http://nodejs.org'); |
101 | 101 | */ |
102 | | - extLink: function (url, title) { |
103 | | - if (url) { |
104 | | - this._hitExt(url, title, this._request.url, null, { |
105 | | - ln: true, |
106 | | - ut: 'noindex' |
107 | | - }); |
108 | | - } |
| 102 | + extLink: function (url, title) { |
| 103 | + if (url) { |
| 104 | + this._hitExt(url, title, this._request.url, null, { |
| 105 | + ln: true, |
| 106 | + ut: 'noindex' |
| 107 | + }); |
| 108 | + } |
109 | 109 |
|
110 | 110 | return this; |
111 | | - }, |
| 111 | + }, |
112 | 112 | /** |
113 | 113 | * Загрузка файла |
114 | 114 | * |
|
119 | 119 | * @example |
120 | 120 | * counter.file('http://mysite.org/secret.zip'); |
121 | 121 | */ |
122 | | - file: function (file, title) { |
123 | | - if (file) { |
124 | | - this._hitExt(file, title, this._request.url, null, { |
125 | | - dl: true, |
126 | | - ln: true |
127 | | - }); |
128 | | - } |
| 122 | + file: function (file, title) { |
| 123 | + if (file) { |
| 124 | + this._hitExt(file, title, this._request.url, null, { |
| 125 | + dl: true, |
| 126 | + ln: true |
| 127 | + }); |
| 128 | + } |
129 | 129 |
|
130 | 130 | return this; |
131 | | - }, |
| 131 | + }, |
132 | 132 | /** |
133 | 133 | * Параметры визитов |
134 | 134 | * |
|
140 | 140 | * или |
141 | 141 | * counter.params('level1', 'level2', 'level3', 1); |
142 | 142 | */ |
143 | | - params: function (data) { |
| 143 | + params: function (data) { |
144 | 144 | var obj = {}; |
145 | 145 | var pointer = obj; |
146 | 146 | var len = arguments.length; |
|
162 | 162 | } |
163 | 163 |
|
164 | 164 | return this; |
165 | | - }, |
| 165 | + }, |
166 | 166 | /** |
167 | 167 | * Не отказ |
168 | 168 | * |
|
171 | 171 | * @example |
172 | 172 | * counter.notBounce(); |
173 | 173 | */ |
174 | | - notBounce: function () { |
175 | | - this._hitExt('', '', '', null, {nb: true}); |
| 174 | + notBounce: function () { |
| 175 | + this._hitExt('', '', '', null, {nb: true}); |
176 | 176 |
|
177 | 177 | return this; |
178 | | - }, |
| 178 | + }, |
179 | 179 | /** |
180 | 180 | * Заполнение необходимых параметров из запроса сервера для отправки данных в Метрику |
181 | 181 | * |
|
205 | 205 | var rh = req.headers; |
206 | 206 | return rh['x-forwarded-proto'] || rh.protocol || (req.secure ? 'https' : 'http'); |
207 | 207 | }, |
208 | | - _hitExt: function (pageUrl, pageTitle, pageRef, userParams, modes) { |
209 | | - var postData = {}; |
| 208 | + _hitExt: function (pageUrl, pageTitle, pageRef, userParams, modes) { |
| 209 | + var postData = {}; |
210 | 210 |
|
211 | | - if (this._type) { |
212 | | - postData['cnt-class'] = this._type; |
213 | | - } |
214 | | - |
215 | | - if (pageUrl) { |
216 | | - postData['page-url'] = pageUrl; |
217 | | - } |
218 | | - |
219 | | - if (pageRef) { |
220 | | - postData['page-ref'] = pageRef; |
221 | | - } |
222 | | - |
223 | | - if (modes) { |
224 | | - modes.ar = true; |
225 | | - } else { |
226 | | - modes = {ar: true}; |
227 | | - } |
228 | | - |
229 | | - var browserInfo = []; |
230 | | - for(var key in modes) { |
231 | | - if (!modes.hasOwnProperty(key)) { |
232 | | - continue; |
233 | | - } |
234 | | - |
235 | | - if (key != 'ut') { |
236 | | - browserInfo.push(key + ':' + (modes[key] === true ? '1' : modes[key])); |
237 | | - } |
238 | | - } |
239 | | - |
240 | | - browserInfo.push('en:' + this._encoding); |
241 | | - browserInfo.push('rn:' + (Math.floor(Math.random() * 1E6))); |
| 211 | + if (this._type) { |
| 212 | + postData['cnt-class'] = this._type; |
| 213 | + } |
| 214 | + |
| 215 | + if (pageUrl) { |
| 216 | + postData['page-url'] = pageUrl; |
| 217 | + } |
| 218 | + |
| 219 | + if (pageRef) { |
| 220 | + postData['page-ref'] = pageRef; |
| 221 | + } |
| 222 | + |
| 223 | + if (modes) { |
| 224 | + modes.ar = true; |
| 225 | + } else { |
| 226 | + modes = {ar: true}; |
| 227 | + } |
| 228 | + |
| 229 | + var browserInfo = []; |
| 230 | + for(var key in modes) { |
| 231 | + if (!modes.hasOwnProperty(key)) { |
| 232 | + continue; |
| 233 | + } |
| 234 | + |
| 235 | + if (key != 'ut') { |
| 236 | + browserInfo.push(key + ':' + (modes[key] === true ? '1' : modes[key])); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + browserInfo.push('en:' + this._encoding); |
| 241 | + browserInfo.push('rn:' + (Math.floor(Math.random() * 1E6))); |
242 | 242 |
|
243 | | - if (pageTitle) { |
244 | | - browserInfo.push('t:' + pageTitle); |
245 | | - } |
246 | | - |
247 | | - postData['browser-info'] = browserInfo.join(':'); |
248 | | - |
249 | | - if (userParams) { |
250 | | - postData['site-info'] = JSON.stringify(userParams); |
251 | | - } |
| 243 | + if (pageTitle) { |
| 244 | + browserInfo.push('t:' + pageTitle); |
| 245 | + } |
| 246 | + |
| 247 | + postData['browser-info'] = browserInfo.join(':'); |
| 248 | + |
| 249 | + if (userParams) { |
| 250 | + postData['site-info'] = JSON.stringify(userParams); |
| 251 | + } |
252 | 252 |
|
253 | | - if (modes['ut']) { |
254 | | - postData['ut'] = modes['ut']; |
255 | | - } |
| 253 | + if (modes['ut']) { |
| 254 | + postData['ut'] = modes['ut']; |
| 255 | + } |
256 | 256 |
|
257 | 257 | this._sendData(postData); |
258 | | - }, |
| 258 | + }, |
259 | 259 | _sendData: function (data) { |
260 | 260 | var path = PATH + this._id |
261 | 261 | + '/1?rn=' + (Math.floor(Math.random() * 1E6)) |
262 | 262 | + '&wmode=2' |
263 | 263 | + '&' + querystring.stringify(data); |
264 | 264 |
|
265 | | - var req = http.request({ |
266 | | - host: HOST, |
267 | | - port: PORT, |
268 | | - path: path, |
269 | | - method: 'GET', |
270 | | - headers: { |
271 | | - 'x-real-ip': this._request.ip, |
272 | | - 'user-agent': this._request['user-agent'] |
273 | | - } |
274 | | - }, function () {}); |
| 265 | + var req = http.request({ |
| 266 | + host: HOST, |
| 267 | + port: PORT, |
| 268 | + path: path, |
| 269 | + method: 'GET', |
| 270 | + headers: { |
| 271 | + 'x-real-ip': this._request.ip, |
| 272 | + 'user-agent': this._request['user-agent'] |
| 273 | + } |
| 274 | + }, function () {}); |
275 | 275 |
|
276 | 276 | req.end(); |
277 | 277 | } |
278 | | - }; |
| 278 | + }; |
279 | 279 |
|
280 | | - exports.counter = function (settings) { |
| 280 | + exports.counter = function (settings) { |
281 | 281 | return new Counter(settings); |
282 | 282 | }; |
283 | 283 | })(); |
0 commit comments