Skip to content

A platform for community discussion. Free, open, simple.

FIX: Remove unselected filters from its dimension on site traffic explorer dashboard (#42851)

Alan Guo Xiang Tan1cbb25c9e0e6
diff --git a/app/services/admin_dashboard_site_traffic_explorer.rb b/app/services/admin_dashboard_site_traffic_explorer.rb
index c6ea409609d..32551604076 100644
--- a/app/services/admin_dashboard_site_traffic_explorer.rb
+++ b/app/services/admin_dashboard_site_traffic_explorer.rb
@@ -175,7 +175,7 @@ class AdminDashboardSiteTrafficExplorer
}
end
- def filter_predicate(except: nil)
+ def filter_predicate
predicates = {
top_url: "(NOT :top_url_filtered OR normalized_url IN (:top_urls))",
entry_url: "(NOT :entry_url_filtered OR entry_url IN (:entry_urls))",
@@ -194,7 +194,7 @@ class AdminDashboardSiteTrafficExplorer
SQL
}
- predicates.except(except).values.join("\n AND ")
+ predicates.values.join("\n AND ")
end
def query(start_date:, end_date:)
@@ -417,20 +417,19 @@ class AdminDashboardSiteTrafficExplorer
SELECT COALESCE(
jsonb_agg(
jsonb_build_object('value', value, 'pageviews', pageviews)
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
),
'[]'::jsonb
)
FROM (
SELECT
normalized_url AS value,
- COUNT(*)::integer AS pageviews,
- :top_url_filtered AND normalized_url IN (:top_urls) AS selected
+ COUNT(*)::integer AS pageviews
FROM dimensioned
- WHERE #{filter_predicate(except: :top_url)}
+ WHERE #{filter_predicate}
AND normalized_url IS NOT NULL
GROUP BY normalized_url
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
LIMIT :dimension_limit
) rows
),
@@ -438,20 +437,19 @@ class AdminDashboardSiteTrafficExplorer
SELECT COALESCE(
jsonb_agg(
jsonb_build_object('value', value, 'pageviews', pageviews)
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
),
'[]'::jsonb
)
FROM (
SELECT
entry_url AS value,
- COUNT(*)::integer AS pageviews,
- :entry_url_filtered AND entry_url IN (:entry_urls) AS selected
+ COUNT(*)::integer AS pageviews
FROM dimensioned
- WHERE #{filter_predicate(except: :entry_url)}
+ WHERE #{filter_predicate}
AND entry_url IS NOT NULL
GROUP BY entry_url
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
LIMIT :dimension_limit
) rows
),
@@ -459,20 +457,19 @@ class AdminDashboardSiteTrafficExplorer
SELECT COALESCE(
jsonb_agg(
jsonb_build_object('value', value, 'pageviews', pageviews)
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
),
'[]'::jsonb
)
FROM (
SELECT
referrer AS value,
- COUNT(*)::integer AS pageviews,
- :referrer_filtered AND referrer IN (:referrers) AS selected
+ COUNT(*)::integer AS pageviews
FROM dimensioned
- WHERE #{filter_predicate(except: :referrer)}
+ WHERE #{filter_predicate}
AND referrer IS NOT NULL
GROUP BY referrer
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
LIMIT :dimension_limit
) rows
),
@@ -484,7 +481,7 @@ class AdminDashboardSiteTrafficExplorer
'pageviews', pageviews,
'representative_ip', representative_ip
)
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
),
'[]'::jsonb
)
@@ -492,13 +489,12 @@ class AdminDashboardSiteTrafficExplorer
SELECT
country_code AS value,
COUNT(*)::integer AS pageviews,
- host(MIN(ip_address)) AS representative_ip,
- :country_filtered AND country_code IN (:countries) AS selected
+ host(MIN(ip_address)) AS representative_ip
FROM dimensioned
- WHERE #{filter_predicate(except: :country)}
+ WHERE #{filter_predicate}
AND country_code IS NOT NULL
GROUP BY country_code
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
LIMIT :dimension_limit
) rows
),
@@ -510,7 +506,7 @@ class AdminDashboardSiteTrafficExplorer
'pageviews', pageviews,
'representative_ip', representative_ip
)
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
),
'[]'::jsonb
)
@@ -518,13 +514,12 @@ class AdminDashboardSiteTrafficExplorer
SELECT
'AS' || asn AS value,
COUNT(*)::integer AS pageviews,
- host(MIN(ip_address)) AS representative_ip,
- :network_filtered AND asn IN (:network_asns) AS selected
+ host(MIN(ip_address)) AS representative_ip
FROM dimensioned
- WHERE #{filter_predicate(except: :network)}
+ WHERE #{filter_predicate}
AND asn IS NOT NULL
GROUP BY asn
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
LIMIT :dimension_limit
) rows
),
@@ -532,19 +527,18 @@ class AdminDashboardSiteTrafficExplorer
SELECT COALESCE(
jsonb_agg(
jsonb_build_object('value', value, 'pageviews', pageviews)
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
),
'[]'::jsonb
)
FROM (
SELECT
browser AS value,
- COUNT(*)::integer AS pageviews,
- :browser_filtered AND browser IN (:browsers) AS selected
+ COUNT(*)::integer AS pageviews
FROM dimensioned
- WHERE #{filter_predicate(except: :browser)}
+ WHERE #{filter_predicate}
GROUP BY browser
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
LIMIT :dimension_limit
) rows
),
@@ -552,19 +546,18 @@ class AdminDashboardSiteTrafficExplorer
SELECT COALESCE(
jsonb_agg(
jsonb_build_object('value', value, 'pageviews', pageviews)
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
),
'[]'::jsonb
)
FROM (
SELECT
host(ip_address) AS value,
- COUNT(*)::integer AS pageviews,
- :ip_filtered AND host(ip_address) IN (:ip_addresses) AS selected
+ COUNT(*)::integer AS pageviews
FROM dimensioned
- WHERE #{filter_predicate(except: :ip)}
+ WHERE #{filter_predicate}
GROUP BY ip_address
- ORDER BY selected DESC, pageviews DESC, value
+ ORDER BY pageviews DESC, value
LIMIT :dimension_limit
) rows
)
diff --git a/spec/requests/admin/dashboard_controller_spec.rb b/spec/requests/admin/dashboard_controller_spec.rb
index b6d0c07b853..634e74a03b7 100644
--- a/spec/requests/admin/dashboard_controller_spec.rb
+++ b/spec/requests/admin/dashboard_controller_spec.rb
@@ -1597,14 +1597,15 @@ RSpec.describe Admin::DashboardController do
{ "key" => "country", "value" => "US", "label" => "United States" },
],
)
- expect(response.parsed_body.dig("dimensions", "referrers")).to eq(
- [
+ expect(response.parsed_body.fetch("dimensions").slice("referrers", "countries")).to eq(
+ "referrers" => [
{
"value" => "search.example/results?q=discourse",
"label" => "search.example/results?q=discourse",
"pageviews" => 1,
},
],
+ "countries" => [{ "value" => "US", "label" => "United States", "pageviews" => 1 }],
)
end
diff --git a/spec/services/admin_dashboard_site_traffic_explorer_spec.rb b/spec/services/admin_dashboard_site_traffic_explorer_spec.rb
index 70fad56b205..98aed577422 100644
--- a/spec/services/admin_dashboard_site_traffic_explorer_spec.rb
+++ b/spec/services/admin_dashboard_site_traffic_explorer_spec.rb
@@ -91,7 +91,7 @@ RSpec.describe AdminDashboardSiteTrafficExplorer do
context "when the query succeeds" do
it { is_expected.to run_successfully }
- it "groups stored browser values and displays rows awaiting backfill as unknown" do
+ it "orders browser dimensions by pageviews" do
browser_dimensions = result.traffic.dig(:dimensions, "browsers")
expect(browser_dimensions).to eq(
@@ -105,6 +105,61 @@ RSpec.describe AdminDashboardSiteTrafficExplorer do
)
end
+ it "applies all filters to every dimension" do
+ Fabricate(
+ :browser_pageview_event,
+ url: "/other",
+ country_code: "GB",
+ asn: 64_500,
+ ip_address: "198.51.100.1",
+ normalized_referrer: "other.example",
+ normalized_referrer_version: BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,
+ session_id: "other",
+ source: BrowserPageviewEvent::SOURCE_BEACON,
+ browser: :chrome,
+ created_at: Time.zone.local(2026, 5, 10, 11),
+ )
+ DiscourseIpInfo
+ .stubs(:get)
+ .with do |ip, **options|
+ ip.to_s.start_with?("192.0.2.") &&
+ options == { locale: I18n.locale, resolve_hostname: false }
+ end
+ .returns(
+ country_code: "US",
+ country: "United States",
+ asn: 64_496,
+ organization: "Example Network",
+ )
+
+ dimensions =
+ described_class.call(params: params.merge(country: "US", browser: "chrome")).traffic[
+ :dimensions
+ ]
+
+ expect(dimensions).to eq(
+ "top_urls" => [
+ { value: "/browser-4", label: "/browser-4", pageviews: 1 },
+ { value: "/browser-5", label: "/browser-5", pageviews: 1 },
+ { value: "/browser-6", label: "/browser-6", pageviews: 1 },
+ ],
+ "entry_urls" => [
+ { value: "/browser-4", label: "/browser-4", pageviews: 1 },
+ { value: "/browser-5", label: "/browser-5", pageviews: 1 },
+ { value: "/browser-6", label: "/browser-6", pageviews: 1 },
+ ],
+ "referrers" => [{ value: "", label: "Direct / unknown", pageviews: 3 }],
+ "countries" => [{ value: "US", label: "United States", pageviews: 3 }],
+ "networks" => [{ value: "AS64496", label: "Example Network (AS64496)", pageviews: 3 }],
+ "browsers" => [{ value: "chrome", label: "Google Chrome", pageviews: 3 }],
+ "ip_addresses" => [
+ { value: "192.0.2.5", label: "192.0.2.5", pageviews: 1 },
+ { value: "192.0.2.6", label: "192.0.2.6", pageviews: 1 },
+ { value: "192.0.2.7", label: "192.0.2.7", pageviews: 1 },
+ ],
+ )
+ end
+
it "uses only local IP data to produce country and network labels" do
DiscourseIpInfo
.expects(:get)
diff --git a/spec/system/admin_dashboard_redesign/site_traffic_explorer_spec.rb b/spec/system/admin_dashboard_redesign/site_traffic_explorer_spec.rb
index ca0d636aa11..65ef6d362cc 100644
--- a/spec/system/admin_dashboard_redesign/site_traffic_explorer_spec.rb
+++ b/spec/system/admin_dashboard_redesign/site_traffic_explorer_spec.rb
@@ -474,6 +474,21 @@ RSpec.describe "Admin Dashboard Redesign | Site Traffic Explorer" do
expect(page).to have_current_path(
"/admin/dashboard/site-traffic-explorer?country=US&end_date=2026-05-12&range=custom&referrer=%5B%22one.example%22%2C%22two.example%22%2C%22three.example%22%5D&start_date=2026-05-01",
)
+ expect(traffic).to have_rows(
+ card: "acquisition",
+ rows: [
+ { label: "one.example", count: "1" },
+ { label: "two.example", count: "1" },
+ { label: "three.example", count: "1" },
+ ],
+ )
+
+ traffic.select_tab(card: "acquisition", tab: "Countries")
+
+ expect(traffic).to have_rows(
+ card: "acquisition",
+ rows: [{ label: "United States", count: "3" }],
+ )
page.refresh
diff --git a/spec/system/page_objects/pages/admin_site_traffic_explorer.rb b/spec/system/page_objects/pages/admin_site_traffic_explorer.rb
index 46f4d2af076..db3039b9e92 100644
--- a/spec/system/page_objects/pages/admin_site_traffic_explorer.rb
+++ b/spec/system/page_objects/pages/admin_site_traffic_explorer.rb
@@ -104,6 +104,22 @@ module PageObjects
end
end
+ def has_rows?(card:, rows:)
+ within("[data-test-site-traffic-card='#{card}']") do
+ return false unless has_css?("[data-test-site-traffic-row]", count: rows.size)
+
+ actual_rows =
+ all("[data-test-site-traffic-row]").map do |row|
+ {
+ label: row.find(".site-traffic-explorer__dimension-text").text,
+ count: row.find(".site-traffic-explorer__row-count").text,
+ }
+ end
+
+ actual_rows == rows
+ end
+ end
+
def has_url_link?(card:, label:, href:)
within("[data-test-site-traffic-card='#{card}']") do
has_css?("a[href='#{href}']", exact_text: label)