unity
6/1/2018 - 5:45 PM

Attribute and Event-based scoring

//Create an array of all the points we give
const scores = [];

//Helper method to push to the scores array.
const addToScore = points => points && scores.push(points);


//Scoring based on User Attributes.
const {
  is_beta,
  is_invited,
  trial_expired,
  embed_code_installed,
  trial_length,
  totalcustomproperties,
  account_domains,
  total_flows,
} = user.traits;

if (is_beta === true) addToScore(3);
if (trial_expired === true) addToScore(-1);
if (embed_code_installed === true) addToScore(8);
if (trial_length && trial_length > 14) addToScore(3);
if (is_invited === true) addToScore(5);
if (account_domains.length === 1) addToScore(-1);
if (total_flows) {
  if (total_flows >= 5) {
    addToScore(total_flows*15)
  } else if (total_flows >= 3) {
    addToScore(total_flows*5);
  } else {
    addToScore(total_flows*2);
  }
}


//compute total score based on attribute from scratch,
//and save as an attribute.
if(scores.length>0) {
  const total = _.sum(scores);
  hull.traits({
    attribute_score: total
  });
}


//Scoring based on User Events

//Another way to do so, just use a number...
const event_score = 0;

//Loop over every event
events.map(e => {
  const { event, properties, context } = e // event: "Email Opened"
  if (event === "Email Opened") event_score += 2;
  if (event === "Requested Demo" && properties.optin===true) event_score += 3;
});


// Take care. Here we INCREMENT the score instead of resetting it...
// We do this with the {operation: "inc", value:xxx} hash
// And we do it because the Processor does NOT go over every event,
// But rather only over the NEW events when it runs.
if (event_score!==0) {
  hull.traits({
    event_score: { operation: "inc", value: event_score }
  })
}