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

196
Views
How to increase click count value when visiting an URL

I am building an URL shortener. So I want to track datewise click count when a URL is visited using the short URL. For example: on 24th January if the URL was visited by using the short URL it will show the click count. I want the data to look like this -

dateWiseCount: [
      {
        date: '01/24/2022',
        clickCount: 5,
      },
      {
        date: '01/25/2022',
        clickCount: 8,
      },
]

I have been able to get the total number of clicks when a URL is visited by using the short URL but for some reason whenever I hit the short URL it is registering date wise click Count like this -

dateWiseCount: [
      {
        date: '01/24/2022',
        clickCount: 0,
      },
      {
        date: '01/24/2022',
        clickCount: 0,
      },
]

So it is not increasing the click count and also creating a new array object with the same date. This is what I am getting from MongoDB -

"totalClicks": 2,
    "dayWiseClicks": [{
        "dailyClicks": 0,
        "_id": {
            "$oid": "61ee56f42b85726d004d535d"
        },
        "date": {
            "$date": "2022-01-24T07:36:20.429Z"
        }
    }, {
        "dailyClicks": 0,
        "_id": {
            "$oid": "61ee57172b85726d004d5361"
        },
        "date": {
            "$date": "2022-01-24T07:36:55.684Z"
        }
    }],

This is my code :

exports.redirect = async (req, res) => {
  const url_hash = req.params.shortUrl;

  await URL.findOne({ url_hash })
    .then((url) => {
      const condition = { _id: url.id };
      let counter = 0;
      const dayWiseNewClicks = {
        date: new Date(),
        dailyClicks: counter++,
      };
      const update = { $push: { dayWiseClicks: dayWiseNewClicks } };
      if (url) {
        url.totalClicks++;
        URL.updateOne(condition, update)
          .then(() => {
            console.log("Push Succesful");
          })
          .catch((error) => {
            return res.status(404).json({ updateError: error.message });
          });
        url.save();
        return res.redirect(url.original_url);
      } else {
        return res.status(404).json({ error: "No Url Found" });
      }
    })
    .catch((error) => {
      return res.status(404).json({ error: error.message });
    });
};

How Can I solve this problem? Please help me out. Stuck on this for a few hours now.

about 4 years ago · Juan Pablo Isaza
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!