Count the total number of times an event has occured for a user
//compute total event count;
const counts = _.reduce(events, (c, e) => {
const { event } = e;
//Remove invalid characters from Name
const eventName = event.toLowerCase().replace(/\.\$/, '');
//Fetch or initialize the counter increment
const operation = c[eventName] || { value: 1, operation: 'inc' };
//Increment Counter for this event name
operation.value++;
c[eventName] = operation;
return c;
}, {});
/*
counts == {
"viewed page": { operation: 'inc', value: 2},
"email open": { operation: 'inc', value: 2},
...
}
*/
//Write in Hull Profile, in `event_counts` group;
hull.traits(counts, { source: 'event_counts' });