Skip to content

Commit

Permalink
mc.yandex.ru - http->https
Browse files Browse the repository at this point in the history
  • Loading branch information
hcodes committed Jun 9, 2015
1 parent d90153f commit bb3a277
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 69 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"name": "yametrika",
"main": "yametrika.js",
"description": "Серверное отслеживание посетителей с помощью Яндекс.Метрики",
"version": "1.0.0",
"version": "1.0.1",
"homepage": "https://github.com/hcodes/server_yametrika_nodejs",
"repository": {
"type": "git",
Expand Down
10 changes: 5 additions & 5 deletions tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
https://metrika.yandex.ru/stat/?counter_id=21312094
*/

var http = require('http');
var counter = require('../yametrika').counter({id: 21312094});
var http = require('http'),
counter = require('../yametrika').counter({id: 21312094});

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('okay');

counter.req(req);

counter.hit('http://example.com', 'Main page', 'http://google.com');
counter.hit('http://example.com/back/', 'Back', 'http://example.com/back/');
counter.reachGoal('action');
counter.extLink('http://nodejs.org');
counter.file('http://example.com/file.zip');
counter.params('level1', 'level2', 'level3', 1);
}).listen(8080);
}).listen(8080);
126 changes: 63 additions & 63 deletions yametrika.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@

(function () {
'use strict';

var HOST = 'mc.yandex.ru';
var PATH = '/watch/';
var PORT = 80;
var PORT = 443;

var querystring = require('querystring');
var http = require('http');
var https = require('https');

/**
* Конструктор счётчика Метрики
* @constructor
* @constructor
*
* @param {Object} settings - настройки счётчика
*/
var Counter = function (settings) {
*/
var Counter = function(settings) {
// Номер счётчика
this._id = settings.id;

// Тип счётчика: 0 - обычный счётчик, 1 - РСЯ-счётчик
this._type = settings.type || 0;

this._encoding = settings.encoding || 'utf-8';

this._request = {
host: null,
url: null,
Expand All @@ -42,40 +42,40 @@
Counter.prototype = {
/**
* Отправка хита
*
*
* @param {string} pageUrl - адрес страницы
* @param {string} [pageTitle] - заголовок страницы
* @param {string} [pageRef] - реферер страницы
* @param {Object} [userParams] - параметры визитов
* @param {string} [ut] - для запрета индексирования 'noindex'
* @return {Object} this
*
*
* @example
* counter.hit('http://mysite.org', 'Main page', 'http://google.com/...');
*/
hit: function (pageUrl, pageTitle, pageRef, userParams, ut) {
*/
hit: function(pageUrl, pageTitle, pageRef, userParams, ut) {
if (!pageUrl) {
pageUrl = this._request.url;
}

if (!pageRef) {
pageRef = this._request.referer;
}

this._hitExt(pageUrl, pageTitle, pageRef, userParams, {ut: ut});

return this;
},
/**
* Достижение цели
*
* @param {string} target - название цели
* @param {Object} [userParams] - параметры визитов
*
*
* @example
* counter.reachGoal('goalName');
*/
reachGoal: function (target, userParams) {
*/
reachGoal: function(target, userParams) {
var referer;
if (target) {
target = 'goal://' + this._request.host + '/' + target;
Expand All @@ -84,9 +84,9 @@
target = this._request.url;
referer = this._request.referer;
}

this._hitExt(target, null, referer, userParams, null);

return this;
},
/**
Expand All @@ -95,18 +95,18 @@
* @param {string} url - адрес страницы
* @param {string} [title] - заголовок страницы
* @return {Object} this
*
*
* @example
* counter.extLink('http://nodejs.org');
*/
extLink: function (url, title) {
*/
extLink: function(url, title) {
if (url) {
this._hitExt(url, title, this._request.url, null, {
ln: true,
ut: 'noindex'
});
}

return this;
},
/**
Expand All @@ -115,32 +115,32 @@
* @param {string} file - ссылка на файл
* @param {string} [title] - заголовок страницы
* @return {Object} this
*
*
* @example
* counter.file('http://mysite.org/secret.zip');
*/
file: function (file, title) {
*/
file: function(file, title) {
if (file) {
this._hitExt(file, title, this._request.url, null, {
dl: true,
ln: true
});
}

return this;
},
/**
* Параметры визитов
*
* @param {...*} параметры визитов
* @return {Object} this
*
* @return {Object} this
*
* @example
* counter.params({level1: {level2: {level3: 1}}});
* или
* counter.params('level1', 'level2', 'level3', 1);
*/
params: function (data) {
*/
params: function(data) {
var obj = {};
var pointer = obj;
var len = arguments.length;
Expand All @@ -153,39 +153,39 @@
pointer = pointer[arguments[i]];
}
}

this._hitExt('', '', '', obj, {pa: true});
} else {
if (data) {
this._hitExt('', '', '', data, {pa: true});
}
}

return this;
},
/**
* Не отказ
*
* @return {Object} this
*
*
* @example
* counter.notBounce();
*/
notBounce: function () {
*/
notBounce: function() {
this._hitExt('', '', '', null, {nb: true});

return this;
},
/**
* Заполнение необходимых параметров из запроса сервера для отправки данных в Метрику
*
* @param {Object} req
* @return {Object} this
*
*
* @example
* counter.req(req);
*/
req: function (req) {
*/
req: function(req) {
var rh = req.headers;
this._request = {
host: rh.host,
Expand All @@ -194,75 +194,75 @@
'user-agent': rh['user-agent'],
ip: this._clientIP(req)
};

return this;
},
_clientIP: function (req) {
_clientIP: function(req) {
var rh = req.headers;
return rh['x-real-ip'] || rh['x-forwarded-for'] || rh['x-remote-ip'] || rh['x-originating-ip'] || req.connection.remoteAddress;
},
_protocol: function (req) {
_protocol: function(req) {
var rh = req.headers;
return rh['x-forwarded-proto'] || rh.protocol || (req.secure ? 'https' : 'http');
},
_hitExt: function (pageUrl, pageTitle, pageRef, userParams, modes) {
_hitExt: function(pageUrl, pageTitle, pageRef, userParams, modes) {
var postData = {};

if (this._type) {
postData['cnt-class'] = this._type;
}

if (pageUrl) {
postData['page-url'] = pageUrl;
}

if (pageRef) {
postData['page-ref'] = pageRef;
}
}

if (modes) {
modes.ar = true;
} else {
modes = {ar: true};
}

var browserInfo = [];
for(var key in modes) {
if (!modes.hasOwnProperty(key)) {
continue;
}

if (key != 'ut') {
browserInfo.push(key + ':' + (modes[key] === true ? '1' : modes[key]));
}
}

browserInfo.push('en:' + this._encoding);
browserInfo.push('rn:' + (Math.floor(Math.random() * 1E6)));

if (pageTitle) {
browserInfo.push('t:' + pageTitle);
}

postData['browser-info'] = browserInfo.join(':');

if (userParams) {
postData['site-info'] = JSON.stringify(userParams);
}

if (modes['ut']) {
postData['ut'] = modes['ut'];
}

this._sendData(postData);
},
_sendData: function (data) {
_sendData: function(data) {
var path = PATH + this._id
+ '/1?rn=' + (Math.floor(Math.random() * 1E6))
+ '&wmode=2'
+ '&' + querystring.stringify(data);

var req = http.request({
var req = https.request({
host: HOST,
port: PORT,
path: path,
Expand All @@ -271,13 +271,13 @@
'x-real-ip': this._request.ip,
'user-agent': this._request['user-agent']
}
}, function () {});
}, function() {});

req.end();
}
};

exports.counter = function (settings) {
exports.counter = function(settings) {
return new Counter(settings);
};
})();

0 comments on commit bb3a277

Please sign in to comment.