Adding documentation and constant (#16140)

* Adding documentation and constant

This loosely relates to a request for information from Success team
about whether or not viewing a draft article counts towards it's stats.

* Apply suggestions from code review

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
This commit is contained in:
Jeremy Friesen 2022-01-17 17:21:28 -05:00 committed by GitHub
parent 5534a8fa18
commit b6c58d329f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View file

@ -111,6 +111,9 @@ function trackCustomImpressions() {
// page view
if (ArticleElement && tokenMeta && !isBot) {
// See https://github.com/forem/forem/blob/main/app/controllers/page_views_controller.rb
//
// If you change the 10, you should look at the PageViewsController as well.
var randomNumber = Math.floor(Math.random() * 10); // 1 in 10; Only track 1 in 10 impressions
if (!checkUserLoggedIn() && randomNumber != 1) {
return;

View file

@ -3,12 +3,23 @@ class PageViewsController < ApplicationMetalController
# It should help performance.
include ActionController::Head
# Each time we have a non-authenticated visitor, we have a 10% chance that we will record that
# specific impression as a page view. When we do record that specific impression as a page view,
# we want to give credit for all likely page views that we didn't record (e.g., the 90% or so).
#
# @note Yes, this is a very verbose constant name. Apologies, don't type it. But I [@jeremyf]
# want it here to explain a magic number.
#
# @see https://github.com/forem/forem/blob/main/app/assets/javascripts/initializers/initializeBaseTracking.js.erb#L113-L117
# @see https://github.com/forem/forem/pull/12686#discussion_r577271589 for further discussion.
VISITOR_IMPRESSIONS_AGGREGATE_COUNTS_FOR_NUMBER_OF_VIEWS = 10
def create
page_view_create_params = params.slice(:article_id, :referrer, :user_agent)
if session_current_user_id
page_view_create_params[:user_id] = session_current_user_id
else
page_view_create_params[:counts_for_number_of_views] = 10
page_view_create_params[:counts_for_number_of_views] = VISITOR_IMPRESSIONS_AGGREGATE_COUNTS_FOR_NUMBER_OF_VIEWS
end
Articles::UpdatePageViewsWorker.perform_at(