Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

322
Views
Simple popular/trending products algorithm for an ecommerce site with Javascript

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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:

    • If Date is in the last 3 days (score = 100)
    • If Date is the last week (score= 75)
    • If Date in the previous week (score = 50)
    • Else default score = 25.
  • Star Rating:

    • (Rating/5 ) *100
  • numberOfViews:

    • Here , you can use percentiles.
    • Lets say max views among all the product is x.
    • Then, the view score is (views/x) *100

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!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!