Dev.to Writing: Counting Views
Tags: meta, writing, tutorial, howtodevto
Published: 2020-08-21 15:55:09

[@amananandrai](https://dev.to/amananandrai) asked a question that I myself have wondered; how are views counted on DEV?

{% comment 13jaj %}

To answer this I went straight to the [source](https://github.com/forem/forem) (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`](https://github.com/forem/forem/blob/10788a50161d7092749d7c575b1cfdf6b07479cf/app/controllers/page_views_controller.rb)

This controller does two things:
1. [Creates a `PageView` record](https://github.com/forem/forem/blob/10788a50161d7092749d7c575b1cfdf6b07479cf/app/controllers/page_views_controller.rb#L7-L13) (brilliant) with a count of either 1 or 10, if the user is logged in or not (respectively).
2. [Updates the `page_views_count`](https://github.com/forem/forem/blob/10788a50161d7092749d7c575b1cfdf6b07479cf/app/controllers/page_views_controller.rb#L34-L42) 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:
```ruby
return if Rails.env.production? && rand(15) != 1
```

`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](https://ruby-doc.org/core-2.6/Random.html#method-i-rand) 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.](https://github.com/forem/forem/blob/cca77291986754f028829822434177782824edc9/app/assets/javascripts/initializers/initializeBaseTracking.js.erb#L111-L121)

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

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

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!