-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (143 loc) · 5.23 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var url = require('url');
var request = require('request');
var querystring = require('querystring');
var extend = require('util')._extend;
var HOSTS = {
us: "sdr.totango.com",
eu: "sdr-eu1.totango.com",
test: "sdr-test.totango.com",
};
module.exports = function (serviceId, env, apiToken) {
var service_id;
var api_token;
var host = HOSTS[env] || HOSTS['us'];
if (serviceId === undefined || typeof serviceId !== 'string') { throw new Error('Please provide a service id (String)'); }
service_id = serviceId;
api_token = apiToken;
var trackActivityByServiceId = function (serviceId, accountId, userId, activity, module, callback) {
callback = callback || function(){};
if (typeof accountId !== 'string' ||
typeof userId !== 'string' ||
typeof activity !== 'string' ||
typeof module !== 'string' ||
typeof callback !== 'function') {
console.log('totango-tracker.trackActivity: Invalid parameters');
}
else {
var params = {
sdr_s: serviceId,
sdr_o: accountId,
sdr_u: userId,
sdr_a: activity,
sdr_m: module,
};
sendSDR(params, callback);
}
};
/**
* Track user activity
* @param {string} accountId
* @param {string} userId
* @param {string} activity
* @param {string} module
* @param {function} callback
* @returns {void}
* @example trackActivity('account_id', 'user_id', 'User logged-in', 'Login', function(err) {});
*/
var trackActivity = function(accountId, userId, activity, module, callback) {
trackActivityByServiceId(service_id, accountId, userId, activity, module, callback)
};
/**
* Set user attributes
* @param {string} accountId
* @param {string}userId
* @param {object} attributes - key value pairs of user attributes
* @param {function} callback
* @returns {void}
* @example setUserAttributes('account_id', 'user_id', { 'Number of Open Support Tickets': 3 }, function(err) {});
*/
var setUserAttributes = function(accountId, userId, attributes, callback) {
if (typeof accountId !== 'string' || typeof userId !== 'string') {
console.log('totango-tracker.setUserAttributes: Invalid parameters');
}
else {
var initialParams = {};
if (attributes['name']) {
initialParams['sdr_u.name'] = attributes['name'];
delete attributes.name;
}
setAttributes('sdr_u.', initialParams, { accountId: accountId, userId: userId }, attributes, callback);
}
};
/**
* Set account attributes
* @param {string} accountId
* @param {object} attributes - key value pairs of user attributes
* @param {function} callback
* @returns {void}
* @example setUserAttributes('account_id', { 'Number of Open Support Tickets': 3 }, function(err) {});
*/
var setAccountAttributes = function(accountId, attributes, callback) {
if (typeof accountId !== 'string') {
console.log('totango-tracker.setAccountAttributes: Invalid parameters');
}
else {
var initialParams = {};
if (attributes['name']) {
initialParams['sdr_odn'] = attributes['name'];
delete attributes.name;
}
setAttributes('sdr_o.', initialParams, { accountId: accountId }, attributes, callback);
}
};
var setAttributes = function(prefix, initialParams, identity, attributes, callback) {
callback = callback || function(){};
if (typeof attributes !== 'object' || typeof callback !== 'function') {
console.log('totango-tracker: Invalid attributes');
}
else {
var params = {
sdr_s: service_id,
sdr_o: identity.accountId,
};
if (identity.userId) { params['sdr_u'] = identity.userId; }
params = extend(params, initialParams);
for (var attr in attributes) {
params[prefix + attr] = attributes[attr];
}
sendSDR(params, callback);
}
};
var sendSDR = function(params, callback) {
if (api_token) {
params['api-token'] = api_token;
}
var options = {
url: url.format({
protocol: 'https',
host: host,
pathname: 'pixel.gif/',
search: querystring.stringify(params)
}),
method: 'GET',
jar: false
};
request(options, function (err, res, body) {
if (err) {
callback(err);
}
else if (res.statusCode !== 200 && res.statusCode !== 201) {
callback(new Error('Invalid request, status code: ' + res.statusCode));
}
else {
callback(null);
}
});
};
return {
trackActivity : trackActivity,
trackActivityByServiceId: trackActivityByServiceId,
setUserAttributes : setUserAttributes,
setAccountAttributes : setAccountAttributes
};
};