-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (55 loc) · 1.74 KB
/
index.js
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
/**
* @param {Interger} counterId Metrica counter identifier
* @param {Object} [config] optional params for request information
*/
module.exports = function (counterId, config = {}) {
let counter = require('yametrika').counter({
id: counterId
});
let defaultConfig = {
host: 'localhost',
path: '/bot',
ip: '127.0.0.1',
pageTitle: 'Bot',
defaultGoal: 'message',
userAgent: 'ChatBot: 0.0.1, '
};
return {
/**
* @param {String} fromId Unical user identifier
* @param {Object} message
* @param {String} [goal='message'] "Goal" to reach with this hit
*/
track: function(
fromId,
msg,
goal = config.defaultGoal || defaultConfig.defaultGoal
) {
if (!msg) return;
var message = {};
message[goal] = msg;
counter.req({
headers: {
protocol: 'https',
referer: fromId,
host: config.host || defaultConfig.host,
url: config.path || defaultConfig.path,
'user-agent': (config.userAgent || defaultConfig.userAgent) + fromId,
'x-real-ip': config.ip || defaultConfig.ip
},
connection: {
remoteAddress: config.ip || defaultConfig.ip
}
})
.hit(
'https://' + (config.host || defaultConfig.host),
config.pageTitle || defaultConfig.pageTitle,
fromId,
message,
'noindex'
)
.reachGoal(goal, message)
.notBounce();
}
};
};