Skip to content

Instantly share code, notes, and snippets.

@WesleyDRobinson
Created July 29, 2017 00:19
Show Gist options
  • Save WesleyDRobinson/53c0de7efb30b6c8dcfc241e5a82cf83 to your computer and use it in GitHub Desktop.
Save WesleyDRobinson/53c0de7efb30b6c8dcfc241e5a82cf83 to your computer and use it in GitHub Desktop.
serverside amplitude-index.js
/**
* Module dependencies.
*/
var integration = require('../../../createIntegration')
var mapper = require('./mapper')
var encode = require('urlencode')
/**
* Expose `Amplitude`
*/
var Amplitude = module.exports = integration('Amplitude')
.endpoint('https://api.amplitude.com')
.channels(['server', 'mobile'])
.ensure('settings.apiKey')
.mapper(mapper)
.retries(2)
/**
* Set up our prototype methods
*/
Amplitude.prototype.page = page
Amplitude.prototype.screen = page
Amplitude.prototype.track = track
Amplitude.prototype.identify = identify
Amplitude.prototype.group = group
/**
* Track an event, screen, or page call.
*
* @param {Object} payload
* @param {Function} fn
*/
function track (payload, fn) {
var self = this
return this
.post('/httpapi')
.send('api_key=' + this.settings.apiKey)
.send('event=' + encode(JSON.stringify(payload)))
.end(this.handle(function (err, res) {
if (err) return fn(err, res)
if (res.text === 'invalid api_key') return fn(self.error('invalid api_key'))
fn(null, res)
}))
}
/**
* Track a screen or page call.
*
* @param {Object} payload
* @param {Function} fn
*/
function page (payload, fn) {
// Amplitude is generally not used for page tracking so we offer options to
// filter out irrelevant page calls. In the mapper, the payload will return
// empty if the none of the tracking page call settings are enabled or the
// proper values aren't sent through which means we want to drop the event.
if (payload.length === 0) {
return fn()
}
var self = this
return this
.post('/httpapi')
.send('api_key=' + this.settings.apiKey)
.send('event=' + encode(JSON.stringify(payload)))
.end(this.handle(function (err, res) {
if (err) return fn(err, res)
if (res.text === 'invalid api_key') return fn(self.error('invalid api_key'))
fn(null, res)
}))
}
/**
* Send an identify call.
*
* @param {Object} payload
* @param {Function} fn
*/
function identify (payload, fn) {
var self = this
return this
.post('/identify')
.send('api_key=' + this.settings.apiKey)
.send('identification=' + encode(JSON.stringify(payload)))
.end(this.handle(function (err, res) {
if (err) return fn(err, res)
if (res.text === 'invalid api_key') return fn(self.error('invalid api_key'))
fn(null, res)
}))
}
/**
* Set groups via an identify call.
*
* @param {Object} payload
* @param {Function} fn
*/
function group (payload, fn) {
var self = this
return this
.post('/identify')
.send('api_key=' + this.settings.apiKey)
.send('identification=' + encode(JSON.stringify(payload)))
.end(this.handle(function (err, res) {
if (err) return fn(err, res)
if (res.text === 'invalid api_key') return fn(self.error('invalid api_key'))
fn(null, res)
}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment