This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (127 loc) · 4.34 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
var ThreadsIo = function (apikey, options) {
'use strict';
var https = require('https'),
util = require('util'),
baseoptions = {
hostname: 'input.threads.io',
port: 443,
path: '/v1/',
method: 'POST',
auth: apikey + ':' + ''
},
dontsend = options.dontsend;
function sendRequest(apimethod, data, callback) {
if (!dontsend) {
var options = util._extend({}, baseoptions),
postdata = JSON.stringify(data),
request;
options.path += apimethod;
options.headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postdata)
};
request = https.request(options, function (response) {
response.setEncoding('utf8');
response.on('data', function (apiresult) {
if (response.statusCode === 200) {
apiresult = JSON.parse(apiresult);
if (apiresult.success === true) {
callback(null, apiresult);
} else {
callback(apiresult);
}
} else {
callback(apiresult);
}
});
});
request.on('error', function (error) {
callback(error);
});
request.write(postdata);
request.end();
} else {
callback(null, true);
}
}
this.identify = function (params, callback) {
if (params.userid) {
var identifyparams = {
timestamp: new Date().toISOString(),
userId: params.userid,
traits: params.traits
};
sendRequest('identify', identifyparams, function (apierror, result) {
if (callback !== undefined) {
callback(apierror, result);
}
});
} else {
if (callback !== undefined) {
callback('No userid specificed for identify to threads.');
}
}
};
this.track = function (params, callback) {
if (params.userid && params.event) {
var trackparams = {
timestamp: new Date().toISOString(),
userId: params.userid,
event: params.event,
properties: params.properties
};
sendRequest('track', trackparams, function (apierror, result) {
if (callback !== undefined) {
callback(apierror, result);
}
});
} else {
if (callback !== undefined) {
callback('No userid or event specificed for track to threads.');
}
}
};
this.page = function (params, callback) {
if (params.userid && params.name) {
var trackparams = {
timestamp: new Date().toISOString(),
userId: params.userid,
eventKey: config.threadsio.key,
name: params.name,
properties: params.properties
};
sendRequest('page', trackparams, function (apierror, result) {
if (callback !== undefined) {
callback(apierror, result);
}
});
} else {
if (callback !== undefined) {
callback('No userid or name specificed for record page view to threads.');
}
}
};
this.remove = function (params, callback) {
if (params.userid) {
var removeparams = {
timestamp: new Date().toISOString(),
userId: params.userid
};
sendRequest('remove', removeparams, function (apierror, result) {
if (callback !== undefined) {
callback(apierror, result);
}
});
} else {
if (callback !== undefined) {
callback('No userid specificed for remove to threads.');
}
}
};
};
module.exports = function (apikey, options) {
if (options === undefined) {
options = {};
}
return new ThreadsIo(apikey, options);
};