FEATURE: Only count topic views for explicit/deferred tracked views (#27533)

Followup 2f2da7274732cba30d03b6c5c3a4194652cb6783

This commit moves topic view tracking from happening
every time a Topic is requested, which is susceptible
to inflating numbers of views from web crawlers, to
our request tracker middleware.

In this new location, topic views are only tracked when
the following headers are sent:

* HTTP_DISCOURSE_TRACK_VIEW - This is sent on every page navigation when
  clicking around the ember app. We count these as browser page views
  because we know it comes from the AJAX call in our app. The topic ID
  is extracted from HTTP_DISCOURSE_TRACK_VIEW_TOPIC_ID
* HTTP_DISCOURSE_DEFERRED_TRACK_VIEW - Sent when MessageBus initializes
  after first loading the page to count the initial page load view. The
  topic ID is extracted from HTTP_DISCOURSE_DEFERRED_TRACK_VIEW.

This will bring topic views more in line with the change we
made to page views in the referenced commit and result in
more realistic topic view counts.
This commit is contained in:
Martin Brennan
2024-07-03 10:38:49 +10:00
committed by GitHub
parent 57af5d6f0d
commit 527f02e99f
12 changed files with 641 additions and 183 deletions

View File

@ -0,0 +1,49 @@
# frozen_string_literal: true
RSpec.describe "Middleware order" do
it "order of middleware in the application is correct" do
middlewares = Rails.configuration.middleware.map { |middleware| "#{middleware.inspect}" }
expect(middlewares).to eq(
%w[
BlockRequestsMiddleware
TestMultisiteMiddleware
ActionDispatch::RemoteIp
Middleware::RequestTracker
MessageBus::Rack::Middleware
ActionDispatch::HostAuthorization
Rack::Sendfile
ActionDispatch::Static
ActionDispatch::Executor
Rack::MethodOverride
Middleware::EnforceHostname
ActionDispatch::RequestId
SilenceLogger
ActionDispatch::ShowExceptions
ActionDispatch::DebugExceptions
ActionDispatch::Callbacks
ActionDispatch::Cookies
ActionDispatch::Session::DiscourseCookieStore
Discourse::Cors
ActionDispatch::Flash
RspecErrorTracker
Middleware::CspScriptNonceInjector
Middleware::AnonymousCache
ContentSecurityPolicy::Middleware
ActionDispatch::PermissionsPolicy::Middleware
Rack::Head
Rack::ConditionalGet
Rack::TempfileReaper
Middleware::OmniauthBypassMiddleware
],
)
end
it "ensures that ActionDispatch::RemoteIp comes before Middleware::RequestTracker" do
remote_ip_found = false
request_tracker_found = false
Rails.configuration.middleware.each do |middleware|
remote_ip_found = true if middleware.inspect == "ActionDispatch::RemoteIp"
expect(remote_ip_found).to eq(true) if middleware.inspect == "Middleware::RequestTracker"
end
end
end