Skip to content

Instantly share code, notes, and snippets.

@SMK1085 SMK1085/index.js Secret

Created Jul 21, 2020
Embed
What would you like to do?
Hull Code Hacks: Unified Exclusion Lists for Growth and Marketing
/*
* ---------------------------------------------------------------------------------
* Temporary objects to diff against, no need to check if something has
* changed, it will be done at the bottom automatically.
*
* Usage:
* `_.set(attribs, <NAME>, <VALUE>);` to set a new attribute value.
*
* Examples:
* `_.set(attribs, 'first_name', user.hubspot.first_name);` to set a top-level attribute
* `_.set(attribs, 'unified/first_name', user.hubspot.first_name);` to set an attribute in a group`
* `_.set(attribs, 'first_name', {
* value: user.hubspot.first_name,
* operation: 'setIfNull'
* });` to set a top-level attribute, but only if it is not set
*
* ---------------------------------------------------------------------------------
*/
let attribs = {};
/*
* ---------------------------------------------------------------------------------
* START YOUR LOGIC
* ---------------------------------------------------------------------------------
*/
// Handle exclusion list attributes
const exclusionAttribs = determineExlusionLists(user, segments);
attribs = _.merge(attribs, exclusionAttribs);
/*
* ---------------------------------------------------------------------------------
* END YOUR LOGIC
* ---------------------------------------------------------------------------------
*/
/*
* ---------------------------------------------------------------------------------
* START UTILITY FUNCTIONS
* ---------------------------------------------------------------------------------
*/
// If you have any utility functions, add them in this block, this helps your
// code staying tidy and everything is in a separate section.
// NOTE: Do not use const myFunction = (foo) => { ... };
// but use function myFunction(foo) { ... }
function determineExlusionLists(usr, segs) {
const grpName = _.get(variables, "unifiedgrp__name", "unified");
const changedAttribs = {};
let isExcludedInAnyCategory = false;
let isAnyExclusionDefined = false;
_.forIn(variables, (v, k) => {
if(_.startsWith(k, "unified_el_segments__")) {
const cat = k.replace("unified_el_segments__", "");
const exclusionSegments = splitVariableToList(v, ";");
const isExcludedInCat = _.some(segs, s => exclusionSegments.includes(s.name));
_.set(changedAttribs, `${grpName}/exclusion_list_${cat}`, isExcludedInCat);
if (isExcludedInCat === true) {
isExcludedInAnyCategory = true;
}
isAnyExclusionDefined = true;
}
});
if (isAnyExclusionDefined === true) {
_.set(changedAttribs, `${grpName}/exclusion_list_combined`, isExcludedInAnyCategory);
}
return changedAttribs;
}
function splitVariableToList(stringVal, separator) {
return _.compact(stringVal.split(separator));
}
/*
* ---------------------------------------------------------------------------------
* END UTILITY FUNCTIONS
* ---------------------------------------------------------------------------------
*/
/*
* ---------------------------------------------------------------------------------
* Hull API calls
*
* Diffs automatically attribs against all attributes of the user
* and only performs calls to the API if needed. This helps you reduce billable
* incoming requests and prevents loops.
*
* A friendly reminder: Unless you know what you are doing, you should NOT
* change the code below. ;-)
*
* _.omitBy() == [].filter()
* ---------------------------------------------------------------------------------
*/
const diffedAttribs = _.omitBy( attribs, ( value, index ) => {
// `folder_name/attribute_name` > `user.folder_name.attribute_name`
const path = _.replace( index, '/', '.' );
if (path.endsWith("_date") || path.endsWith("_at")) {
return !_.isNil(_.get( user, path )) && moment(_.get( user, path )).isSame(moment(value), "second");
} else {
return _.isEqual( _.get( user, path ), value );
}
});
if ( Object.keys( diffedAttribs ).length !== 0 ) {
hull.traits( diffedAttribs );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.