unity
6/1/2018 - 7:13 PM

Store Last Events dates as Attributes

const mappings = {
  'Email Opened': 'last_email_opened',
  'Email Clicked': 'last_email_clicked',
  'Email Link Clicked': 'last_email_clicked',
  'Loaded dashboard': 'last_active',
  'Created segment': 'last_saved_segment',
  'Updated segment': 'last_saved_segment',
  'Created users segment': 'last_saved_segment',
  'Updated users segment': 'last_saved_segment',
  'Created accounts segment': 'last_saved_segment',
  'Updated accounts segment': 'last_saved_segment',
  'Viewed users': 'last_viewed_user_profiles',
  'Viewed ships': 'last_viewed_connectors',
  'Viewed attributes': 'last_viewed_attributes',
  'Viewed accounts': 'last_viewed_accounts',
  'Viewed logs': 'last_viewed_logs',
  'Viewed settings': 'last_viewed_settings'
};


//Loop over events to store the last time they happened as an attribute.
event.reduce((attributes, e) => {
  const { event, created_at } = e;
  const attribute = mappings[event];
  if (attribute && created_at) {
    attributes[attribute] = created_at;
  }
  return attributes;
}, {});


//Store results.
if (_.keys(attributes).length > 0) {
  hull.traits(attributes, { source: 'recency_activitylevel' });
}