I am currently building a small ecommerce site and for the home page, I would like to rank the products based on popularity. The product schema has the following data:
{
...
noViews: Number, // Number of times a user has clicked on the product
avgRating: Number, // Average star rating (1 - 5)
datePosted: Date // Date the product was posted
}
What is a (relatively) simple algorithm/code I can use to implement this (with Node.js), where the products would be ranked based on popularity (with time taken into consideration so the content isn't stale).
Thank you.
First of all, you need a time based metric to solve this.
Overall trending products does not make much sense particularly for a ecommerce app.
Lets say, you want to do find the trending products for a particular day (say today).
Now this is not a easy problem to solve. And there is no just 1 direct way to solve it. For each company, one method works!
Ideally , what could be a simple solution here would be to create a score value based on combination of factors. For example, number of buys, number of views, number of clicks and so on.
Here, you have the datePosted
parameter. Now, create a date_score for this. So, the basic idea here is a product that was posted yesterday has more score than the one posted a week back. The values you put need to tweaked and checked for your algorithm's customization.
Similarly, implemeent a similar score for avgRating
and noViews
.
Once you have these scores ready, create weights for these scores. Now the weighted average for these out of 100 is the final score.
So, a final example solution:
Date Posted:
Star Rating:
numberOfViews:
Now, handle the corner case of what happens when two scores match.
After this, you can simply order by the score parameter.
Also, make sure everything you do is dynamic as static thresholds dont provide a great resul generally!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With