Home > Enterprise >  How to count the number of views on a post
How to count the number of views on a post

Time:06-01

I am trying to make a website containing posts so I have a 'Read more' link which expands the content there and there only like quora. How do I know the total number of times it has been clicked so that I can display the views?

I am using nodejs, express and mongodb as the server.

This is my hbs (handlebars) file with that read more link

    <p >{{stripTags (truncate body 200)}}<span  >... 
    </span></p>
    <span >
    <p>{{stripTags (truncate body 400)}}</p>
    </span>
    <a onclick="readMoreFunction(this)"  >Read more</a>
    </div>

This is my main hbs file which executes all the js its script tags

    <script>
  function readMoreFunction(el) {
    var parent = el.closest(".wrapper")
    var dots = parent.querySelector(".dots");
    var contentText = parent.querySelector(".content");
    var btnText = parent.querySelector(".buttonReadMore");
    var startPara=parent.querySelector(".startPara");

  

    btnText.style.display = "none";
      startPara.style.display= "none";
      dots.style.display = "none";
      contentText.style.display = "inline";
    
  }

</script>

Now every post is unique clicked by a unique user so just incrementing a variable won't help.

So please tell me how to calculate the total number of times that link has been clicked for a specific post and then how to communicate it to the database so that I can display it on the webpage

CodePudding user response:

I imagine you would need to have a 'views' property on your Post in MongoDB.

You could serve the first 400 characters for each post in your getPosts api request.

Clicking read more could call getPost(id) on your api, returning the full post payload, during that process you could increment post.views by one.

Additionally as @tobiv suggests in the comments you could have code that ensures only unique visitor clicks are counted.

CodePudding user response:

Store an object in the viewers session and db column called 'viewed_posts'. Use the object to determine whether they viewed a post or not.

if (typeof(req.session.viewed_posts[post_id]) == 'undefined') {
  req.session.viewed_posts[post_id] = 1; //update session
  //increment post count in posts table with post id
  //update 'viewed_posts' column with new session 
}

//select post

When the object in 'viewed_posts' gets too large (say 3000 posts viewed), delete it. If the average viewer reads 2 unique posts a day, it will be 4.5 years until the second unique view MIGHT occur on the same post.

Although this does not guarantee uniqueness forever (still a long time), it is much more efficient because..

  1. You avoid a views table completely

  2. The views count in the posts table is a single number (No searching)

  3. The viewer does not have to search a table to see if they already viewed a post. They just have to refer to their session.

Use varchar(x) AND the incremented id NOT the uuid to store the object.

Minimum complexity is O(2), maximum is O(4)

Minimum network requests 1, maximum 3

  • Related