discourse/app/models/concerns/reports/site_traffic.rb
Martin Brennan 14b436923c
FEATURE: Switch to new methods of pageview measurement and reporting (#28729)
### UI changes

All of the UI changes described are gated behind the `use_legacy_pageviews`
site setting.

This commit changes the admin dashboard pageviews report to
use the "Consolidated Pageviews with Browser Detection" report
introduced in 2f2da72747 with
the following changes:

* The report name is changed to "Site traffic"
* The pageview count on the dashboard is counting only using the new method
* The old "Consolidated Pageviews" report is renamed as "Consolidated Legacy Pageviews"
* By default "known crawlers" and "other" sources of pageviews are hidden on the report

When `use_legacy_pageviews` is `true`, we do not show or allow running
the "Site traffic" report for admins. When `use_legacy_pageviews` is `false`,
we do not show or allow running the following legacy reports:

* consolidated_page_views
* consolidated_page_views_browser_detection
* page_view_anon_reqs
* page_view_logged_in_reqs

### Historical data changes

Also part of this change is that, since we introduced our new "Consolidated
Pageviews with Browser Detection" report, some admins are confused at either:

* The lack of data before a certain date , which didn’t exist before
  we started collecting it
* Comparing this and the current "Consolidated Pageviews" report data,
  which rolls up "Other Pageviews" into "Anonymous Browser" and so it
  appears inaccurate

All pageview data in the new report before the date where the _first_
anon or logged in browser pageview was recorded is now hidden.
2024-09-10 09:51:49 +10:00

81 lines
3.2 KiB
Ruby

# frozen_string_literal: true
module Reports::SiteTraffic
extend ActiveSupport::Concern
class_methods do
def report_site_traffic(report)
report.modes = [:stacked_chart]
first_browser_pageview_date =
DB.query_single(
<<~SQL,
SELECT date FROM application_requests
WHERE req_type = :page_view_logged_in_browser OR req_type = :page_view_anon_browser ORDER BY date LIMIT 1
SQL
page_view_logged_in_browser: ApplicationRequest.req_types[:page_view_logged_in_browser],
page_view_anon_browser: ApplicationRequest.req_types[:page_view_anon_browser],
).first
data =
DB.query(
<<~SQL,
SELECT
date,
SUM(CASE WHEN req_type = :page_view_logged_in_browser THEN count ELSE 0 END) AS page_view_logged_in_browser,
SUM(CASE WHEN req_type = :page_view_anon_browser THEN count ELSE 0 END) AS page_view_anon_browser,
SUM(CASE WHEN req_type = :page_view_crawler THEN count ELSE 0 END) AS page_view_crawler,
SUM(
CASE WHEN req_type = :page_view_anon THEN count
WHEN req_type = :page_view_logged_in THEN count
WHEN req_type = :page_view_anon_browser THEN -count
WHEN req_type = :page_view_logged_in_browser THEN -count
ELSE 0
END
) AS page_view_other
FROM application_requests
WHERE date >= :start_date AND date <= :end_date AND date >= :first_browser_pageview_date
GROUP BY date
ORDER BY date ASC
SQL
start_date: report.start_date,
end_date: report.end_date,
page_view_anon: ApplicationRequest.req_types[:page_view_anon],
page_view_crawler: ApplicationRequest.req_types[:page_view_crawler],
page_view_logged_in: ApplicationRequest.req_types[:page_view_logged_in],
page_view_anon_browser: ApplicationRequest.req_types[:page_view_anon_browser],
page_view_logged_in_browser: ApplicationRequest.req_types[:page_view_logged_in_browser],
first_browser_pageview_date: first_browser_pageview_date,
)
report.data = [
{
req: "page_view_logged_in_browser",
label: I18n.t("reports.site_traffic.xaxis.page_view_logged_in_browser"),
color: report.colors[:turquoise],
data: data.map { |row| { x: row.date, y: row.page_view_logged_in_browser } },
},
{
req: "page_view_anon_browser",
label: I18n.t("reports.site_traffic.xaxis.page_view_anon_browser"),
color: report.colors[:lime],
data: data.map { |row| { x: row.date, y: row.page_view_anon_browser } },
},
{
req: "page_view_crawler",
label: I18n.t("reports.site_traffic.xaxis.page_view_crawler"),
color: report.colors[:purple],
data: data.map { |row| { x: row.date, y: row.page_view_crawler } },
},
{
req: "page_view_other",
label: I18n.t("reports.site_traffic.xaxis.page_view_other"),
color: report.colors[:magenta],
data: data.map { |row| { x: row.date, y: row.page_view_other } },
},
]
end
end
end