Created
June 26, 2017 22:07
-
-
Save WesleyDRobinson/77c16ee5e81f9eeced643a0924057add to your computer and use it in GitHub Desktop.
hubspot serverside mapper
This file contains hidden or 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
/** | |
* Module dependencies. | |
*/ | |
var traverse = require('isodate-traverse') | |
var del = require('obj-case').del | |
var extend = require('extend') | |
var reject = require('reject') | |
var each = require('@ndhoule/each') | |
/** | |
* Map track `msg`. | |
* | |
* Notes: | |
* | |
* https://developers.hubspot.com/docs/methods/enterprise_events/http_api | |
* https://app.hubspot.com/analyze/{{ portalId }}/events/ | |
* | |
* @param {Facade} msg | |
* @param {Object} settings | |
* @return {Object} | |
*/ | |
exports.track = function (msg, settings) { | |
return { | |
_a: settings.portalId, | |
email: msg.email(), | |
_m: msg.revenue(), | |
_n: msg.event() | |
} | |
} | |
/** | |
* Map identify `msg`. | |
* | |
* Notes: | |
* | |
* https://developers.hubspot.com/docs/methods/contacts/update_contact | |
* https://developers.hubspot.com/docs/methods/contacts/create_contact | |
* https://groups.google.com/forum/#!searchin/hubspot-api/datetime/hubspot-api/azXRWXWWLVc/oiSmkT2Y_DcJ | |
* | |
* TODO: | |
* | |
* spec .jobTitle | |
* .city() == .traits.city || traits.address.city | |
* .zip() == .traits.zip || traits.address.zip | |
* | |
* @param {Facade} msg | |
* @return {Object} | |
*/ | |
exports.identify = function (msg) { | |
var payload = traverse(formatTraits(msg.traits())) | |
payload = reject(extend(payload, { | |
jobtitle: msg.position(), | |
city: msg.city(), | |
zip: msg.zip(), | |
email: msg.email(), | |
firstname: msg.firstName(), | |
lastname: msg.lastName(), | |
address: msg.address(), | |
phone: msg.phone() | |
})) | |
// remove .position, .postalCode | |
del(payload, 'position') | |
return payload | |
} | |
/** | |
* lowercase & snakecase any trait with uppercase letters or spaces | |
* Hubspot cannot accept uppercases or spaces | |
* | |
* @api private | |
* @param {Object} traits | |
* @return {Object} ret | |
*/ | |
function formatTraits (traits) { | |
var ret = {} | |
each(function (value, key) { | |
var k = key.toLowerCase().replace(/\s/g, '_') | |
ret[k] = value | |
}, traits) | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment