To reflect a customer's favorite brand based on what they view or buy most frequently, use aggregates and expressions to create an attribute that is dynamically updated based on the customer's activity.
Prerequisites
Before starting, make sure you're tracking purchase or view_item events with a brand attribute.
Setting up the aggregates
Go to Data & Assets > Data Manager > Definitions and create a new aggregate.
Select the most common operator, your
purchaseorview_itemevent, and brand attribute. You can also set a date filter to target a specific timeframe.- If one attribute is enough, save the aggregate and use it in your campaigns right away.
- If you need to check both purchased and viewed brands, create a second aggregate with the most common
view_itembrand and an expression from the guide below. For example, if you want to show a recommendation but the customer hasn't purchased anything yet, use their most-viewed brand as a fallback.
Setting up the expression
Go to Data & Assets > Data Manager > Definitions and create a new Expression.
.
The expression will use an ifnull() function – if the first aggregate you add doesn't have any value, it will return the value of the second one. To select it, click on '+ Function' and then 'ifnull'.
Click on the 'select' field, then 'Select an attribute' dropdown, and select your first aggregate. If this aggregate has value, the expression will return it.
Repeat the previous step on the next field to add your second aggregate. This is used as a fallback value that returns the most-viewed brand if the customer has no
purchaseevent but hasview_itemevents.- Save the expression. Now you can use it in your campaigns.
Notes
- The most common aggregate returns the value that appears most often for the selected attribute in a customer's event history. If there's a tie, it returns the one that appeared first in the event history.
- It works with any tracked attribute type, not just strings. For list/array attributes, the entire list is treated as a single value so that the result may be the most common list combination rather than the most common item within it.
- Empty strings are also treated as valid values, allowing the aggregate to return an empty result. To prevent this, add a has value condition to exclude empty values.
- The same applies in expressions: if the aggregate returns an empty string instead of
null,ifnull()will not use the fallback value.