Last active
September 2, 2015 22:16
-
-
Save sperand-io/77e8471e7f53ae70da4d to your computer and use it in GitHub Desktop.
Sending Some Traits to Some Tools But Not To Others!
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
// these are the trait keys you want to send to customer.io | |
var importantTraits = ['trait1', 'trait2', 'trait3']; | |
function identify(userId, traits) { | |
traits = extend({}, traits || {}); | |
var matchingTraits = reduce(function(acc, val, key){ | |
if (~importantTraits.indexOf(key)) acc[key] = val; | |
return acc; | |
}, {}, traits); | |
each(function(obj) { | |
analytics.identify(userId, obj.traits, { integrations: obj.context }, function(){ | |
analytics.user().traits({}); | |
}) | |
}, [ | |
{ | |
traits: traits, | |
context: { "All": true, "Customer.io": false } | |
}, | |
{ | |
traits: matchingTraits, | |
context: { "All": false, "Customer.io": true } | |
} | |
]); | |
} | |
// @segmentio/extend | |
function extend(object /*, ...sources */) { | |
var sources = Array.prototype.slice.call(arguments, 1); | |
for (var i = 0, source; source = sources[i]; i++) { | |
if (!source) continue; | |
for (var property in source) { | |
object[property] = source[property]; | |
} | |
} | |
return object; | |
} | |
// @ndhoule/foldl | |
function reduce(iterator, accumulator, collection) { | |
each(function(val, key, collection) { | |
accumulator = iterator(accumulator, val, key, collection); | |
}, collection); | |
return accumulator; | |
} | |
// @ndhoule/each | |
function each(iterator, object) { | |
var ks = keys(object); | |
for (var i = 0; i < ks.length; i += 1) { | |
if (iterator(object[ks[i]], ks[i], object) === false) { | |
break; | |
} | |
} | |
} | |
// @ndhoule/keys | |
function keys(object) { | |
var results = []; | |
for (var key in object) { | |
if (Object.prototype.hasOwnProperty.call(object, key)) { | |
results.push(String(key)); | |
} | |
} | |
return results; | |
} |
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
identify('123', { trait1: 'this will go through to customer.io', trait4: 'this wont!' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment