Last active
March 10, 2016 01:20
-
-
Save nijikokun/af05fa60e3908b9fa4c2 to your computer and use it in GitHub Desktop.
Segment IO Native Node.js Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var https = require('https') | |
function check (body, callback) { | |
if (typeof body === 'function') { | |
callback = body | |
body = null | |
} | |
body = body || {} | |
callback = callback || function () {} | |
if (!body.timestamp) { | |
body.timestamp = new Date().toISOString() | |
} | |
return { | |
body: body, | |
callback: callback | |
} | |
} | |
function Segment (key) { | |
var auth = 'Basic ' + new Buffer(key + ':').toString('base64') | |
return { | |
post: function (path, headers, data, callback) { | |
data = JSON.stringify(data) | |
headers = headers || {} | |
headers['Content-Type'] = 'application/json' | |
headers['Content-Length'] = Buffer.byteLength(data) | |
headers['Authorization'] = auth | |
var request = https.request({ | |
host: 'api.segment.io', | |
path: path, | |
headers: headers, | |
method: 'POST' | |
}, function (response) { | |
response.body = '' | |
response.setEncoding('utf8') | |
response.on('data', function (chunk) { | |
response.body += chunk | |
}) | |
response.on('end', function () { | |
try { | |
response.body = JSON.parse(response.body) | |
callback(null, response) | |
} catch (error) { | |
callback(error) | |
} | |
}) | |
}) | |
request.on('error', callback) | |
request.write(data) | |
request.end() | |
}, | |
track: function (userId, eventName, body, callback) { | |
var output = check(body, callback) | |
body = output.body | |
callback = output.callback | |
body.userId = userId | |
body.event = eventName | |
this.post('/v1/track', {}, body, callback) | |
}, | |
identify: function (body, callback) { | |
var output = check(body, callback) | |
body = output.body | |
callback = output.callback | |
this.post('/v1/identify', {}, body, callback) | |
}, | |
group: function (userId, groupId, body, callback) { | |
var output = check(body, callback) | |
body = output.body | |
callback = output.callback | |
body.userId = userId | |
body.groupId = groupId | |
this.post('/v1/group', {}, body, callback) | |
}, | |
screen: function (userId, name, body, callback) { | |
check(body, callback) | |
body.userId = userId | |
body.name = name | |
this.post('/v1/screen', {}, body, callback) | |
}, | |
alias: function (previousId, currentId, body, callback) { | |
var output = check(body, callback) | |
body = output.body | |
callback = output.callback | |
body.userId = currentId | |
body.previousId = previousId | |
this.post('/v1/alias', {}, body, callback) | |
}, | |
import: function (users, body, callback) { | |
var output = check(body, callback) | |
body = output.body | |
callback = output.callback | |
body.batch = users | |
this.post('/v1/alias', {}, body, callback) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment