|
7 | 7 |
|
8 | 8 | (function () { |
9 | 9 | 'use strict'; |
10 | | - |
| 10 | + |
11 | 11 | var HOST = 'mc.yandex.ru'; |
12 | 12 | var PATH = '/watch/'; |
13 | | - var PORT = 80; |
14 | | - |
| 13 | + var PORT = 443; |
| 14 | + |
15 | 15 | var querystring = require('querystring'); |
16 | | - var http = require('http'); |
17 | | - |
| 16 | + var https = require('https'); |
| 17 | + |
18 | 18 | /** |
19 | 19 | * Конструктор счётчика Метрики |
20 | | - * @constructor |
| 20 | + * @constructor |
21 | 21 | * |
22 | 22 | * @param {Object} settings - настройки счётчика |
23 | | - */ |
24 | | - var Counter = function (settings) { |
| 23 | + */ |
| 24 | + var Counter = function(settings) { |
25 | 25 | // Номер счётчика |
26 | 26 | this._id = settings.id; |
27 | | - |
| 27 | + |
28 | 28 | // Тип счётчика: 0 - обычный счётчик, 1 - РСЯ-счётчик |
29 | 29 | this._type = settings.type || 0; |
30 | | - |
| 30 | + |
31 | 31 | this._encoding = settings.encoding || 'utf-8'; |
32 | | - |
| 32 | + |
33 | 33 | this._request = { |
34 | 34 | host: null, |
35 | 35 | url: null, |
|
42 | 42 | Counter.prototype = { |
43 | 43 | /** |
44 | 44 | * Отправка хита |
45 | | - * |
| 45 | + * |
46 | 46 | * @param {string} pageUrl - адрес страницы |
47 | 47 | * @param {string} [pageTitle] - заголовок страницы |
48 | 48 | * @param {string} [pageRef] - реферер страницы |
49 | 49 | * @param {Object} [userParams] - параметры визитов |
50 | 50 | * @param {string} [ut] - для запрета индексирования 'noindex' |
51 | 51 | * @return {Object} this |
52 | | - * |
| 52 | + * |
53 | 53 | * @example |
54 | 54 | * counter.hit('http://mysite.org', 'Main page', 'http://google.com/...'); |
55 | | - */ |
56 | | - hit: function (pageUrl, pageTitle, pageRef, userParams, ut) { |
| 55 | + */ |
| 56 | + hit: function(pageUrl, pageTitle, pageRef, userParams, ut) { |
57 | 57 | if (!pageUrl) { |
58 | 58 | pageUrl = this._request.url; |
59 | 59 | } |
60 | | - |
| 60 | + |
61 | 61 | if (!pageRef) { |
62 | 62 | pageRef = this._request.referer; |
63 | 63 | } |
64 | | - |
| 64 | + |
65 | 65 | this._hitExt(pageUrl, pageTitle, pageRef, userParams, {ut: ut}); |
66 | | - |
| 66 | + |
67 | 67 | return this; |
68 | 68 | }, |
69 | 69 | /** |
70 | 70 | * Достижение цели |
71 | 71 | * |
72 | 72 | * @param {string} target - название цели |
73 | 73 | * @param {Object} [userParams] - параметры визитов |
74 | | - * |
| 74 | + * |
75 | 75 | * @example |
76 | 76 | * counter.reachGoal('goalName'); |
77 | | - */ |
78 | | - reachGoal: function (target, userParams) { |
| 77 | + */ |
| 78 | + reachGoal: function(target, userParams) { |
79 | 79 | var referer; |
80 | 80 | if (target) { |
81 | 81 | target = 'goal://' + this._request.host + '/' + target; |
|
84 | 84 | target = this._request.url; |
85 | 85 | referer = this._request.referer; |
86 | 86 | } |
87 | | - |
| 87 | + |
88 | 88 | this._hitExt(target, null, referer, userParams, null); |
89 | | - |
| 89 | + |
90 | 90 | return this; |
91 | 91 | }, |
92 | 92 | /** |
|
95 | 95 | * @param {string} url - адрес страницы |
96 | 96 | * @param {string} [title] - заголовок страницы |
97 | 97 | * @return {Object} this |
98 | | - * |
| 98 | + * |
99 | 99 | * @example |
100 | 100 | * counter.extLink('http://nodejs.org'); |
101 | | - */ |
102 | | - extLink: function (url, title) { |
| 101 | + */ |
| 102 | + extLink: function(url, title) { |
103 | 103 | if (url) { |
104 | 104 | this._hitExt(url, title, this._request.url, null, { |
105 | 105 | ln: true, |
106 | 106 | ut: 'noindex' |
107 | 107 | }); |
108 | 108 | } |
109 | | - |
| 109 | + |
110 | 110 | return this; |
111 | 111 | }, |
112 | 112 | /** |
|
115 | 115 | * @param {string} file - ссылка на файл |
116 | 116 | * @param {string} [title] - заголовок страницы |
117 | 117 | * @return {Object} this |
118 | | - * |
| 118 | + * |
119 | 119 | * @example |
120 | 120 | * counter.file('http://mysite.org/secret.zip'); |
121 | | - */ |
122 | | - file: function (file, title) { |
| 121 | + */ |
| 122 | + file: function(file, title) { |
123 | 123 | if (file) { |
124 | 124 | this._hitExt(file, title, this._request.url, null, { |
125 | 125 | dl: true, |
126 | 126 | ln: true |
127 | 127 | }); |
128 | 128 | } |
129 | | - |
| 129 | + |
130 | 130 | return this; |
131 | 131 | }, |
132 | 132 | /** |
133 | 133 | * Параметры визитов |
134 | 134 | * |
135 | 135 | * @param {...*} параметры визитов |
136 | | - * @return {Object} this |
137 | | - * |
| 136 | + * @return {Object} this |
| 137 | + * |
138 | 138 | * @example |
139 | 139 | * counter.params({level1: {level2: {level3: 1}}}); |
140 | 140 | * или |
141 | 141 | * counter.params('level1', 'level2', 'level3', 1); |
142 | | - */ |
143 | | - params: function (data) { |
| 142 | + */ |
| 143 | + params: function(data) { |
144 | 144 | var obj = {}; |
145 | 145 | var pointer = obj; |
146 | 146 | var len = arguments.length; |
|
153 | 153 | pointer = pointer[arguments[i]]; |
154 | 154 | } |
155 | 155 | } |
156 | | - |
| 156 | + |
157 | 157 | this._hitExt('', '', '', obj, {pa: true}); |
158 | 158 | } else { |
159 | 159 | if (data) { |
160 | 160 | this._hitExt('', '', '', data, {pa: true}); |
161 | 161 | } |
162 | 162 | } |
163 | | - |
| 163 | + |
164 | 164 | return this; |
165 | 165 | }, |
166 | 166 | /** |
167 | 167 | * Не отказ |
168 | 168 | * |
169 | 169 | * @return {Object} this |
170 | | - * |
| 170 | + * |
171 | 171 | * @example |
172 | 172 | * counter.notBounce(); |
173 | | - */ |
174 | | - notBounce: function () { |
| 173 | + */ |
| 174 | + notBounce: function() { |
175 | 175 | this._hitExt('', '', '', null, {nb: true}); |
176 | | - |
| 176 | + |
177 | 177 | return this; |
178 | 178 | }, |
179 | 179 | /** |
180 | 180 | * Заполнение необходимых параметров из запроса сервера для отправки данных в Метрику |
181 | 181 | * |
182 | 182 | * @param {Object} req |
183 | 183 | * @return {Object} this |
184 | | - * |
| 184 | + * |
185 | 185 | * @example |
186 | 186 | * counter.req(req); |
187 | | - */ |
188 | | - req: function (req) { |
| 187 | + */ |
| 188 | + req: function(req) { |
189 | 189 | var rh = req.headers; |
190 | 190 | this._request = { |
191 | 191 | host: rh.host, |
|
194 | 194 | 'user-agent': rh['user-agent'], |
195 | 195 | ip: this._clientIP(req) |
196 | 196 | }; |
197 | | - |
| 197 | + |
198 | 198 | return this; |
199 | 199 | }, |
200 | | - _clientIP: function (req) { |
| 200 | + _clientIP: function(req) { |
201 | 201 | var rh = req.headers; |
202 | 202 | return rh['x-real-ip'] || rh['x-forwarded-for'] || rh['x-remote-ip'] || rh['x-originating-ip'] || req.connection.remoteAddress; |
203 | 203 | }, |
204 | | - _protocol: function (req) { |
| 204 | + _protocol: function(req) { |
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) { |
| 208 | + _hitExt: function(pageUrl, pageTitle, pageRef, userParams, modes) { |
209 | 209 | var postData = {}; |
210 | 210 |
|
211 | 211 | if (this._type) { |
212 | 212 | postData['cnt-class'] = this._type; |
213 | 213 | } |
214 | | - |
| 214 | + |
215 | 215 | if (pageUrl) { |
216 | 216 | postData['page-url'] = pageUrl; |
217 | 217 | } |
218 | | - |
| 218 | + |
219 | 219 | if (pageRef) { |
220 | 220 | postData['page-ref'] = pageRef; |
221 | | - } |
222 | | - |
| 221 | + } |
| 222 | + |
223 | 223 | if (modes) { |
224 | 224 | modes.ar = true; |
225 | 225 | } else { |
226 | 226 | modes = {ar: true}; |
227 | 227 | } |
228 | | - |
| 228 | + |
229 | 229 | var browserInfo = []; |
230 | 230 | for(var key in modes) { |
231 | 231 | if (!modes.hasOwnProperty(key)) { |
232 | 232 | continue; |
233 | 233 | } |
234 | | - |
| 234 | + |
235 | 235 | if (key != 'ut') { |
236 | 236 | browserInfo.push(key + ':' + (modes[key] === true ? '1' : modes[key])); |
237 | 237 | } |
238 | 238 | } |
239 | | - |
| 239 | + |
240 | 240 | browserInfo.push('en:' + this._encoding); |
241 | 241 | browserInfo.push('rn:' + (Math.floor(Math.random() * 1E6))); |
242 | 242 |
|
243 | 243 | if (pageTitle) { |
244 | 244 | browserInfo.push('t:' + pageTitle); |
245 | 245 | } |
246 | | - |
| 246 | + |
247 | 247 | postData['browser-info'] = browserInfo.join(':'); |
248 | | - |
| 248 | + |
249 | 249 | if (userParams) { |
250 | 250 | postData['site-info'] = JSON.stringify(userParams); |
251 | 251 | } |
252 | 252 |
|
253 | 253 | if (modes['ut']) { |
254 | 254 | postData['ut'] = modes['ut']; |
255 | 255 | } |
256 | | - |
| 256 | + |
257 | 257 | this._sendData(postData); |
258 | 258 | }, |
259 | | - _sendData: function (data) { |
| 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({ |
| 265 | + var req = https.request({ |
266 | 266 | host: HOST, |
267 | 267 | port: PORT, |
268 | 268 | path: path, |
|
271 | 271 | 'x-real-ip': this._request.ip, |
272 | 272 | 'user-agent': this._request['user-agent'] |
273 | 273 | } |
274 | | - }, function () {}); |
275 | | - |
| 274 | + }, function() {}); |
| 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