Skip to content

Commit bb3a277

Browse files
committed
mc.yandex.ru - http->https
1 parent d90153f commit bb3a277

3 files changed

Lines changed: 69 additions & 69 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"name": "yametrika",
88
"main": "yametrika.js",
99
"description": "Серверное отслеживание посетителей с помощью Яндекс.Метрики",
10-
"version": "1.0.0",
10+
"version": "1.0.1",
1111
"homepage": "https://github.com/hcodes/server_yametrika_nodejs",
1212
"repository": {
1313
"type": "git",

tests/server.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
https://metrika.yandex.ru/stat/?counter_id=21312094
33
*/
44

5-
var http = require('http');
6-
var counter = require('../yametrika').counter({id: 21312094});
5+
var http = require('http'),
6+
counter = require('../yametrika').counter({id: 21312094});
77

88
http.createServer(function (req, res) {
99
res.writeHead(200, {'Content-Type': 'text/plain'});
1010
res.end('okay');
11-
11+
1212
counter.req(req);
13-
13+
1414
counter.hit('http://example.com', 'Main page', 'http://google.com');
1515
counter.hit('http://example.com/back/', 'Back', 'http://example.com/back/');
1616
counter.reachGoal('action');
1717
counter.extLink('http://nodejs.org');
1818
counter.file('http://example.com/file.zip');
1919
counter.params('level1', 'level2', 'level3', 1);
20-
}).listen(8080);
20+
}).listen(8080);

yametrika.js

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77

88
(function () {
99
'use strict';
10-
10+
1111
var HOST = 'mc.yandex.ru';
1212
var PATH = '/watch/';
13-
var PORT = 80;
14-
13+
var PORT = 443;
14+
1515
var querystring = require('querystring');
16-
var http = require('http');
17-
16+
var https = require('https');
17+
1818
/**
1919
* Конструктор счётчика Метрики
20-
* @constructor
20+
* @constructor
2121
*
2222
* @param {Object} settings - настройки счётчика
23-
*/
24-
var Counter = function (settings) {
23+
*/
24+
var Counter = function(settings) {
2525
// Номер счётчика
2626
this._id = settings.id;
27-
27+
2828
// Тип счётчика: 0 - обычный счётчик, 1 - РСЯ-счётчик
2929
this._type = settings.type || 0;
30-
30+
3131
this._encoding = settings.encoding || 'utf-8';
32-
32+
3333
this._request = {
3434
host: null,
3535
url: null,
@@ -42,40 +42,40 @@
4242
Counter.prototype = {
4343
/**
4444
* Отправка хита
45-
*
45+
*
4646
* @param {string} pageUrl - адрес страницы
4747
* @param {string} [pageTitle] - заголовок страницы
4848
* @param {string} [pageRef] - реферер страницы
4949
* @param {Object} [userParams] - параметры визитов
5050
* @param {string} [ut] - для запрета индексирования 'noindex'
5151
* @return {Object} this
52-
*
52+
*
5353
* @example
5454
* 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) {
5757
if (!pageUrl) {
5858
pageUrl = this._request.url;
5959
}
60-
60+
6161
if (!pageRef) {
6262
pageRef = this._request.referer;
6363
}
64-
64+
6565
this._hitExt(pageUrl, pageTitle, pageRef, userParams, {ut: ut});
66-
66+
6767
return this;
6868
},
6969
/**
7070
* Достижение цели
7171
*
7272
* @param {string} target - название цели
7373
* @param {Object} [userParams] - параметры визитов
74-
*
74+
*
7575
* @example
7676
* counter.reachGoal('goalName');
77-
*/
78-
reachGoal: function (target, userParams) {
77+
*/
78+
reachGoal: function(target, userParams) {
7979
var referer;
8080
if (target) {
8181
target = 'goal://' + this._request.host + '/' + target;
@@ -84,9 +84,9 @@
8484
target = this._request.url;
8585
referer = this._request.referer;
8686
}
87-
87+
8888
this._hitExt(target, null, referer, userParams, null);
89-
89+
9090
return this;
9191
},
9292
/**
@@ -95,18 +95,18 @@
9595
* @param {string} url - адрес страницы
9696
* @param {string} [title] - заголовок страницы
9797
* @return {Object} this
98-
*
98+
*
9999
* @example
100100
* counter.extLink('http://nodejs.org');
101-
*/
102-
extLink: function (url, title) {
101+
*/
102+
extLink: function(url, title) {
103103
if (url) {
104104
this._hitExt(url, title, this._request.url, null, {
105105
ln: true,
106106
ut: 'noindex'
107107
});
108108
}
109-
109+
110110
return this;
111111
},
112112
/**
@@ -115,32 +115,32 @@
115115
* @param {string} file - ссылка на файл
116116
* @param {string} [title] - заголовок страницы
117117
* @return {Object} this
118-
*
118+
*
119119
* @example
120120
* counter.file('http://mysite.org/secret.zip');
121-
*/
122-
file: function (file, title) {
121+
*/
122+
file: function(file, title) {
123123
if (file) {
124124
this._hitExt(file, title, this._request.url, null, {
125125
dl: true,
126126
ln: true
127127
});
128128
}
129-
129+
130130
return this;
131131
},
132132
/**
133133
* Параметры визитов
134134
*
135135
* @param {...*} параметры визитов
136-
* @return {Object} this
137-
*
136+
* @return {Object} this
137+
*
138138
* @example
139139
* counter.params({level1: {level2: {level3: 1}}});
140140
* или
141141
* counter.params('level1', 'level2', 'level3', 1);
142-
*/
143-
params: function (data) {
142+
*/
143+
params: function(data) {
144144
var obj = {};
145145
var pointer = obj;
146146
var len = arguments.length;
@@ -153,39 +153,39 @@
153153
pointer = pointer[arguments[i]];
154154
}
155155
}
156-
156+
157157
this._hitExt('', '', '', obj, {pa: true});
158158
} else {
159159
if (data) {
160160
this._hitExt('', '', '', data, {pa: true});
161161
}
162162
}
163-
163+
164164
return this;
165165
},
166166
/**
167167
* Не отказ
168168
*
169169
* @return {Object} this
170-
*
170+
*
171171
* @example
172172
* counter.notBounce();
173-
*/
174-
notBounce: function () {
173+
*/
174+
notBounce: function() {
175175
this._hitExt('', '', '', null, {nb: true});
176-
176+
177177
return this;
178178
},
179179
/**
180180
* Заполнение необходимых параметров из запроса сервера для отправки данных в Метрику
181181
*
182182
* @param {Object} req
183183
* @return {Object} this
184-
*
184+
*
185185
* @example
186186
* counter.req(req);
187-
*/
188-
req: function (req) {
187+
*/
188+
req: function(req) {
189189
var rh = req.headers;
190190
this._request = {
191191
host: rh.host,
@@ -194,75 +194,75 @@
194194
'user-agent': rh['user-agent'],
195195
ip: this._clientIP(req)
196196
};
197-
197+
198198
return this;
199199
},
200-
_clientIP: function (req) {
200+
_clientIP: function(req) {
201201
var rh = req.headers;
202202
return rh['x-real-ip'] || rh['x-forwarded-for'] || rh['x-remote-ip'] || rh['x-originating-ip'] || req.connection.remoteAddress;
203203
},
204-
_protocol: function (req) {
204+
_protocol: function(req) {
205205
var rh = req.headers;
206206
return rh['x-forwarded-proto'] || rh.protocol || (req.secure ? 'https' : 'http');
207207
},
208-
_hitExt: function (pageUrl, pageTitle, pageRef, userParams, modes) {
208+
_hitExt: function(pageUrl, pageTitle, pageRef, userParams, modes) {
209209
var postData = {};
210210

211211
if (this._type) {
212212
postData['cnt-class'] = this._type;
213213
}
214-
214+
215215
if (pageUrl) {
216216
postData['page-url'] = pageUrl;
217217
}
218-
218+
219219
if (pageRef) {
220220
postData['page-ref'] = pageRef;
221-
}
222-
221+
}
222+
223223
if (modes) {
224224
modes.ar = true;
225225
} else {
226226
modes = {ar: true};
227227
}
228-
228+
229229
var browserInfo = [];
230230
for(var key in modes) {
231231
if (!modes.hasOwnProperty(key)) {
232232
continue;
233233
}
234-
234+
235235
if (key != 'ut') {
236236
browserInfo.push(key + ':' + (modes[key] === true ? '1' : modes[key]));
237237
}
238238
}
239-
239+
240240
browserInfo.push('en:' + this._encoding);
241241
browserInfo.push('rn:' + (Math.floor(Math.random() * 1E6)));
242242

243243
if (pageTitle) {
244244
browserInfo.push('t:' + pageTitle);
245245
}
246-
246+
247247
postData['browser-info'] = browserInfo.join(':');
248-
248+
249249
if (userParams) {
250250
postData['site-info'] = JSON.stringify(userParams);
251251
}
252252

253253
if (modes['ut']) {
254254
postData['ut'] = modes['ut'];
255255
}
256-
256+
257257
this._sendData(postData);
258258
},
259-
_sendData: function (data) {
259+
_sendData: function(data) {
260260
var path = PATH + this._id
261261
+ '/1?rn=' + (Math.floor(Math.random() * 1E6))
262262
+ '&wmode=2'
263263
+ '&' + querystring.stringify(data);
264264

265-
var req = http.request({
265+
var req = https.request({
266266
host: HOST,
267267
port: PORT,
268268
path: path,
@@ -271,13 +271,13 @@
271271
'x-real-ip': this._request.ip,
272272
'user-agent': this._request['user-agent']
273273
}
274-
}, function () {});
275-
274+
}, function() {});
275+
276276
req.end();
277277
}
278278
};
279279

280-
exports.counter = function (settings) {
280+
exports.counter = function(settings) {
281281
return new Counter(settings);
282282
};
283283
})();

0 commit comments

Comments
 (0)