Sorting Products by Custom Score in recommendations | Community
Skip to main content
ZvoidZ
New Participant
February 7, 2024
Question

Sorting Products by Custom Score in recommendations

  • February 7, 2024
  • 2 replies
  • 1309 views

 

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!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

New Participant
February 20, 2024


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.

Employee
February 7, 2024

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.html?lang=en#weighting

 

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:

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.