DEV Community

Cover image for Dev.to Writing: Counting Views
Nathan Kallman
Nathan Kallman

Posted on

Dev.to Writing: Counting Views

@amananandrai asked a question that I myself have wondered; how are views counted on DEV?

I would like to know what is the duration for the updation of views on a post. As you know that DEV doesn't show live views of a particular post. It updates after sometime so I would like to know what is the average duration of update of no. of views of a post.

To answer this I went straight to the source (the open source ๐Ÿ˜‰) and started digging.

I learned quickly that everywhere page views are presented come from an attribute on the Article model called page_views_count. A reasonable name to be sure.

But what is less clear is how that value is set. Perhaps daily or hourly? I've observed some strange behavior on my own post view counts. Once immediately (within seconds of publishing) jumping to 33 views, then stalling; while other times stalling below the 25 mark until hours later when it magically jumps to 100. So what is happening?


Eventually I found the PageViewsController

This controller does two things:

  1. Creates a PageView record (brilliant) with a count of either 1 or 10, if the user is logged in or not (respectively).
  2. Updates the page_views_count on the related article, sometimes.

And it's this sometimes condition that answers our question of how page views get counted up. Look at this line:

return if Rails.env.production? && rand(15) != 1
Enter fullscreen mode Exit fullscreen mode

Rails.env.production? should always be true for the articles we write here on DEV, so for our purposes we can see this as just return if rand(15) != 1.

rand(max) in ruby returns a random integer, r, where 0 <= r < max.

So if your article's page views feel like they update at random times... that's because they do! About once every 15 times a PageView is recorded.


Oh, are you wondering about that weird 1 or 10 counting on the PageView record?

So was I. So, I dug up in the front-end code where the PageViewsController endpoint gets called. You can see for yourself.

There's a similar random filtering for non-logged-in users (and even a helpful comment!):

var randomNumber = Math.floor(Math.random() * 10); // 1 in 10; Only track 1 in 10 impressions
if (!checkUserLoggedIn() && randomNumber != 1) {
  return;
}
Enter fullscreen mode Exit fullscreen mode

The front-end will only call the API (on average) 1 in 10 times to create a PageView record for anonymous users. The back-end corrects for this by counting those views 10 times as much as the views logged in users have. On average, this works out to an accurate count of views, but has an interesting effect on our already random view counting logic.


So the final answer is: Views are updated randomly, averaging updates once every 15 to 150 views (depending on the mix of logged-in vs non-logged-in traffic your article draws); but being subject to randomness, updates can happen much more or much less frequently.


Thanks for reading! I hope this answered your question @amananandrai.

If you have a question of your own about writing on DEV, just ask in a comment. I read all of them and would love to add another post in this series that will actually help other writers in this awesome community!

Top comments (1)

Collapse
 
amananandrai profile image
amananandrai

Thanks @kallmanation this article has indeed answered my question and cleared my doubts about the randomness of the increase in page views. Thanks again for putting so much effort to gather details from the source code.