Expand my Community achievements bar.

Join us for an upcoming in-person Adobe Target Skill Builders event ~~> We're hosting these live learning opportunities to equip you with the knowledge and skills to leverage Target successfully. Learn more to see if we'll be coming to a city near you!

Sorting Products by Custom Score in recommendations

Avatar

Level 3

 

Hello everyone,

 

In the context of Adobe Target recommendations, I've encountered a specific need. I've created a custom entity called "score," which ranges from 1 to 100, and I populate this entity based on internal rules.

 

My question is as follows: How can I configure a criteria or segment to have products displayed in descending order based on their "score" values? The goal is to show products with higher scores at the top of the list, followed by products with lower scores.

 

I appreciate in advance for any guidance or suggestions!

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

2 Replies

Avatar

Employee Advisor

Hi @ZvoidZ ,

You have a few options. One option is to use the attribute weighting feature in the criteria to nudge entities up or down based on its "score":

https://experienceleague.adobe.com/docs/target/using/recommendations/criteria/create-new-algorithm.h...

 

Another option, if you really want to force the sort order of the entities is to do it in your design. I do this to deliver the "What's new" recommendations at the top of various tutorial overview pages on Experience League. Here is an example, where I force the order of the recommendations to display based on a date entity:

dwright_0-1707322128725.png

https://experienceleague.adobe.com/docs/platform-learn/tutorials/overview.html?lang=en

I loop through the entities in Velocity and construct a javascript array of objects, which I then sort in javascript, roughly this:

 

 

<script>
const recsEntities = [
    #foreach($e in $entities )
        {name: "$e.name", pageUrl: "$e.pageUrl?lang=$lang", message: "$e.message", lastSubstantialUpdate: "$e.lastSubstantialUpdate" },
    #end
];
// sort by last-substantial-update
recsEntities.sort((a, b) => (a.lastSubstantialUpdate > b.lastSubstantialUpdate) ? -1 : 1)
</script>

 

 

 Then I have additional javascript code in my design that loops through the ordered objects and constructs the HTML as needed.

Avatar

Level 3


Here's a simplified example of how you might implement this logic:

javascript

// Access product elements on the page
const productElements = document.querySelectorAll('.product');

// Convert NodeList to Array for easier manipulation
const products = Array.from(productElements);

// Sort products by score in descending order
products.sort((a, b) => {
const scoreA = parseInt(a.dataset.score);
const scoreB = parseInt(b.dataset.score);
return scoreB - scoreA;
});

// Update display order
const productListContainer = document.querySelector('.product-list');
products.forEach(product => {
productListContainer.appendChild(product);
});


In this example:

.product represents the CSS class of individual product elements.
dataset.score refers to the custom data attribute (data-score) where the "score" value is stored for each product.
.product-list represents the container element where the sorted product elements will be appended.
Make sure to adapt this code to fit the specific structure and behavior of your website or application.