Number of page views for content

I am developing a website where I need to track the number of views for specific content (say, articles).

Its very similar to the stackoverflow view for the question.

I do not know how stackoverflow does it. It basically should

  • Enlarge the view when a user, other than the author of the article, visits the article. If he continues to click the refresh button, he should not increase the view.
  • Do not increase the view if the visitor constantly clicks the refresh button. But it should increase for the first visit.

I have a field in the database to store the number of views.

Does anyone know how stackoverflow does this? Is there a plugin in rails that I can use? I think I should use sessions and an IP address for tracking ... Should I use logs? OR Google Analytics?

help...

+3
source share
2 answers

Depending on how accurately you need it, you probably need a table containing the records for which the user has read this article. Their MAC address is not sent with their HTTP request, and their IP address (v4) is probably not unique.

If you want to display data (e.g. stackoverflow), you will not be able to use analytics.

, , Rails.

Pseudo-ish code ( API ):

ArticlesController < ApplicationController
  def show
    @article = Article.find(params[:id])
    # Create an ArticleReading (your join table) for this user unless one already exists
    current_user.article_readings.find_or_create_by_article_id(@article)
    # Count the number of users that have read this article
    @article_readings = ArticleReading.count(@article.id)
  end
end

- "", , , (, , ). google.

+1

, , , , MAC- . , , , MAC-. , , , mac.

0

Source: https://habr.com/ru/post/1776960/


All Articles