A platform for community discussion. Free, open, simple.
FEATURE: Add the Site Traffic Explorer (#42399)
Alan Guo Xiang Tan21f3123c277c
diff --git a/app/assets/stylesheets/admin/admin-dashboard-skeleton.scss b/app/assets/stylesheets/admin/admin-dashboard-skeleton.scssindex 67cf59f34d5..38fad19eba8 100644--- a/app/assets/stylesheets/admin/admin-dashboard-skeleton.scss+++ b/app/assets/stylesheets/admin/admin-dashboard-skeleton.scss@@ -293,7 +293,7 @@90deg,rgb(var(--db-skeleton-animation-rgb), 0) 0,rgb(var(--db-skeleton-animation-rgb), 0.3) 50%,- rgb(var(--db-skeleton-animation-rgb), 0.5) 100%+ rgb(var(--db-skeleton-animation-rgb), 0) 100%);content: "";diff --git a/app/assets/stylesheets/admin/admin_dashboard.scss b/app/assets/stylesheets/admin/admin_dashboard.scssindex b5be78ef13c..e236f835bee 100644--- a/app/assets/stylesheets/admin/admin_dashboard.scss+++ b/app/assets/stylesheets/admin/admin_dashboard.scss@@ -7,6 +7,7 @@ $db-bar-height: var(--space-2);@import "admin/dashboard/reports";@import "admin/dashboard/search";@import "admin/dashboard/traffic";+@import "admin/dashboard/site-traffic-explorer";.db-site-advice {border: 1px solid var(--content-border-color);diff --git a/app/assets/stylesheets/admin/dashboard/site-traffic-explorer.scss b/app/assets/stylesheets/admin/dashboard/site-traffic-explorer.scssnew file mode 100644index 00000000000..150d52de455--- /dev/null+++ b/app/assets/stylesheets/admin/dashboard/site-traffic-explorer.scss@@ -0,0 +1,351 @@+@use "lib/viewport";++.site-traffic-explorer {+ .d-page-header {+ margin-bottom: var(--space-4);+ }++ &__content {+ display: flex;+ flex-direction: column;+ gap: var(--space-4);+ padding-bottom: var(--space-8);+ }++ &__filters {+ display: flex;+ flex-wrap: wrap;+ align-items: center;+ gap: var(--space-2);+ }++ &__filter-pill {+ display: inline-flex;+ align-items: center;+ gap: var(--space-1);+ padding-inline-start: var(--space-3);+ border-radius: 2rem;+ background: var(--primary-low);+ }++ &__filter-remove {+ padding: var(--space-1) var(--space-2);+ }++ &__partial-warning {+ margin: 0;+ }++ &__empty {+ border-radius: var(--d-border-radius);+ padding: var(--space-3) var(--space-4);+ }++ &__skeleton {+ .db-skeleton__section-wrapper {+ overflow: hidden;+ padding-bottom: 0;+ }++ .db-skeleton__heading-line {+ width: min(24rem, 100%);+ height: 2rem;+ }++ .db-skeleton__chart {+ height: 360px;+ min-height: 260px;+ }++ @include viewport.until(md) {+ .db-skeleton__subheader {+ flex-direction: column;+ gap: var(--space-4);+ }++ .db-skeleton__metric-row {+ display: grid;+ grid-template-columns: repeat(2, minmax(0, 1fr));+ }++ .db-skeleton__row {+ flex-direction: column;+ }++ .db-skeleton__row-block:not(:last-child) {+ border-right: 0;+ border-bottom: 1px solid var(--primary-low);+ }+ }++ @include viewport.until(sm) {+ .db-skeleton__metric-row {+ grid-template-columns: minmax(0, 1fr);+ }++ .db-skeleton__chart {+ height: 18rem;+ }+ }+ }++ &__results {+ min-width: 0;+ }++ &__report {+ overflow: hidden;+ background: var(--secondary);+ }++ &__metrics {+ .db-section__subintro {+ flex: 1 1 15rem;+ }++ .db-section__metrics {+ align-items: flex-start;+ }++ @include viewport.until(md) {+ flex-direction: column;+ gap: var(--space-4);++ .db-section__subintro {+ flex-basis: auto;+ }++ .db-section__metrics {+ display: grid;+ grid-template-columns: repeat(2, minmax(0, 1fr));+ width: 100%;+ }+ }++ @include viewport.until(sm) {+ .db-section__metrics {+ grid-template-columns: minmax(0, 1fr);+ }+ }+ }++ &__metric {+ display: flex;+ flex-direction: column;+ gap: var(--space-1);+ min-width: 0;++ &.--primary {+ flex-flow: row wrap;+ align-items: baseline;++ .site-traffic-explorer__metric-value,+ .site-traffic-explorer__metric-label {+ color: var(--primary);+ font-size: var(--font-up-1);+ font-weight: 700;+ }+ }+ }++ &__metric-value {+ overflow-wrap: anywhere;+ font-variant-numeric: tabular-nums;+ }++ &__metric-label-row {+ display: flex;+ align-items: center;+ gap: var(--space-1);+ width: max-content;+ max-width: 100%;+ }++ &__metric-label {+ overflow-wrap: anywhere;+ }++ &__empty {+ border: 1px dashed var(--content-border-color);+ background: var(--primary-very-low);+ text-align: center;+ }++ &__cards {+ gap: 0;+ }++ &__card {+ display: flex;+ flex-direction: column;+ gap: var(--space-3);+ min-width: 0;+ }++ &__tabs {+ display: flex;+ flex-wrap: wrap;+ gap: 0 var(--space-4);+ min-width: 0;+ border-bottom: 1px solid var(--content-border-color);++ button {+ appearance: none;+ border: 0;+ border-bottom: 2px solid transparent;+ padding-block: var(--space-2);+ padding-inline: 0;+ background: transparent;+ color: var(--primary-high);+ font: inherit;+ font-size: var(--font-down-1-rem);+ white-space: nowrap;+ cursor: pointer;++ &.is-active {+ border-bottom-color: var(--tertiary);+ color: var(--primary);+ font-weight: 600;+ }++ &:focus-visible {+ outline: 2px solid var(--tertiary);+ outline-offset: -2px;+ }+ }+ }++ &__breakdown-list {+ @include unstyled-list;+ display: flex;+ flex-direction: column;+ min-width: 0;+ font-size: var(--font-down-1-rem);+ }++ &__row {+ box-sizing: border-box;+ display: flex;+ align-items: center;+ justify-content: space-between;+ width: calc(100% + 2 * var(--space-2));+ min-width: 0;+ gap: var(--space-3);+ margin-inline: calc(-1 * var(--space-2));+ border: 0;+ border-radius: var(--d-border-radius);+ padding-block: var(--space-1);+ padding-inline: var(--space-2);+ background: transparent;+ color: var(--primary);+ font: inherit;+ text-align: start;++ > span:not(.site-traffic-explorer__row-count) {+ @include ellipsis;+ }+ }++ &__row-count {+ flex-shrink: 0;+ margin-inline-start: auto;+ color: var(--primary-medium);+ font-variant-numeric: tabular-nums;+ }++ &__row-link {+ @include ellipsis;+ min-width: 0;+ color: inherit;+ text-decoration: none;++ &:focus-visible {+ color: var(--tertiary);+ text-decoration: underline;+ }+ }++ html.discourse-no-touch &__row-link:hover {+ color: var(--tertiary);+ text-decoration: underline;+ }++ &__row-filter-area {+ display: flex;+ align-items: center;+ align-self: stretch;+ justify-content: flex-end;+ flex: 1 1 auto;+ min-width: 0;+ margin-block: calc(-1 * var(--space-1));+ margin-inline-end: calc(-1 * var(--space-2));+ border: 0;+ padding-block: var(--space-1);+ padding-inline: 0 var(--space-2);+ background: transparent;+ color: inherit;+ font: inherit;+ cursor: pointer;+ }++ button.site-traffic-explorer__row {+ cursor: pointer;++ &:focus-visible {+ background: var(--token-color-surface-hovered);+ color: var(--tertiary);+ }+ }++ html.discourse-no-touch & .site-traffic-explorer__row:hover {+ background: var(--token-color-surface-hovered);+ }++ &__view-more {+ width: max-content;+ max-width: 100%;+ margin-block-start: auto;+ margin-inline: calc(-1 * var(--space-2));+ padding-inline: var(--space-2);+ font-size: var(--font-down-1-rem);+ }++ &__dimension-label {+ display: inline-flex;+ align-items: center;+ gap: var(--space-2);+ min-width: 0;+ }++ &__card-empty {+ padding-block: var(--space-3);+ color: var(--primary-medium);+ }++ @include viewport.until(md) {+ &__cards {+ flex-direction: column;+ }++ &__card:not(:last-child) {+ border-right: 0;+ border-bottom: 1px solid var(--content-border-color);+ }+ }++ @include viewport.until(sm) {+ &__chart {+ height: 18rem;+ }+ }+}++.site-traffic-breakdown-modal {+ &__pageviews {+ text-align: end;++ .d-table__mobile-label {+ text-align: start;+ }+ }+}diff --git a/app/controllers/admin/dashboard_controller.rb b/app/controllers/admin/dashboard_controller.rbindex 2bbea264ae0..042c6f24b4a 100644--- a/app/controllers/admin/dashboard_controller.rb+++ b/app/controllers/admin/dashboard_controller.rb@@ -6,10 +6,12 @@ class Admin::DashboardController < Admin::StaffControllerbefore_action :ensure_admin,only: %i[available_reports+ trafficupdate_reports_sectionupdate_configurationupdate_section_settings]+ before_action :ensure_dashboard_improvements_enabled, only: :trafficdef indexif dashboard_improvements?@@ -58,6 +60,30 @@ class Admin::DashboardController < Admin::StaffControllerrender json: AdminDashboardGeneralData.fetch_cached_statsend+ def traffic+ permitted =+ params.permit(+ :start_date,+ :end_date,+ :traffic_type,+ :top_url,+ :entry_url,+ :referrer,+ :country,+ :network,+ :browser,+ :ip,+ )++ AdminDashboardSiteTrafficExplorer.call(service_params.deep_merge(params: permitted.to_h)) do+ on_success { |traffic:| render json: traffic }+ on_failed_contract { raise Discourse::InvalidParameters }+ on_failed_step(:load_traffic) do |step|+ render json: { error_type: step.error }, status: :service_unavailable+ end+ end+ end+def problemsProblemCheck.realtime.run_alldiff --git a/app/jobs/scheduled/maintain_browser_pageview_rollups.rb b/app/jobs/scheduled/maintain_browser_pageview_rollups.rbindex 6b36f7d5da5..d054875c959 100644--- a/app/jobs/scheduled/maintain_browser_pageview_rollups.rb+++ b/app/jobs/scheduled/maintain_browser_pageview_rollups.rb@@ -13,6 +13,7 @@ module Jobsaggregate_engagementaggregate_crawlersbackfill_referrers+ backfill_urlsendprivate@@ -93,7 +94,7 @@ module Jobsenddef next_batch- params = { version: BrowserPageviewReferrerInspector::VERSION, limit: batch_size }+ params = { version: BrowserPageviewEventUrlNormalizer::REFERRER_VERSION, limit: batch_size }retention_clause = ""if SiteSetting.clean_up_browser_pageview_events@@ -120,7 +121,8 @@ module Jobsdef store_normalized_referrers(rows)ids = rows.map(&:id)- normalized = rows.map { |row| BrowserPageviewReferrerInspector.normalize(row.referrer) }+ normalized =+ rows.map { |row| BrowserPageviewEventUrlNormalizer.normalize_referrer(row.referrer) }DB.exec(<<~SQL, ids: ids, normalized: normalized)UPDATE browser_pageview_events AS e@@ -135,7 +137,7 @@ module Jobsenddef recomputable_dates(ids)- params = { ids: ids, version: BrowserPageviewReferrerInspector::VERSION }+ params = { ids: ids, version: BrowserPageviewEventUrlNormalizer::REFERRER_VERSION }retention_clause = ""if SiteSetting.clean_up_browser_pageview_events@@ -176,13 +178,58 @@ module Jobsenddef stamp_version(ids)- DB.exec(<<~SQL, version: BrowserPageviewReferrerInspector::VERSION, ids: ids)+ DB.exec(<<~SQL, version: BrowserPageviewEventUrlNormalizer::REFERRER_VERSION, ids: ids)UPDATE browser_pageview_eventsSET normalized_referrer_version = :versionWHERE id IN (:ids)SQLend+ def backfill_urls+ rows = url_batch+ return if rows.empty?++ ids = rows.map(&:id)+ normalized = rows.map { |row| BrowserPageviewEventUrlNormalizer.normalize_site_path(row.url) }++ DB.exec(+ <<~SQL,+ UPDATE browser_pageview_events AS e+ SET+ normalized_url = data.normalized_url,+ normalized_url_version = :version+ FROM unnest(+ ARRAY[:ids]::bigint[],+ ARRAY[:normalized]::text[]+ ) AS data(id, normalized_url)+ WHERE e.id = data.id+ SQL+ ids: ids,+ normalized: normalized,+ version: BrowserPageviewEventUrlNormalizer::SITE_PATH_VERSION,+ )+ end++ def url_batch+ DB.query(+ <<~SQL,+ SELECT id, url+ FROM browser_pageview_events+ WHERE created_at >= :retention_cutoff+ AND #{BrowserPageviewEvent.rollup_source_condition}+ AND (+ normalized_url_version IS NULL+ OR normalized_url_version < :version+ )+ ORDER BY id+ LIMIT :limit+ SQL+ retention_cutoff: BrowserPageviewEvent.retention_cutoff,+ version: BrowserPageviewEventUrlNormalizer::SITE_PATH_VERSION,+ limit: batch_size,+ )+ end+def batch_sizeSiteSetting.browser_pageview_referrer_backfill_batch_sizeenddiff --git a/app/models/browser_pageview_event.rb b/app/models/browser_pageview_event.rbindex 7b813bfaf28..d62153ac9c9 100644--- a/app/models/browser_pageview_event.rb+++ b/app/models/browser_pageview_event.rb@@ -6,6 +6,7 @@ class BrowserPageviewEvent < ActiveRecord::BaseMAX_REFERRER_LENGTH = 2000MAX_USER_AGENT_LENGTH = 1000MAX_NORMALIZED_REFERRER_LENGTH = 2000+ MAX_NORMALIZED_URL_LENGTH = 2000RETENTION_PERIOD = 3.monthsSOURCE_PIGGYBACK = 1SOURCE_BEACON = 2@@ -168,16 +169,19 @@ class BrowserPageviewEvent < ActiveRecord::Baseenddef attributes_from_payload(payload)- normalized_referrer = BrowserPageviewReferrerInspector.normalize(payload[:referrer])+ normalized_referrer = BrowserPageviewEventUrlNormalizer.normalize_referrer(payload[:referrer])+ normalized_url = BrowserPageviewEventUrlNormalizer.normalize_site_path(payload[:url]){url: payload[:url]&.slice(0, MAX_URL_LENGTH),+ normalized_url: normalized_url&.slice(0, MAX_NORMALIZED_URL_LENGTH),+ normalized_url_version: BrowserPageviewEventUrlNormalizer::SITE_PATH_VERSION,ip_address: payload[:ip_address],country_code: payload[:country_code]&.slice(0, 2),asn: payload[:asn],referrer: payload[:referrer]&.slice(0, MAX_REFERRER_LENGTH),normalized_referrer: normalized_referrer&.slice(0, MAX_NORMALIZED_REFERRER_LENGTH),- normalized_referrer_version: BrowserPageviewReferrerInspector::VERSION,+ normalized_referrer_version: BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,user_agent: payload[:user_agent]&.slice(0, MAX_USER_AGENT_LENGTH),session_id: payload[:session_id]&.slice(0, MAX_SESSION_ID_LENGTH),user_id: payload[:user_id],@@ -239,6 +243,9 @@ class BrowserPageviewEvent < ActiveRecord::Baseif normalized_referrer.present?self.normalized_referrer = normalized_referrer.slice(0, MAX_NORMALIZED_REFERRER_LENGTH)end+ if normalized_url.present?+ self.normalized_url = normalized_url.slice(0, MAX_NORMALIZED_URL_LENGTH)+ endendend@@ -252,6 +259,8 @@ end# ip_address :inet not null# normalized_referrer :string(2000)# normalized_referrer_version :integer+# normalized_url :string(2000)+# normalized_url_version :integer# referrer :string(2000)# score :integer# source :integer default("piggyback"), not null@@ -268,6 +277,7 @@ end# idx_bpe_created_at_normalized_referrer (created_at,normalized_referrer)# idx_bpe_ip_ua_created_at (ip_address,user_agent,created_at)# idx_bpe_normalized_referrer_version (normalized_referrer_version) WHERE (referrer IS NOT NULL)+# idx_bpe_normalized_url_version (normalized_url_version)# idx_bpe_session_created_at (session_id,created_at)# index_browser_pageview_events_on_created_at (created_at) USING brin# index_browser_pageview_events_on_topic_id (topic_id)diff --git a/app/models/browser_pageview_session_engagement_daily_rollup.rb b/app/models/browser_pageview_session_engagement_daily_rollup.rbindex 2a4b33f4ecc..78ab6274721 100644--- a/app/models/browser_pageview_session_engagement_daily_rollup.rb+++ b/app/models/browser_pageview_session_engagement_daily_rollup.rb@@ -4,6 +4,10 @@ class BrowserPageviewSessionEngagementDailyRollup < ActiveRecord::BaseBOUNCE_ENGAGED_SECONDS_THRESHOLD = 10private_constant :BOUNCE_ENGAGED_SECONDS_THRESHOLD+ def self.bounce_engaged_seconds_threshold+ BOUNCE_ENGAGED_SECONDS_THRESHOLD+ end+def self.aggregate(start_date:, end_date:)start_date = start_date.to_dateend_date = end_date.to_date + 1diff --git a/app/models/concerns/reports/top_referrers_by_browser_pageviews.rb b/app/models/concerns/reports/top_referrers_by_browser_pageviews.rbindex 25ae84b599d..18c4fba4c80 100644--- a/app/models/concerns/reports/top_referrers_by_browser_pageviews.rb+++ b/app/models/concerns/reports/top_referrers_by_browser_pageviews.rb@@ -25,7 +25,7 @@ module Reports::TopReferrersByBrowserPageviewscount_expr = SiteSetting.login_required ? "logged_in_count" : "count"end_date_exclusive = report.end_date.to_date + 1- host = BrowserPageviewReferrerInspector.normalize_host(Discourse.current_hostname)+ host = BrowserPageviewEventUrlNormalizer.normalize_host(Discourse.current_hostname)escaped_host = host.gsub(/[\\_%]/) { |char| "\\#{char}" }sql = <<~SQLdiff --git a/app/services/admin_dashboard_site_traffic_explorer.rb b/app/services/admin_dashboard_site_traffic_explorer.rbnew file mode 100644index 00000000000..ac3d0827f19--- /dev/null+++ b/app/services/admin_dashboard_site_traffic_explorer.rb@@ -0,0 +1,657 @@+# frozen_string_literal: true++class AdminDashboardSiteTrafficExplorer+ include Service::Base++ DIMENSION_LIMIT = 50+ private_constant :DIMENSION_LIMIT++ STATEMENT_TIMEOUT_MS = 10_000+ private_constant :STATEMENT_TIMEOUT_MS++ FILTER_KEYS = %i[traffic_type top_url entry_url referrer country network browser ip].freeze+ private_constant :FILTER_KEYS++ FILTER_DIMENSIONS = {+ top_url: "top_urls",+ entry_url: "entry_urls",+ referrer: "referrers",+ country: "countries",+ network: "networks",+ browser: "browsers",+ ip: "ip_addresses",+ }.freeze+ private_constant :FILTER_DIMENSIONS++ BROWSER_VALUES = %w[edge firefox chrome safari unknown].freeze+ private_constant :BROWSER_VALUES++ TRAFFIC_TYPE_VALUES = %w[logged_in anonymous likely_crawler].freeze+ private_constant :TRAFFIC_TYPE_VALUES++ params do+ attribute :start_date, :date+ attribute :end_date, :date+ attribute :traffic_type, :string+ attribute :top_url, :string+ attribute :entry_url, :string+ attribute :referrer, :string+ attribute :country, :string+ attribute :network, :string+ attribute :browser, :string+ attribute :ip, :string++ validates :start_date, :end_date, presence: true+ validate :start_date_precedes_end_date++ def filters+ FILTER_KEYS.to_h { |key| [key, normalize_filter(key, public_send(key))] }+ end++ private++ def start_date_precedes_end_date+ return if start_date.blank? || end_date.blank? || start_date <= end_date++ errors.add(:start_date, :invalid)+ end++ def normalize_filter(key, value)+ return nil if value.nil?+ return "" if key == :referrer && value == ""+ raise Discourse::InvalidParameters.new(key) if value.blank?++ case key+ when :traffic_type+ traffic_types = value.to_s.split(",").uniq+ if traffic_types.empty? || (traffic_types - TRAFFIC_TYPE_VALUES).any?+ raise Discourse::InvalidParameters.new(key)+ end+ TRAFFIC_TYPE_VALUES.select { |traffic_type| traffic_types.include?(traffic_type) }+ when :top_url, :entry_url+ BrowserPageviewEventUrlNormalizer.normalize_site_path(value) ||+ raise(Discourse::InvalidParameters.new(key))+ when :country+ country = value.to_s.upcase+ raise Discourse::InvalidParameters.new(key) if !country.match?(/\A[A-Z]{2}\z/)+ country+ when :network+ match = value.to_s.match(/\AAS(\d+)\z/)+ raise Discourse::InvalidParameters.new(key) if !match+ match[1].to_i+ when :browser+ browser = value.to_s+ raise Discourse::InvalidParameters.new(key) if !BROWSER_VALUES.include?(browser)+ browser+ when :ip+ IPAddr.new(value.to_s).to_s+ when :referrer+ BrowserPageviewEventUrlNormalizer.normalize_referrer("https://#{value}").presence ||+ raise(Discourse::InvalidParameters.new(key))+ end+ rescue IPAddr::Error+ raise Discourse::InvalidParameters.new(key)+ end+ end++ step :load_traffic++ private++ def load_traffic(params:)+ filters = params.filters+ row = execute_query(start_date: params.start_date, end_date: params.end_date, filters:)++ traffic = {+ partial_data: partial_data(row.fetch("pageview_limited"), start_date: params.start_date),+ summary: row.fetch("summary"),+ series: row.fetch("series"),+ series_colors: series_colors,+ dimensions: decorate_dimensions(row.fetch("dimensions")),+ }+ active_filters =+ decorate_active_filters(row.fetch("active_filter_representative_ips"), filters:)+ traffic[:active_filters] = active_filters if active_filters.any?+ context[:traffic] = traffic+ rescue ActiveRecord::QueryCanceled, PG::QueryCanceled+ fail!("traffic_query_timeout")+ end++ def execute_query(start_date:, end_date:, filters:)+ ActiveRecord::Base.transaction(requires_new: true) do+ DB.exec("SET TRANSACTION READ ONLY")+ DB.exec("SET LOCAL statement_timeout = #{STATEMENT_TIMEOUT_MS}")+ DB.query_hash(query, query_params(start_date:, end_date:, filters:)).first+ end+ end++ def query_params(start_date:, end_date:, filters:)+ retention_cutoff = BrowserPageviewEvent.retention_cutoff.to_date+ effective_start_date = [start_date, retention_cutoff].max+ cap = SiteSetting.admin_site_traffic_event_cap++ {+ start_date: effective_start_date,+ retention_cutoff: retention_cutoff,+ end_date: end_date + 1.day,+ cap: cap,+ cap_plus_one: cap + 1,+ top_url: filters[:top_url],+ entry_url: filters[:entry_url],+ referrer: filters[:referrer],+ country: filters[:country],+ network_asn: filters[:network],+ browser: filters[:browser],+ ip_address: filters[:ip],+ traffic_type_filtered: filters[:traffic_type].present?,+ include_logged_in: filters[:traffic_type]&.include?("logged_in"),+ include_anonymous: filters[:traffic_type]&.include?("anonymous"),+ include_likely_crawler: filters[:traffic_type]&.include?("likely_crawler"),+ crawler_detection_enabled: UpcomingChanges.enabled?(:improved_crawler_detection),+ crawler_threshold: CrawlerScorer::BOT_SCORE_THRESHOLD,+ bounce_threshold:+ BrowserPageviewSessionEngagementDailyRollup.bounce_engaged_seconds_threshold,+ dimension_limit: DIMENSION_LIMIT,+ }+ end++ def query+ <<~SQL+ WITH eligible AS MATERIALIZED (+ SELECT+ candidates.id,+ candidates.created_at,+ candidates.session_id,+ candidates.user_id,+ candidates.normalized_url,+ candidates.country_code,+ candidates.asn,+ candidates.ip_address,+ candidates.user_agent,+ candidates.score,+ ROW_NUMBER() OVER (+ ORDER BY candidates.created_at DESC, candidates.id DESC+ ) AS cap_position+ FROM (+ SELECT+ bpe.id,+ bpe.created_at,+ bpe.session_id,+ bpe.user_id,+ bpe.normalized_url,+ bpe.country_code,+ bpe.asn,+ bpe.ip_address,+ bpe.user_agent,+ bpe.score+ FROM browser_pageview_events bpe+ WHERE bpe.created_at >= :start_date+ AND bpe.created_at < :end_date+ AND #{BrowserPageviewEvent.rollup_source_condition(table: "bpe")}+ ORDER BY bpe.created_at DESC, bpe.id DESC+ LIMIT :cap_plus_one+ ) candidates+ ),+ population AS NOT MATERIALIZED (+ SELECT+ id,+ created_at,+ session_id,+ user_id,+ normalized_url,+ country_code,+ asn,+ ip_address,+ user_agent,+ score+ FROM eligible+ WHERE cap_position <= :cap+ ),+ population_sessions AS (+ SELECT DISTINCT session_id+ FROM population+ ),+ session_entries AS (+ SELECT entry.*+ FROM population_sessions+ CROSS JOIN LATERAL (+ SELECT+ bpe.id,+ bpe.session_id,+ bpe.normalized_url,+ bpe.normalized_referrer+ FROM browser_pageview_events bpe+ WHERE bpe.session_id = population_sessions.session_id+ AND bpe.created_at >= :retention_cutoff+ AND #{BrowserPageviewEvent.rollup_source_condition(table: "bpe")}+ ORDER BY bpe.created_at, bpe.id+ LIMIT 1+ ) entry+ ),+ browser_values AS MATERIALIZED (+ SELECT+ user_agent,+ CASE+ WHEN user_agent ~* 'Edg' THEN 'edge'+ WHEN user_agent ~* '(Opera|OPR)' THEN 'unknown'+ WHEN user_agent ~* 'Firefox' THEN 'firefox'+ WHEN user_agent ~* '(Chrome|CriOS)' THEN 'chrome'+ WHEN user_agent ~* 'Safari' THEN 'safari'+ ELSE 'unknown'+ END AS browser+ FROM population+ GROUP BY user_agent+ ),+ classified AS (+ SELECT+ population.id,+ population.created_at,+ population.session_id,+ population.user_id,+ population.normalized_url,+ population.country_code,+ population.asn,+ population.ip_address,+ browser_values.browser,+ (+ :crawler_detection_enabled+ AND COALESCE(population.score, 0) > :crawler_threshold+ ) AS likely_crawler+ FROM population+ JOIN browser_values+ ON browser_values.user_agent = population.user_agent+ ),+ dimensioned AS (+ SELECT+ classified.*,+ CASE+ WHEN classified.id = session_entries.id THEN session_entries.normalized_url+ END AS entry_url,+ CASE+ WHEN session_entries.id IS NULL OR classified.id <> session_entries.id THEN NULL+ ELSE COALESCE(session_entries.normalized_referrer, '')+ END AS referrer+ FROM classified+ LEFT JOIN session_entries+ ON session_entries.session_id = classified.session_id+ ),+ filtered AS MATERIALIZED (+ SELECT *+ FROM dimensioned+ WHERE (:top_url IS NULL OR normalized_url = :top_url)+ AND (+ :entry_url IS NULL+ OR entry_url = :entry_url+ )+ AND (+ :referrer IS NULL+ OR referrer = :referrer+ )+ AND (:country IS NULL OR country_code = :country)+ AND (:network_asn IS NULL OR asn = :network_asn)+ AND (:browser IS NULL OR browser = :browser)+ AND (:ip_address IS NULL OR host(ip_address) = :ip_address)+ AND (+ NOT :traffic_type_filtered+ OR (+ :include_logged_in+ AND NOT likely_crawler+ AND user_id IS NOT NULL+ )+ OR (+ :include_anonymous+ AND NOT likely_crawler+ AND user_id IS NULL+ )+ OR (+ :include_likely_crawler+ AND likely_crawler+ )+ )+ ),+ sessions AS (+ SELECT session_id, COUNT(*) AS pageviews+ FROM filtered+ GROUP BY session_id+ ),+ session_summary AS (+ SELECT+ COUNT(*)::integer AS distinct_sessions,+ COUNT(*) FILTER (+ WHERE sessions.pageviews = 1+ AND COALESCE(engagement.engaged_seconds, 0) < :bounce_threshold+ )::integer AS bounced_sessions,+ COALESCE(SUM(engagement.engaged_seconds), 0)::bigint AS engaged_seconds+ FROM sessions+ LEFT JOIN browser_pageview_session_engagements engagement+ ON engagement.session_id = sessions.session_id+ ),+ traffic_summary AS (+ SELECT+ COUNT(*) FILTER (+ WHERE :traffic_type_filtered OR NOT likely_crawler+ )::integer AS pageviews,+ COUNT(*) FILTER (+ WHERE NOT likely_crawler AND user_id IS NOT NULL+ )::integer AS logged_in_pageviews+ FROM filtered+ ),+ series_rows AS (+ SELECT+ created_at::date AS date,+ COUNT(*) FILTER (+ WHERE :traffic_type_filtered OR NOT likely_crawler+ )::integer AS pageviews,+ COUNT(*) FILTER (+ WHERE NOT likely_crawler AND user_id IS NOT NULL+ )::integer AS logged_in_human_pageviews,+ COUNT(*) FILTER (+ WHERE NOT likely_crawler AND user_id IS NULL+ )::integer AS anonymous_human_pageviews,+ COUNT(*) FILTER (WHERE likely_crawler)::integer AS likely_crawler_pageviews+ FROM filtered+ GROUP BY created_at::date+ )+ SELECT+ (SELECT EXISTS (SELECT 1 FROM eligible WHERE cap_position > :cap)) AS pageview_limited,+ jsonb_build_object(+ 'country', (+ SELECT MIN(host(ip_address))+ FROM population+ WHERE :country IS NOT NULL+ AND country_code = :country+ ),+ 'network', (+ SELECT MIN(host(ip_address))+ FROM population+ WHERE :network_asn IS NOT NULL+ AND asn = :network_asn+ )+ ) AS active_filter_representative_ips,+ jsonb_build_object(+ 'pageviews', traffic_summary.pageviews,+ 'distinct_sessions', session_summary.distinct_sessions,+ 'logged_in_share', CASE+ WHEN traffic_summary.pageviews = 0 THEN 0+ ELSE ROUND(+ traffic_summary.logged_in_pageviews::numeric * 100 /+ traffic_summary.pageviews+ )::integer+ END,+ 'bounce_rate', CASE+ WHEN session_summary.distinct_sessions = 0 THEN 0+ ELSE ROUND(+ session_summary.bounced_sessions::numeric * 100 /+ session_summary.distinct_sessions+ )::integer+ END,+ 'average_session_duration_seconds', CASE+ WHEN session_summary.distinct_sessions = 0 THEN 0+ ELSE ROUND(+ session_summary.engaged_seconds::numeric /+ session_summary.distinct_sessions+ )::integer+ END+ ) AS summary,+ COALESCE(+ (+ SELECT jsonb_agg(+ jsonb_build_object(+ 'date', date,+ 'pageviews', pageviews,+ 'logged_in_human_pageviews', logged_in_human_pageviews,+ 'anonymous_human_pageviews', anonymous_human_pageviews,+ 'likely_crawler_pageviews', likely_crawler_pageviews+ )+ ORDER BY date+ )+ FROM series_rows+ ),+ '[]'::jsonb+ ) AS series,+ jsonb_build_object(+ 'top_urls', (+ SELECT COALESCE(+ jsonb_agg(+ jsonb_build_object('value', value, 'pageviews', pageviews)+ ORDER BY pageviews DESC, value+ ),+ '[]'::jsonb+ )+ FROM (+ SELECT normalized_url AS value, COUNT(*)::integer AS pageviews+ FROM filtered+ WHERE normalized_url IS NOT NULL+ GROUP BY normalized_url+ ORDER BY pageviews DESC, value+ LIMIT :dimension_limit+ ) rows+ ),+ 'entry_urls', (+ SELECT COALESCE(+ jsonb_agg(+ jsonb_build_object('value', value, 'pageviews', pageviews)+ ORDER BY pageviews DESC, value+ ),+ '[]'::jsonb+ )+ FROM (+ SELECT entry_url AS value, COUNT(*)::integer AS pageviews+ FROM filtered+ WHERE entry_url IS NOT NULL+ GROUP BY entry_url+ ORDER BY pageviews DESC, value+ LIMIT :dimension_limit+ ) rows+ ),+ 'referrers', (+ SELECT COALESCE(+ jsonb_agg(+ jsonb_build_object('value', value, 'pageviews', pageviews)+ ORDER BY pageviews DESC, value+ ),+ '[]'::jsonb+ )+ FROM (+ SELECT referrer AS value, COUNT(*)::integer AS pageviews+ FROM filtered+ WHERE referrer IS NOT NULL+ GROUP BY referrer+ ORDER BY pageviews DESC, value+ LIMIT :dimension_limit+ ) rows+ ),+ 'countries', (+ SELECT COALESCE(+ jsonb_agg(+ jsonb_build_object(+ 'value', value,+ 'pageviews', pageviews,+ 'representative_ip', representative_ip+ )+ ORDER BY pageviews DESC, value+ ),+ '[]'::jsonb+ )+ FROM (+ SELECT+ country_code AS value,+ COUNT(*)::integer AS pageviews,+ MIN(host(ip_address)) AS representative_ip+ FROM filtered+ WHERE country_code IS NOT NULL+ GROUP BY country_code+ ORDER BY pageviews DESC, value+ LIMIT :dimension_limit+ ) rows+ ),+ 'networks', (+ SELECT COALESCE(+ jsonb_agg(+ jsonb_build_object(+ 'value', value,+ 'pageviews', pageviews,+ 'representative_ip', representative_ip+ )+ ORDER BY pageviews DESC, value+ ),+ '[]'::jsonb+ )+ FROM (+ SELECT+ 'AS' || asn AS value,+ COUNT(*)::integer AS pageviews,+ MIN(host(ip_address)) AS representative_ip+ FROM filtered+ WHERE asn IS NOT NULL+ GROUP BY asn+ ORDER BY pageviews DESC, value+ LIMIT :dimension_limit+ ) rows+ ),+ 'browsers', (+ SELECT COALESCE(+ jsonb_agg(+ jsonb_build_object('value', value, 'pageviews', pageviews)+ ORDER BY pageviews DESC, value+ ),+ '[]'::jsonb+ )+ FROM (+ SELECT browser AS value, COUNT(*)::integer AS pageviews+ FROM filtered+ GROUP BY browser+ ORDER BY pageviews DESC, value+ LIMIT :dimension_limit+ ) rows+ ),+ 'ip_addresses', (+ SELECT COALESCE(+ jsonb_agg(+ jsonb_build_object('value', value, 'pageviews', pageviews)+ ORDER BY pageviews DESC, value+ ),+ '[]'::jsonb+ )+ FROM (+ SELECT host(ip_address) AS value, COUNT(*)::integer AS pageviews+ FROM filtered+ GROUP BY ip_address+ ORDER BY pageviews DESC, value+ LIMIT :dimension_limit+ ) rows+ )+ ) AS dimensions+ FROM traffic_summary+ CROSS JOIN session_summary+ SQL+ end++ def partial_data(pageview_limited, start_date:)+ retention_limited = start_date < BrowserPageviewEvent.retention_cutoff.to_date+ return nil if !retention_limited && !pageview_limited++ if retention_limited && pageview_limited+ {+ reason: "retention_and_pageview_limit",+ available_start_date: BrowserPageviewEvent.retention_cutoff.to_date.iso8601,+ pageview_limit: SiteSetting.admin_site_traffic_event_cap,+ }+ elsif retention_limited+ {+ reason: "retention",+ available_start_date: BrowserPageviewEvent.retention_cutoff.to_date.iso8601,+ }+ else+ { reason: "pageview_limit", pageview_limit: SiteSetting.admin_site_traffic_event_cap }+ end+ end++ def decorate_dimensions(dimensions)+ dimensions.to_h do |dimension, rows|+ [dimension, rows.map { |row| decorate_dimension_row(dimension, row) }]+ end+ end++ def decorate_active_filters(representative_ips, filters:)+ filters.flat_map do |key, value|+ next [] if value.nil?++ if key == :traffic_type+ next(+ value.map do |traffic_type|+ {+ key: key,+ value: traffic_type,+ label: I18n.t("admin_site_traffic_explorer.traffic_types.#{traffic_type}"),+ }+ end+ )+ end++ canonical_value = key == :network ? "AS#{value}" : value+ dimension = FILTER_DIMENSIONS.fetch(key)+ [+ {+ key: key,+ value: canonical_value,+ label: dimension_label(dimension, canonical_value, representative_ips[key.to_s]),+ },+ ]+ end+ end++ def series_colors+ {+ "logged_in_human_pageviews" =>+ Reports::SiteTraffic::SERIES_COLORS.fetch("page_view_logged_in_browser"),+ "anonymous_human_pageviews" =>+ Reports::SiteTraffic::SERIES_COLORS.fetch("page_view_anon_browser"),+ "likely_crawler_pageviews" =>+ Reports::SiteTraffic::SERIES_COLORS.fetch("page_view_likely_crawler"),+ }+ end++ def decorate_dimension_row(dimension, row)+ value = row.fetch("value")+ {+ value: value,+ label: dimension_label(dimension, value, row["representative_ip"]),+ pageviews: row.fetch("pageviews"),+ }+ end++ def dimension_label(dimension, value, representative_ip)+ case dimension+ when "referrers"+ value.presence || I18n.t("admin_site_traffic_explorer.direct_or_unknown")+ when "countries"+ country_label(value, representative_ip)+ when "networks"+ network_label(value, representative_ip)+ when "browsers"+ I18n.t("admin_site_traffic_explorer.browsers.#{value}", default: value)+ else+ value+ end+ end++ def country_label(value, representative_ip)+ return value if representative_ip.blank?++ info = DiscourseIpInfo.get(representative_ip, locale: I18n.locale, resolve_hostname: false)+ return info[:country] if info[:country_code].to_s.casecmp?(value) && info[:country].present?++ value+ end++ def network_label(value, representative_ip)+ return value if representative_ip.blank?++ info = DiscourseIpInfo.get(representative_ip, locale: I18n.locale, resolve_hostname: false)+ asn = value.delete_prefix("AS").to_i+ return value if info[:asn].to_i != asn || info[:organization].blank?++ "#{info[:organization]} (#{value})"+ end+enddiff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rbindex 44f7fffd745..be58bda53a5 100644--- a/config/initializers/filter_parameter_logging.rb+++ b/config/initializers/filter_parameter_logging.rb@@ -12,4 +12,8 @@ Rails.application.config.filter_parameters += %i[facebook_app_secretgithub_client_secretsecond_factor_token+ top_url+ entry_url+ referrer+ ip]diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 4e8e332d6ba..9b0399bda4b 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -7248,6 +7248,75 @@ en:new_features:title: "What's new?"check_for_updates: "Check for updates"+ site_traffic_explorer:+ title: "Site Traffic Explorer"+ loading: "Loading traffic…"+ errors:+ timeout: "This traffic query took too long. Choose a shorter date range and try again."+ unexpected: "Couldn't load site traffic. Try again."+ empty: "No matching pageviews"+ summary: "Traffic summary"+ traffic_over_time: "Traffic over time"+ pageviews: "Pageviews"+ metric_information: "About this metric"+ active_filters: "Active traffic filters"+ filter_description: "%{filter} is <strong>%{value}</strong>"+ filter_by:+ one: "Filter by %{label}, %{count} pageview"+ other: "Filter by %{label}, %{count} pageviews"+ remove_filter: "Remove %{filter} filter"+ remove_traffic_type_filter: "Remove %{value} traffic filter"+ view_more: "View more"+ no_dimension_data: "No data for this breakdown."+ direct_or_unknown: "Direct / unknown"+ cards:+ acquisition: "Acquisition"+ pages: "Pages"+ visitors: "Visitors"+ dimensions:+ referrers: "Referrers"+ countries: "Countries"+ networks: "Networks"+ top_urls: "Top URLs"+ entry_urls: "Entry URLs"+ browsers: "Browsers"+ ip_addresses: "IP addresses"+ columns:+ referrers: "Referrer"+ countries: "Country"+ networks: "Network"+ top_urls: "URL"+ entry_urls: "Entry URL"+ browsers: "Browser"+ ip_addresses: "IP address"+ filters:+ traffic_type: "Traffic type"+ top_url: "Top URL"+ entry_url: "Entry URL"+ referrer: "Referrer"+ country: "Country"+ network: "Network"+ browser: "Browser"+ ip: "IP address"+ metrics:+ pageviews:+ label: "Pageviews"+ distinct_sessions:+ label: "Distinct sessions"+ logged_in_share:+ label: "Logged-in share"+ bounce_rate:+ label: "Bounce rate"+ average_session_duration:+ label: "Average session duration"+ series:+ logged_in_human: "Logged in"+ anonymous_human: "Anonymous"+ likely_crawler: "Likely crawlers"+ partial:+ retention: "Detailed pageview activity before %{date} is no longer available, so these results cover only part of the selected date range."+ pageview_limit: "These results are limited to the most recent %{limit} pageviews in the selected date range. Choose a shorter date range to include all available traffic."+ combined: "Detailed pageview activity before %{date} is no longer available, so these results cover only part of the selected date range. They are also limited to the most recent %{limit} pageviews. Choose a shorter date range within the available dates to include all available pageview activity."dashboard:title: "Dashboard"last_updated: "Dashboard updated:"diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex b06725895d9..7d6ca4724e1 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -1104,6 +1104,7 @@ en:opera: "Opera"safari: "Safari"unknown: "unknown browser"+device:android: "Android Device"chromebook: "Chrome OS"@@ -1124,6 +1125,19 @@ en:windows: "Microsoft Windows"unknown: "unknown operating system"+ admin_site_traffic_explorer:+ direct_or_unknown: "Direct / unknown"+ traffic_types:+ logged_in: "Logged in"+ anonymous: "Anonymous"+ likely_crawler: "Likely crawlers"+ browsers:+ chrome: "Chrome"+ edge: "Microsoft Edge"+ firefox: "Firefox"+ safari: "Safari"+ unknown: "Unknown browser"+change_email:error: "There was an error changing your email address. Perhaps the address is already in use?"doesnt_exist: "That email address is not associated with your account."@@ -2895,6 +2909,7 @@ en:dashboard_hidden_reports: "Allow to hide the specified reports from the dashboard."dashboard_visible_tabs: "Choose which dashboard tabs are visible."dashboard_general_tab_activity_metrics: "Choose reports to be displayed as activity metrics on the general tab."+ enable_site_traffic_explorer: "Link the admin dashboard's site traffic details action to the Site Traffic Explorer."gravatar_enabled: "Use the <a href='https://gravatar.com/'>Gravatar</a> service for user avatars. Disabling this will prevent users from switching to Gravatar for their avatars, but will not impact users who already use it."gravatar_name: "Specify the name of the Gravatar service provider. This name is typically used to identify the source providing Gravatar avatars to the site."diff --git a/config/routes.rb b/config/routes.rbindex 3d46f1a3aad..dda88d96eab 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -327,6 +327,8 @@ Discourse::Application.routes.draw doget "version_check" => "versions#show"get "dashboard" => "dashboard#index"+ get "dashboard/site-traffic-explorer" => "dashboard#traffic",+ :constraints => AdminConstraint.newput "dashboard/configuration" => "dashboard#update_configuration",:constraints => AdminConstraint.newput "dashboard/sections/:section_id/settings/:setting_key" =>diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 43e662c24ca..aa678401d4c 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -621,6 +621,12 @@ basic:min: 1max: 20000hidden: true+ admin_site_traffic_event_cap:+ default: 1000000+ type: integer+ min: 1+ max: 1000000+ hidden: truebrowser_pageview_max_engaged_seconds:default: 1800type: integer@@ -4643,6 +4649,10 @@ dashboard:- user_to_user_private_messages_with_replies- signupsarea: "reports"+ enable_site_traffic_explorer:+ default: false+ client: true+ hidden: trueverbose_user_stat_count_logging:hidden: truedefault: falsediff --git a/db/migrate/20260806074204_add_normalized_url_to_browser_pageview_events.rb b/db/migrate/20260806074204_add_normalized_url_to_browser_pageview_events.rbnew file mode 100644index 00000000000..1a965762486--- /dev/null+++ b/db/migrate/20260806074204_add_normalized_url_to_browser_pageview_events.rb@@ -0,0 +1,8 @@+# frozen_string_literal: true++class AddNormalizedUrlToBrowserPageviewEvents < ActiveRecord::Migration[8.0]+ def change+ add_column :browser_pageview_events, :normalized_url, :string, limit: 2000+ add_column :browser_pageview_events, :normalized_url_version, :integer+ end+enddiff --git a/db/migrate/20260806074210_add_normalized_url_version_index_to_browser_pageview_events.rb b/db/migrate/20260806074210_add_normalized_url_version_index_to_browser_pageview_events.rbnew file mode 100644index 00000000000..54ea3150091--- /dev/null+++ b/db/migrate/20260806074210_add_normalized_url_version_index_to_browser_pageview_events.rb@@ -0,0 +1,25 @@+# frozen_string_literal: true++class AddNormalizedUrlVersionIndexToBrowserPageviewEvents < ActiveRecord::Migration[8.0]+ disable_ddl_transaction!++ INDEX_NAME = "idx_bpe_normalized_url_version"++ def up+ remove_index :browser_pageview_events,+ name: INDEX_NAME,+ algorithm: :concurrently,+ if_exists: true+ add_index :browser_pageview_events,+ :normalized_url_version,+ name: INDEX_NAME,+ algorithm: :concurrently+ end++ def down+ remove_index :browser_pageview_events,+ name: INDEX_NAME,+ algorithm: :concurrently,+ if_exists: true+ end+enddiff --git a/db/structure.sql b/db/structure.sqlindex 1db2ece52e4..69d6ac48aa6 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -2068,7 +2068,9 @@ CREATE TABLE public.browser_pageview_events (score integer,normalized_referrer character varying(2000),normalized_referrer_version smallint,- source smallint DEFAULT 1 NOT NULL+ source smallint DEFAULT 1 NOT NULL,+ normalized_url character varying(2000),+ normalized_url_version integer);@@ -17610,6 +17612,13 @@ CREATE INDEX idx_bpe_ip_ua_created_at ON public.browser_pageview_events USING btCREATE INDEX idx_bpe_normalized_referrer_version ON public.browser_pageview_events USING btree (normalized_referrer_version) WHERE (referrer IS NOT NULL);+--+-- Name: idx_bpe_normalized_url_version; Type: INDEX; Schema: public; Owner: -+--++CREATE INDEX idx_bpe_normalized_url_version ON public.browser_pageview_events USING btree (normalized_url_version);++---- Name: idx_bpe_session_created_at; Type: INDEX; Schema: public; Owner: ---@@ -23144,6 +23153,8 @@ SET search_path TO "$user", public;INSERT INTO "schema_migrations" (version) VALUES('20260810154331'),('20260810012238'),+('20260806074210'),+('20260806074204'),('20260803015314'),('20260731055703'),('20260730183114'),diff --git a/frontend/discourse/admin/components/admin-report-stacked-chart.gjs b/frontend/discourse/admin/components/admin-report-stacked-chart.gjsindex 78d424bd82f..66d6bf87dde 100644--- a/frontend/discourse/admin/components/admin-report-stacked-chart.gjs+++ b/frontend/discourse/admin/components/admin-report-stacked-chart.gjs@@ -128,6 +128,11 @@ export default class AdminReportStackedChart extends Component {const ci = legend.chart;const req = chartData[index].req;+ if (chartOptions.onLegendClick) {+ chartOptions.onLegendClick(req);+ return;+ }+if (ci.isDatasetVisible(index)) {ci.hide(index);if (!chartOptions.hiddenLabels.includes(req)) {@@ -210,7 +215,10 @@ export default class AdminReportStackedChart extends Component {display: !chartOptions.hideYAxisGridLines,},ticks: {- callback: (label) => number(label),+ callback: (_label, index, ticks) => {+ const value = ticks[index]?.value;+ return Number.isInteger(Number(value)) ? number(value) : null;+ },sampleSize: 5,maxRotation: 25,minRotation: 0,diff --git a/frontend/discourse/admin/components/dashboard/traffic.gjs b/frontend/discourse/admin/components/dashboard/traffic.gjsindex 0f9c8ffe98f..d0c384e86cb 100644--- a/frontend/discourse/admin/components/dashboard/traffic.gjs+++ b/frontend/discourse/admin/components/dashboard/traffic.gjs@@ -1,6 +1,7 @@import Component from "@glimmer/component";import { concat, hash } from "@ember/helper";import { LinkTo } from "@ember/routing";+import { service } from "@ember/service";import AdminReportStackedChart from "discourse/admin/components/admin-report-stacked-chart";import DashboardSection from "discourse/admin/components/dashboard/section";import { countryFlag, countryName } from "discourse/admin/lib/format-country";@@ -29,6 +30,9 @@ const PERIOD_COPY_KEYS = {};export default class DashboardTraffic extends Component {+ @service currentUser;+ @service siteSettings;+hiddenLabels = ["page_view_crawler"];get browserPageviews() {@@ -155,6 +159,20 @@ export default class DashboardTraffic extends Component {};}+ get explorerQuery() {+ if (this.args.period === "custom") {+ return { range: this.args.period, ...this.reportQuery };+ }++ return { range: this.args.period };+ }++ get showTrafficExplorerLink() {+ return (+ this.currentUser.admin && this.siteSettings.enable_site_traffic_explorer+ );+ }+formatHeadlineCount(value) {if (value >= 1_000_000) {const formatted = I18n.toNumber(value / 1_000_000, { precision: 1 });@@ -385,18 +403,29 @@ export default class DashboardTraffic extends Component {class="db-section__traffic-chart-canvas"/></div>- <LinkTo- class="db-traffic__see-details"- @route="adminReports.show"- @model="site_traffic"- @query={{hash- start_date=this.reportQuery.start_date- end_date=this.reportQuery.end_date- }}- >- {{i18n "admin.dashboard.site_traffic.see_details"}}- {{dIcon "arrow-right"}}- </LinkTo>+ {{#if this.showTrafficExplorerLink}}+ <LinkTo+ class="db-traffic__see-details"+ @route="adminSiteTraffic"+ @query={{this.explorerQuery}}+ >+ {{i18n "admin.dashboard.site_traffic.see_details"}}+ {{dIcon "arrow-right"}}+ </LinkTo>+ {{else}}+ <LinkTo+ class="db-traffic__see-details"+ @route="adminReports.show"+ @model="site_traffic"+ @query={{hash+ start_date=this.reportQuery.start_date+ end_date=this.reportQuery.end_date+ }}+ >+ {{i18n "admin.dashboard.site_traffic.see_details"}}+ {{dIcon "arrow-right"}}+ </LinkTo>+ {{/if}}{{else}}<div class="db-section__traffic-chart"><div class="db-section__traffic-chart-shell"></div>diff --git a/frontend/discourse/admin/components/site-traffic-explorer-breakdown-card.gjs b/frontend/discourse/admin/components/site-traffic-explorer-breakdown-card.gjsnew file mode 100644index 00000000000..f661df6914f--- /dev/null+++ b/frontend/discourse/admin/components/site-traffic-explorer-breakdown-card.gjs@@ -0,0 +1,245 @@+import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";+import { concat, fn } from "@ember/helper";+import { on } from "@ember/modifier";+import { action } from "@ember/object";+import { next, schedule } from "@ember/runloop";+import { service } from "@ember/service";+import SiteTrafficExplorerBreakdownModal from "discourse/admin/components/site-traffic-explorer-breakdown-modal";+import SiteTrafficExplorerDimensionLabel from "discourse/admin/components/site-traffic-explorer-dimension-label";+import SiteTrafficExplorerPageviewCount from "discourse/admin/components/site-traffic-explorer-pageview-count";+import getURL from "discourse/lib/get-url";+import { eq } from "discourse/truth-helpers";+import DButton from "discourse/ui-kit/d-button";+import { i18n } from "discourse-i18n";++const CARD_ROW_LIMIT = 8;++export default class SiteTrafficExplorerBreakdownCard extends Component {+ @service modal;++ @tracked activeTabIndex = 0;++ get activeTab() {+ return this.args.tabs[this.activeTabIndex] ?? this.args.tabs[0];+ }++ get rows() {+ return this.args.dimensions?.[this.activeTab.dimension] ?? [];+ }++ get visibleRows() {+ return this.rows.slice(0, CARD_ROW_LIMIT);+ }++ get canExpand() {+ return this.rows.length > CARD_ROW_LIMIT;+ }++ @action+ filterLabel(row) {+ return i18n("admin.site_traffic_explorer.filter_by", {+ label: row.label,+ count: row.pageviews,+ });+ }++ @action+ selectTab(index) {+ this.activeTabIndex = index;+ }++ @action+ navigateTabs(index, event) {+ let nextIndex;++ if (event.key === "ArrowLeft") {+ nextIndex = (index - 1 + this.args.tabs.length) % this.args.tabs.length;+ } else if (event.key === "ArrowRight") {+ nextIndex = (index + 1) % this.args.tabs.length;+ } else if (event.key === "Home") {+ nextIndex = 0;+ } else if (event.key === "End") {+ nextIndex = this.args.tabs.length - 1;+ } else {+ return;+ }++ event.preventDefault();+ this.selectTab(nextIndex);+ schedule("afterRender", () => {+ document+ .getElementById(+ `site-traffic-${this.args.name}-tab-${this.activeTab.dimension}`+ )+ ?.focus();+ });+ }++ @action+ async openModal() {+ const result = await this.modal.show(SiteTrafficExplorerBreakdownModal, {+ model: {+ title: this.activeTab.label,+ columnLabel: this.activeTab.columnLabel,+ dimension: this.activeTab.dimension,+ rows: this.rows,+ rowLink: this.rowLink,+ },+ });++ if (result?.filterRow) {+ this.filter(result.filterRow);+ }+ }++ @action+ rowLink(row) {+ if (!row.value) {+ return null;+ }++ if (this.activeTab.dimension === "referrers") {+ return {+ href: `https://${row.value}`,+ rel: "noopener noreferrer nofollow ugc",+ target: "_blank",+ };+ }++ if (["top_urls", "entry_urls"].includes(this.activeTab.dimension)) {+ return { href: getURL(row.value) };+ }++ return null;+ }++ @action+ filter(row) {+ const filterKey = this.activeTab.filter;+ this.args.setFilter(filterKey, row);++ next(() => {+ document+ .querySelector(`#site-traffic-filter-pill-${filterKey} button`)+ ?.focus();+ });+ }++ <template>+ <section+ class="db-section__row-block site-traffic-explorer__card"+ data-test-site-traffic-card={{@name}}+ >+ <h2 class="sr-only">{{@title}}</h2>+ <div+ class="site-traffic-explorer__tabs"+ role="tablist"+ aria-label={{@title}}+ >+ {{#each @tabs as |tab index|}}+ <button+ type="button"+ role="tab"+ id={{concat "site-traffic-" @name "-tab-" tab.dimension}}+ aria-controls={{concat "site-traffic-" @name "-panel"}}+ aria-selected={{if (eq index this.activeTabIndex) "true" "false"}}+ tabindex={{if (eq index this.activeTabIndex) "0" "-1"}}+ class={{if (eq index this.activeTabIndex) "is-active"}}+ {{on "click" (fn this.selectTab index)}}+ {{on "keydown" (fn this.navigateTabs index)}}+ >+ {{tab.label}}+ </button>+ {{/each}}+ </div>++ <div+ role="tabpanel"+ id={{concat "site-traffic-" @name "-panel"}}+ aria-labelledby={{concat+ "site-traffic-"+ @name+ "-tab-"+ this.activeTab.dimension+ }}+ >+ <ul class="site-traffic-explorer__breakdown-list">+ {{#each this.visibleRows as |row|}}+ <li>+ {{#let (this.rowLink row) as |rowLink|}}+ {{#if rowLink}}+ <span+ class="site-traffic-explorer__row"+ data-test-site-traffic-row+ >+ <a+ href={{rowLink.href}}+ rel={{rowLink.rel}}+ target={{rowLink.target}}+ class="site-traffic-explorer__row-link"+ >+ <SiteTrafficExplorerDimensionLabel+ @dimension={{this.activeTab.dimension}}+ @row={{row}}+ />+ </a>+ <button+ type="button"+ class="site-traffic-explorer__row-filter-area"+ aria-label={{this.filterLabel row}}+ {{on "click" (fn this.filter row)}}+ >+ <SiteTrafficExplorerPageviewCount+ @value={{row.pageviews}}+ as |formattedValue|+ >+ <span class="site-traffic-explorer__row-count">+ {{formattedValue}}+ </span>+ </SiteTrafficExplorerPageviewCount>+ </button>+ </span>+ {{else}}+ <button+ type="button"+ class="site-traffic-explorer__row"+ aria-label={{this.filterLabel row}}+ data-test-site-traffic-row+ {{on "click" (fn this.filter row)}}+ >+ <span>+ <SiteTrafficExplorerDimensionLabel+ @dimension={{this.activeTab.dimension}}+ @row={{row}}+ />+ </span>+ <SiteTrafficExplorerPageviewCount+ @value={{row.pageviews}}+ as |formattedValue|+ >+ <span class="site-traffic-explorer__row-count">+ {{formattedValue}}+ </span>+ </SiteTrafficExplorerPageviewCount>+ </button>+ {{/if}}+ {{/let}}+ </li>+ {{else}}+ <li class="site-traffic-explorer__card-empty">+ {{i18n "admin.site_traffic_explorer.no_dimension_data"}}+ </li>+ {{/each}}+ </ul>+ {{#if this.canExpand}}+ <DButton+ class="site-traffic-explorer__view-more btn-flat"+ @action={{this.openModal}}+ @label="admin.site_traffic_explorer.view_more"+ />+ {{/if}}+ </div>+ </section>+ </template>+}diff --git a/frontend/discourse/admin/components/site-traffic-explorer-breakdown-modal.gjs b/frontend/discourse/admin/components/site-traffic-explorer-breakdown-modal.gjsnew file mode 100644index 00000000000..e1413870658--- /dev/null+++ b/frontend/discourse/admin/components/site-traffic-explorer-breakdown-modal.gjs@@ -0,0 +1,97 @@+import Component from "@glimmer/component";+import { action } from "@ember/object";+import SiteTrafficExplorerDimensionLabel from "discourse/admin/components/site-traffic-explorer-dimension-label";+import SiteTrafficExplorerPageviewCount from "discourse/admin/components/site-traffic-explorer-pageview-count";+import DButton from "discourse/ui-kit/d-button";+import DModal from "discourse/ui-kit/d-modal";+import { i18n } from "discourse-i18n";++export default class SiteTrafficExplorerBreakdownModal extends Component {+ get rows() {+ return this.args.model.rows.slice(0, 50);+ }++ @action+ filterLabel(row) {+ return i18n("admin.site_traffic_explorer.filter_by", {+ label: row.label,+ count: row.pageviews,+ });+ }++ @action+ filter(row) {+ this.args.closeModal({ filterRow: row });+ }++ <template>+ <DModal+ @title={{@model.title}}+ @closeModal={{@closeModal}}+ class="site-traffic-breakdown-modal"+ >+ <:body>+ <table class="d-table">+ <thead class="d-table__header">+ <tr class="d-table__row">+ <th class="d-table__header-cell" scope="col">+ {{@model.columnLabel}}+ </th>+ <th+ class="d-table__header-cell site-traffic-breakdown-modal__pageviews"+ scope="col"+ >{{i18n "admin.site_traffic_explorer.pageviews"}}</th>+ </tr>+ </thead>+ <tbody class="d-table__body">+ {{#each this.rows as |row|}}+ <tr class="d-table__row">+ <td class="d-table__cell --overview">+ {{#let (@model.rowLink row) as |rowLink|}}+ {{#if rowLink}}+ <a+ href={{rowLink.href}}+ rel={{rowLink.rel}}+ target={{rowLink.target}}+ class="site-traffic-explorer__row-link"+ >+ <SiteTrafficExplorerDimensionLabel+ @dimension={{@model.dimension}}+ @row={{row}}+ />+ </a>+ {{else}}+ <SiteTrafficExplorerDimensionLabel+ @dimension={{@model.dimension}}+ @row={{row}}+ />+ {{/if}}+ {{/let}}+ </td>+ <td+ class="d-table__cell --detail site-traffic-breakdown-modal__pageviews"+ >+ <div class="d-table__mobile-label">+ {{i18n "admin.site_traffic_explorer.pageviews"}}+ </div>+ <SiteTrafficExplorerPageviewCount+ @value={{row.pageviews}}+ as |formattedValue|+ >+ <DButton+ @display="link"+ @translatedLabel={{formattedValue}}+ @translatedAriaLabel={{this.filterLabel row}}+ @action={{this.filter}}+ @actionParam={{row}}+ />+ </SiteTrafficExplorerPageviewCount>+ </td>+ </tr>+ {{/each}}+ </tbody>+ </table>+ </:body>+ </DModal>+ </template>+}diff --git a/frontend/discourse/admin/components/site-traffic-explorer-dimension-label.gjs b/frontend/discourse/admin/components/site-traffic-explorer-dimension-label.gjsnew file mode 100644index 00000000000..5b98c2fca64--- /dev/null+++ b/frontend/discourse/admin/components/site-traffic-explorer-dimension-label.gjs@@ -0,0 +1,36 @@+import Component from "@glimmer/component";+import { action } from "@ember/object";+import { countryFlag } from "discourse/admin/lib/format-country";+import { eq } from "discourse/truth-helpers";+import dIcon from "discourse/ui-kit/helpers/d-icon";++const BROWSER_ICONS = {+ edge: "fab-microsoft",+ firefox: "fab-firefox-browser",+ chrome: "fab-chrome",+ safari: "fab-apple",+ unknown: "globe",+};++export default class SiteTrafficExplorerDimensionLabel extends Component {+ @action+ countryFlag(value) {+ return countryFlag(value);+ }++ @action+ browserIcon(value) {+ return BROWSER_ICONS[value] ?? BROWSER_ICONS.unknown;+ }++ <template>+ <span class="site-traffic-explorer__dimension-label">+ {{#if (eq @dimension "countries")}}+ <span aria-hidden="true">{{this.countryFlag @row.value}}</span>+ {{else if (eq @dimension "browsers")}}+ {{dIcon (this.browserIcon @row.value)}}+ {{/if}}+ <span>{{@row.label}}</span>+ </span>+ </template>+}diff --git a/frontend/discourse/admin/components/site-traffic-explorer-filter-pills.gjs b/frontend/discourse/admin/components/site-traffic-explorer-filter-pills.gjsnew file mode 100644index 00000000000..f2f045f3c8d--- /dev/null+++ b/frontend/discourse/admin/components/site-traffic-explorer-filter-pills.gjs@@ -0,0 +1,69 @@+import Component from "@glimmer/component";+import { fn } from "@ember/helper";+import { action } from "@ember/object";+import { trustHTML } from "@ember/template";+import { escapeExpression } from "discourse/lib/utilities";+import DButton from "discourse/ui-kit/d-button";+import { i18n } from "discourse-i18n";++export default class SiteTrafficExplorerFilterPills extends Component {+ @action+ filterLabel(key) {+ return i18n(`admin.site_traffic_explorer.filters.${key}`);+ }++ @action+ removeLabel(filter) {+ if (filter.key === "traffic_type") {+ return i18n("admin.site_traffic_explorer.remove_traffic_type_filter", {+ value: filter.label,+ });+ }++ return i18n("admin.site_traffic_explorer.remove_filter", {+ filter: this.filterLabel(filter.key),+ });+ }++ @action+ filterDescription(filter) {+ return i18n("admin.site_traffic_explorer.filter_description", {+ filter: escapeExpression(this.filterLabel(filter.key)),+ value: escapeExpression(filter.label),+ });+ }++ @action+ filterId(filter) {+ const suffix = filter.key === "traffic_type" ? `-${filter.value}` : "";+ return `site-traffic-filter-pill-${filter.key}${suffix}`;+ }++ <template>+ {{#if @filters.length}}+ <div+ class="site-traffic-explorer__filters"+ role="group"+ aria-label={{i18n "admin.site_traffic_explorer.active_filters"}}+ >+ {{#each @filters as |filter|}}+ <span+ class="site-traffic-explorer__filter-pill"+ id={{this.filterId filter}}+ data-test-site-traffic-filter-pill={{filter.key}}+ >+ <span>+ {{trustHTML (this.filterDescription filter)}}+ </span>+ <DButton+ class="btn-flat site-traffic-explorer__filter-remove"+ @icon="xmark"+ @translatedAriaLabel={{this.removeLabel filter}}+ @action={{fn @removeFilter filter.key filter.value}}+ />+ </span>+ {{/each}}+ </div>+ {{/if}}+ </template>+}diff --git a/frontend/discourse/admin/components/site-traffic-explorer-metric.gjs b/frontend/discourse/admin/components/site-traffic-explorer-metric.gjsnew file mode 100644index 00000000000..3a9d525acf5--- /dev/null+++ b/frontend/discourse/admin/components/site-traffic-explorer-metric.gjs@@ -0,0 +1,38 @@+import { concat } from "@ember/helper";+import SiteTrafficExplorerPageviewCount from "discourse/admin/components/site-traffic-explorer-pageview-count";+import DTooltip from "discourse/float-kit/components/d-tooltip";+import { i18n } from "discourse-i18n";++export default <template>+ <div+ class="site-traffic-explorer__metric {{if @primary '--primary'}}"+ data-test-site-traffic-metric={{@name}}+ >+ {{#if @compact}}+ <SiteTrafficExplorerPageviewCount @value={{@value}} as |formattedValue|>+ <span+ class="db-section__metric-number site-traffic-explorer__metric-value"+ >{{formattedValue}}</span>+ </SiteTrafficExplorerPageviewCount>+ {{else}}+ <span+ class="db-section__metric-number site-traffic-explorer__metric-value"+ >{{@value}}</span>+ {{/if}}+ <span class="site-traffic-explorer__metric-label-row">+ <span+ class="db-section__metric-label site-traffic-explorer__metric-label"+ >{{@label}}</span>+ {{#if @tooltip}}+ <DTooltip+ class="db-section__info"+ @identifier={{concat "site-traffic-explorer-" @name "-tooltip"}}+ @icon="far-circle-question"+ @title={{i18n "admin.site_traffic_explorer.metric_information"}}+ >+ <:content>{{@tooltip}}</:content>+ </DTooltip>+ {{/if}}+ </span>+ </div>+</template>diff --git a/frontend/discourse/admin/components/site-traffic-explorer-pageview-count.gjs b/frontend/discourse/admin/components/site-traffic-explorer-pageview-count.gjsnew file mode 100644index 00000000000..e9e0fb3e9b3--- /dev/null+++ b/frontend/discourse/admin/components/site-traffic-explorer-pageview-count.gjs@@ -0,0 +1,11 @@+import {+ formatExactPageviewCount,+ formatPageviewCount,+} from "discourse/admin/lib/format-pageview-count";+import DTooltip from "discourse/float-kit/components/d-tooltip";++export default <template>+ <DTooltip @content={{formatExactPageviewCount @value}}>+ <:trigger>{{yield (formatPageviewCount @value)}}</:trigger>+ </DTooltip>+</template>diff --git a/frontend/discourse/admin/components/site-traffic-explorer.gjs b/frontend/discourse/admin/components/site-traffic-explorer.gjsnew file mode 100644index 00000000000..aaf8d577223--- /dev/null+++ b/frontend/discourse/admin/components/site-traffic-explorer.gjs@@ -0,0 +1,423 @@+import Component from "@glimmer/component";+import { action } from "@ember/object";+import didUpdate from "@ember/render-modifiers/modifiers/did-update";+import { service } from "@ember/service";+import AdminReportStackedChart from "discourse/admin/components/admin-report-stacked-chart";+import DashboardDateRange from "discourse/admin/components/dashboard/date-range";+import SiteTrafficExplorerBreakdownCard from "discourse/admin/components/site-traffic-explorer-breakdown-card";+import SiteTrafficExplorerFilterPills from "discourse/admin/components/site-traffic-explorer-filter-pills";+import SiteTrafficExplorerMetric from "discourse/admin/components/site-traffic-explorer-metric";+import { formatMinutesSeconds } from "discourse/lib/formatter";+import DBreadcrumbsItem from "discourse/ui-kit/d-breadcrumbs-item";+import DPageHeader from "discourse/ui-kit/d-page-header";+import I18n, { i18n } from "discourse-i18n";++const SKELETON_METRICS = Array.from({ length: 4 });+const SKELETON_BREAKDOWNS = Array.from({ length: 3 });+const SKELETON_ROWS = Array.from({ length: 5 });+const TRAFFIC_TYPE_BY_SERIES = {+ page_view_logged_in_browser: "logged_in",+ page_view_anon_browser: "anonymous",+ page_view_likely_crawler: "likely_crawler",+};++export default class SiteTrafficExplorer extends Component {+ @service a11y;+ @service siteSettings;++ get summary() {+ return this.args.traffic?.summary ?? {};+ }++ get metrics() {+ return [+ {+ name: "pageviews",+ label: i18n("admin.site_traffic_explorer.metrics.pageviews.label"),+ value: this.summary.pageviews ?? 0,+ compact: true,+ },+ {+ name: "distinct_sessions",+ label: i18n(+ "admin.site_traffic_explorer.metrics.distinct_sessions.label"+ ),+ value: this.#number(this.summary.distinct_sessions),+ },+ {+ name: "logged_in_share",+ label: i18n(+ "admin.site_traffic_explorer.metrics.logged_in_share.label"+ ),+ tooltip: i18n(+ "admin.dashboard.site_traffic.kpi.logged_in_share.tooltip"+ ),+ value: `${this.#number(this.summary.logged_in_share)}%`,+ },+ {+ name: "bounce_rate",+ label: i18n("admin.site_traffic_explorer.metrics.bounce_rate.label"),+ tooltip: i18n("admin.dashboard.site_traffic.kpi.bounce_rate.tooltip"),+ value: `${this.#number(this.summary.bounce_rate)}%`,+ },+ {+ name: "average_session_duration",+ label: i18n(+ "admin.site_traffic_explorer.metrics.average_session_duration.label"+ ),+ tooltip: i18n(+ "admin.dashboard.site_traffic.kpi.average_session_duration.tooltip"+ ),+ value: formatMinutesSeconds(+ this.summary.average_session_duration_seconds ?? 0+ ),+ },+ ];+ }++ get primaryMetric() {+ return this.metrics[0];+ }++ get secondaryMetrics() {+ return this.metrics.slice(1);+ }++ get chartModel() {+ return {+ start_date: moment(this.args.startDate).format("YYYY-MM-DD"),+ end_date: moment(this.args.endDate).format("YYYY-MM-DD"),+ data: this.series,+ };+ }++ get chartOptions() {+ return {+ hideYAxisGridLines: true,+ hiddenLabels: Object.entries(TRAFFIC_TYPE_BY_SERIES)+ .filter(+ ([, trafficType]) => !this.args.trafficTypes.includes(trafficType)+ )+ .map(([series]) => series),+ onLegendClick: this.toggleTrafficType,+ };+ }++ get series() {+ const rows = this.args.traffic?.series ?? [];+ const series = [+ {+ key: "logged-in-human",+ column: "logged_in_human_pageviews",+ req: "page_view_logged_in_browser",+ label: i18n("admin.site_traffic_explorer.series.logged_in_human"),+ color: this.args.traffic?.series_colors?.logged_in_human_pageviews,+ },+ {+ key: "anonymous-human",+ column: "anonymous_human_pageviews",+ req: "page_view_anon_browser",+ label: i18n("admin.site_traffic_explorer.series.anonymous_human"),+ color: this.args.traffic?.series_colors?.anonymous_human_pageviews,+ },+ ];++ if (this.siteSettings.improved_crawler_detection) {+ series.push({+ key: "likely-crawler",+ column: "likely_crawler_pageviews",+ req: "page_view_likely_crawler",+ label: i18n("admin.site_traffic_explorer.series.likely_crawler"),+ color: this.args.traffic?.series_colors?.likely_crawler_pageviews,+ });+ }++ return series.map((item) => ({+ ...item,+ total: rows.reduce((sum, row) => sum + (row[item.column] ?? 0), 0),+ data: rows.map((row) => ({ x: row.date, y: row[item.column] ?? 0 })),+ }));+ }++ get acquisitionTabs() {+ return [+ this.#tab({ dimension: "referrers", filter: "referrer" }),+ this.#tab({ dimension: "countries", filter: "country" }),+ this.#tab({ dimension: "networks", filter: "network" }),+ ];+ }++ get pagesTabs() {+ return [+ this.#tab({ dimension: "top_urls", filter: "top_url" }),+ this.#tab({ dimension: "entry_urls", filter: "entry_url" }),+ ];+ }++ get visitorsTabs() {+ return [+ this.#tab({ dimension: "browsers", filter: "browser" }),+ this.#tab({ dimension: "ip_addresses", filter: "ip" }),+ ];+ }++ get partialWarning() {+ const partial = this.args.traffic?.partial_data;+ if (!partial) {+ return null;+ }++ const availableDate = partial.available_start_date+ ? moment(partial.available_start_date).format("ll")+ : null;++ if (partial.reason === "retention_and_pageview_limit") {+ return i18n("admin.site_traffic_explorer.partial.combined", {+ date: availableDate,+ limit: this.#number(partial.pageview_limit),+ });+ }+ if (partial.reason === "retention") {+ return i18n("admin.site_traffic_explorer.partial.retention", {+ date: availableDate,+ });+ }+ return i18n("admin.site_traffic_explorer.partial.pageview_limit", {+ limit: this.#number(partial.pageview_limit),+ });+ }++ get fetchErrorMessage() {+ return i18n(+ `admin.site_traffic_explorer.errors.${this.args.fetchError ?? "unexpected"}`+ );+ }++ @action+ announceResults() {+ if (this.args.loading || !this.args.traffic) {+ return;+ }++ if (this.partialWarning) {+ this.a11y.announce(this.partialWarning, "polite");+ } else if (!this.args.hasPageviews) {+ this.a11y.announce(i18n("admin.site_traffic_explorer.empty"), "polite");+ }+ }++ @action+ toggleTrafficType(series) {+ this.args.toggleTrafficType(TRAFFIC_TYPE_BY_SERIES[series]);+ }++ #number(value) {+ return I18n.toNumber(value ?? 0, { precision: 0 });+ }++ #tab({ dimension, filter }) {+ return {+ dimension,+ filter,+ label: i18n(`admin.site_traffic_explorer.dimensions.${dimension}`),+ columnLabel: i18n(`admin.site_traffic_explorer.columns.${dimension}`),+ };+ }++ <template>+ <div+ class="site-traffic-explorer admin-config-page"+ {{didUpdate this.announceResults @traffic @loading @hasPageviews}}+ >+ <DPageHeader+ @titleLabel={{i18n "admin.site_traffic_explorer.title"}}+ @hideTabs={{true}}+ @collapseActionsOnMobile={{false}}+ >+ <:breadcrumbs>+ <DBreadcrumbsItem @path="/admin" @label={{i18n "admin_title"}} />+ <DBreadcrumbsItem+ @path="/admin"+ @label={{i18n "admin.dashboard.title"}}+ />+ <DBreadcrumbsItem+ @path="/admin/dashboard/site-traffic-explorer"+ @label={{i18n "admin.site_traffic_explorer.title"}}+ />+ </:breadcrumbs>+ <:actions>+ <DashboardDateRange+ @period={{@period}}+ @startDate={{@startDate}}+ @endDate={{@endDate}}+ @setPeriod={{@setPeriod}}+ @setCustomDateRange={{@setCustomDateRange}}+ />+ </:actions>+ </DPageHeader>++ <div class="admin-container site-traffic-explorer__content">+ <SiteTrafficExplorerFilterPills+ @filters={{@activeFilters}}+ @removeFilter={{@removeFilter}}+ />++ {{#if @fetchError}}+ <div+ class="db-section__wrapper --column site-traffic-explorer__report"+ >+ <div class="db-section__traffic-chart site-traffic-explorer__chart">+ <div class="db-section__traffic-chart-message" role="alert">+ {{this.fetchErrorMessage}}+ </div>+ </div>+ </div>+ {{else}}+ {{#if @loading}}+ <div+ class="db-skeleton --animation site-traffic-explorer__skeleton"+ role="status"+ aria-label={{i18n "admin.site_traffic_explorer.loading"}}+ data-test-site-traffic-skeleton+ >+ <div class="db-skeleton__section-wrapper">+ <div class="db-skeleton__subheader">+ <div class="db-skeleton__subintro">+ <div class="db-skeleton__heading-line"></div>+ </div>+ <div class="db-skeleton__metric-row">+ {{#each SKELETON_METRICS}}+ <div class="db-skeleton__metric">+ <div class="db-skeleton__metric-number"></div>+ <div class="db-skeleton__metric-label"></div>+ </div>+ {{/each}}+ </div>+ </div>+ <div class="db-skeleton__chart"></div>+ <div class="db-skeleton__row">+ {{#each SKELETON_BREAKDOWNS}}+ <div class="db-skeleton__row-block">+ <div class="db-skeleton__row-block-title"></div>+ <ul class="db-skeleton__list">+ {{#each SKELETON_ROWS}}+ <li class="db-skeleton__list-row">+ <span class="db-skeleton__list-name"></span>+ <span class="db-skeleton__list-value"></span>+ </li>+ {{/each}}+ </ul>+ </div>+ {{/each}}+ </div>+ </div>+ </div>+ {{else if this.partialWarning}}+ <p+ class="alert alert-warning site-traffic-explorer__partial-warning"+ data-test-site-traffic-partial-warning+ data-test-partial-reason+ >+ {{this.partialWarning}}+ </p>+ {{/if}}++ {{#if @traffic}}+ <div class="site-traffic-explorer__results" hidden={{@loading}}>+ {{#if @hasPageviews}}+ <div+ class="db-section__wrapper --column site-traffic-explorer__report"+ >+ <section+ class="db-section__subheader site-traffic-explorer__metrics"+ aria-labelledby="site-traffic-explorer-metrics"+ >+ <h2 id="site-traffic-explorer-metrics" class="sr-only">+ {{i18n "admin.site_traffic_explorer.summary"}}+ </h2>+ <div class="db-section__subintro">+ <SiteTrafficExplorerMetric+ @name={{this.primaryMetric.name}}+ @label={{this.primaryMetric.label}}+ @tooltip={{this.primaryMetric.tooltip}}+ @value={{this.primaryMetric.value}}+ @compact={{this.primaryMetric.compact}}+ @primary={{true}}+ />+ </div>+ <div class="db-section__metrics">+ {{#each this.secondaryMetrics as |metric|}}+ <SiteTrafficExplorerMetric+ @name={{metric.name}}+ @label={{metric.label}}+ @tooltip={{metric.tooltip}}+ @value={{metric.value}}+ @compact={{metric.compact}}+ />+ {{/each}}+ </div>+ </section>++ <div+ class="db-section__traffic-chart site-traffic-explorer__chart"+ aria-label={{i18n+ "admin.site_traffic_explorer.traffic_over_time"+ }}+ >+ <AdminReportStackedChart+ @model={{this.chartModel}}+ @options={{this.chartOptions}}+ class="db-section__traffic-chart-canvas"+ />+ <div class="sr-only">+ {{#each this.series as |series|}}+ <span+ data-test-traffic-series={{series.key}}+ >{{series.total}}</span>+ {{/each}}+ </div>+ </div>++ <div class="db-section__row site-traffic-explorer__cards">+ <SiteTrafficExplorerBreakdownCard+ @name="acquisition"+ @title={{i18n+ "admin.site_traffic_explorer.cards.acquisition"+ }}+ @tabs={{this.acquisitionTabs}}+ @dimensions={{@traffic.dimensions}}+ @setFilter={{@setFilter}}+ />+ <SiteTrafficExplorerBreakdownCard+ @name="pages"+ @title={{i18n "admin.site_traffic_explorer.cards.pages"}}+ @tabs={{this.pagesTabs}}+ @dimensions={{@traffic.dimensions}}+ @setFilter={{@setFilter}}+ />+ <SiteTrafficExplorerBreakdownCard+ @name="visitors"+ @title={{i18n+ "admin.site_traffic_explorer.cards.visitors"+ }}+ @tabs={{this.visitorsTabs}}+ @dimensions={{@traffic.dimensions}}+ @setFilter={{@setFilter}}+ />+ </div>+ </div>+ {{else}}+ <div+ class="site-traffic-explorer__empty"+ data-test-site-traffic-empty+ >+ {{i18n "admin.site_traffic_explorer.empty"}}+ </div>+ {{/if}}+ </div>+ {{/if}}+ {{/if}}+ </div>+ </div>+ </template>+}diff --git a/frontend/discourse/admin/controllers/admin-site-traffic.js b/frontend/discourse/admin/controllers/admin-site-traffic.jsnew file mode 100644index 00000000000..03be251084e--- /dev/null+++ b/frontend/discourse/admin/controllers/admin-site-traffic.js@@ -0,0 +1,283 @@+import { tracked } from "@glimmer/tracking";+import Controller from "@ember/controller";+import { action } from "@ember/object";+import {+ calculatePresetStartDate,+ DEFAULT_PERIOD,+ PERIOD_CUSTOM,+ VALID_PERIODS,+} from "discourse/admin/lib/dashboard-date-range";+import { countryName } from "discourse/admin/lib/format-country";+import { ajax } from "discourse/lib/ajax";+import { i18n } from "discourse-i18n";++const FILTER_KEYS = [+ "traffic_type",+ "top_url",+ "entry_url",+ "referrer",+ "country",+ "network",+ "browser",+ "ip",+];++const TRAFFIC_TYPES = ["logged_in", "anonymous", "likely_crawler"];+const TRAFFIC_TYPE_LABEL_KEYS = {+ logged_in: "logged_in_human",+ anonymous: "anonymous_human",+ likely_crawler: "likely_crawler",+};++const DIMENSION_KEYS = {+ top_url: "top_urls",+ entry_url: "entry_urls",+ referrer: "referrers",+ country: "countries",+ network: "networks",+ browser: "browsers",+ ip: "ip_addresses",+};++export default class AdminSiteTrafficController extends Controller {+ @tracked range = DEFAULT_PERIOD;+ @tracked start_date = null;+ @tracked end_date = null;+ @tracked traffic_type = null;+ @tracked top_url = null;+ @tracked entry_url = null;+ @tracked referrer = null;+ @tracked country = null;+ @tracked network = null;+ @tracked browser = null;+ @tracked ip = null;+ @tracked traffic = null;+ @tracked loading = false;+ @tracked fetchError = null;++ queryParams = ["range", "start_date", "end_date", ...FILTER_KEYS];++ #fetchId = 0;++ get safePeriod() {+ if (!VALID_PERIODS.includes(this.range)) {+ return DEFAULT_PERIOD;+ }+ if (this.range === PERIOD_CUSTOM && (!this.start_date || !this.end_date)) {+ return DEFAULT_PERIOD;+ }+ return this.range;+ }++ get startDate() {+ return (+ this.#customDate(this.start_date, "startOf") ??+ calculatePresetStartDate(this.safePeriod)+ );+ }++ get endDate() {+ return (+ this.#customDate(this.end_date, "endOf") ?? moment().endOf("day").toDate()+ );+ }++ get hasPageviews() {+ return (this.traffic?.series ?? []).some(+ (row) =>+ (row.pageviews ?? 0) > 0 || (row.likely_crawler_pageviews ?? 0) > 0+ );+ }++ get activeFilters() {+ return FILTER_KEYS.flatMap((key) => {+ const values =+ key === "traffic_type"+ ? this.traffic_type === null+ ? []+ : this.selectedTrafficTypes+ : [this[key]];++ return values+ .filter((value) => value !== null)+ .map((value) => {+ const rows = this.traffic?.dimensions?.[DIMENSION_KEYS[key]] ?? [];+ const activeFilter = this.traffic?.active_filters?.find(+ (filter) => filter.key === key && filter.value === value+ );+ const label =+ activeFilter?.label ??+ rows.find((row) => row.value === value)?.label;++ return {+ key,+ value,+ label:+ label ??+ (key === "traffic_type"+ ? i18n(+ `admin.site_traffic_explorer.series.${TRAFFIC_TYPE_LABEL_KEYS[value]}`+ )+ : key === "referrer" && value === ""+ ? i18n("admin.site_traffic_explorer.direct_or_unknown")+ : value),+ };+ });+ });+ }++ get selectedTrafficTypes() {+ if (this.traffic_type === null) {+ return TRAFFIC_TYPES;+ }++ const selected = this.traffic_type.split(",");+ return TRAFFIC_TYPES.filter((trafficType) =>+ selected.includes(trafficType)+ );+ }++ #customDate(value, edge) {+ if (this.safePeriod !== PERIOD_CUSTOM || !value) {+ return null;+ }++ const parsed = moment(value, "YYYY-MM-DD", true);+ return parsed.isValid() ? parsed[edge]("day").toDate() : null;+ }++ #requestParams() {+ const params = {+ start_date: moment(this.startDate).format("YYYY-MM-DD"),+ end_date: moment(this.endDate).format("YYYY-MM-DD"),+ };++ for (const key of FILTER_KEYS) {+ if (this[key] !== null) {+ params[key] = this[key];+ }+ }++ return params;+ }++ #localizeCountryLabels(traffic) {+ const countries = traffic.dimensions?.countries ?? [];+ const activeFilters = traffic.active_filters ?? [];++ return {+ ...traffic,+ dimensions: {+ ...traffic.dimensions,+ countries: countries.map((row) => ({+ ...row,+ label: countryName(row.value),+ })),+ },+ active_filters: activeFilters.map((filter) =>+ filter.key === "country"+ ? { ...filter, label: countryName(filter.value) }+ : filter+ ),+ };+ }++ @action+ async fetchTraffic() {+ const fetchId = ++this.#fetchId;+ this.loading = true;+ this.fetchError = null;++ try {+ const traffic = await ajax(+ "/admin/dashboard/site-traffic-explorer.json",+ {+ data: this.#requestParams(),+ }+ );++ if (fetchId === this.#fetchId) {+ this.traffic = this.#localizeCountryLabels(traffic);+ }+ } catch (error) {+ if (fetchId === this.#fetchId) {+ this.fetchError =+ error.jqXHR?.responseJSON?.error_type === "traffic_query_timeout"+ ? "timeout"+ : "unexpected";+ }+ } finally {+ if (fetchId === this.#fetchId) {+ this.loading = false;+ }+ }+ }++ @action+ setPeriod(period) {+ this.range = period;+ this.start_date = null;+ this.end_date = null;+ }++ @action+ setCustomDateRange(startDate, endDate) {+ this.range = PERIOD_CUSTOM;+ this.start_date = moment(startDate).format("YYYY-MM-DD");+ this.end_date = moment(endDate).format("YYYY-MM-DD");+ }++ @action+ setFilter(key, row) {+ this[key] = row.value;+ }++ @action+ toggleTrafficType(trafficType) {+ const selectedTrafficTypes = this.selectedTrafficTypes;+ const nextTrafficTypes = selectedTrafficTypes.includes(trafficType)+ ? selectedTrafficTypes.filter((selected) => selected !== trafficType)+ : [...selectedTrafficTypes, trafficType];++ this.#setTrafficTypes(nextTrafficTypes);+ }++ @action+ removeFilter(key, value) {+ if (key === "traffic_type") {+ const remainingTrafficTypes = this.selectedTrafficTypes.filter(+ (trafficType) => trafficType !== value+ );+ this.#setTrafficTypes(remainingTrafficTypes);+ return;+ }++ this[key] = null;+ }++ @action+ resetState() {+ this.#fetchId++;+ this.range = DEFAULT_PERIOD;+ this.start_date = null;+ this.end_date = null;+ for (const key of FILTER_KEYS) {+ this[key] = null;+ }+ this.traffic = null;+ this.loading = false;+ this.fetchError = null;+ }++ #setTrafficTypes(trafficTypes) {+ const orderedTrafficTypes = TRAFFIC_TYPES.filter((trafficType) =>+ trafficTypes.includes(trafficType)+ );++ this.traffic_type =+ orderedTrafficTypes.length === 0 ||+ orderedTrafficTypes.length === TRAFFIC_TYPES.length+ ? null+ : orderedTrafficTypes.join(",");+ }+}diff --git a/frontend/discourse/admin/lib/format-pageview-count.js b/frontend/discourse/admin/lib/format-pageview-count.jsnew file mode 100644index 00000000000..835af5a91b9--- /dev/null+++ b/frontend/discourse/admin/lib/format-pageview-count.js@@ -0,0 +1,18 @@+import I18n from "discourse-i18n";++export function formatPageviewCount(value) {+ if (value >= 1_000_000) {+ const formatted = I18n.toNumber(value / 1_000_000, { precision: 1 });+ return `${formatted.replace(/[,.]0$/, "")}M`;+ }++ if (value >= 1_000) {+ return `${I18n.toNumber(Math.round(value / 1_000), { precision: 0 })}K`;+ }++ return I18n.toNumber(value, { precision: 0 });+}++export function formatExactPageviewCount(value) {+ return I18n.toNumber(value, { precision: 0 });+}diff --git a/frontend/discourse/admin/routes/admin-route-map.js b/frontend/discourse/admin/routes/admin-route-map.jsindex 212655a6a5f..a8f20702c59 100644--- a/frontend/discourse/admin/routes/admin-route-map.js+++ b/frontend/discourse/admin/routes/admin-route-map.js@@ -16,6 +16,10 @@ export default function () {resetNamespace: true,});});+ this.route("adminSiteTraffic", {+ path: "/dashboard/site-traffic-explorer",+ resetNamespace: true,+ });this.route("adminSiteSettings",diff --git a/frontend/discourse/admin/routes/admin-site-traffic.js b/frontend/discourse/admin/routes/admin-site-traffic.jsnew file mode 100644index 00000000000..6bf43993a90--- /dev/null+++ b/frontend/discourse/admin/routes/admin-site-traffic.js@@ -0,0 +1,37 @@+import DiscourseRoute from "discourse/routes/discourse";+import { i18n } from "discourse-i18n";++export default class AdminSiteTrafficRoute extends DiscourseRoute {+ queryParams = {+ range: { refreshModel: true },+ start_date: { refreshModel: true },+ end_date: { refreshModel: true },+ traffic_type: { refreshModel: true },+ top_url: { refreshModel: true },+ entry_url: { refreshModel: true },+ referrer: { refreshModel: true },+ country: { refreshModel: true },+ network: { refreshModel: true },+ browser: { refreshModel: true },+ ip: { refreshModel: true },+ };++ titleToken() {+ return i18n("admin.site_traffic_explorer.title");+ }++ model(params) {+ return params;+ }++ setupController(controller) {+ super.setupController(...arguments);+ controller.fetchTraffic();+ }++ resetController(controller, isExiting) {+ if (isExiting) {+ controller.resetState();+ }+ }+}diff --git a/frontend/discourse/admin/templates/admin-site-traffic.gjs b/frontend/discourse/admin/templates/admin-site-traffic.gjsnew file mode 100644index 00000000000..1c589311cad--- /dev/null+++ b/frontend/discourse/admin/templates/admin-site-traffic.gjs@@ -0,0 +1,20 @@+import SiteTrafficExplorer from "discourse/admin/components/site-traffic-explorer";++export default <template>+ <SiteTrafficExplorer+ @traffic={{@controller.traffic}}+ @loading={{@controller.loading}}+ @fetchError={{@controller.fetchError}}+ @hasPageviews={{@controller.hasPageviews}}+ @period={{@controller.safePeriod}}+ @startDate={{@controller.startDate}}+ @endDate={{@controller.endDate}}+ @activeFilters={{@controller.activeFilters}}+ @trafficTypes={{@controller.selectedTrafficTypes}}+ @setPeriod={{@controller.setPeriod}}+ @setCustomDateRange={{@controller.setCustomDateRange}}+ @setFilter={{@controller.setFilter}}+ @toggleTrafficType={{@controller.toggleTrafficType}}+ @removeFilter={{@controller.removeFilter}}+ />+</template>diff --git a/lib/browser_pageview_referrer_inspector.rb b/lib/browser_pageview_event_url_normalizer.rbsimilarity index 52%rename from lib/browser_pageview_referrer_inspector.rbrename to lib/browser_pageview_event_url_normalizer.rbindex 26c49b87bad..744cf50272c 100644--- a/lib/browser_pageview_referrer_inspector.rb+++ b/lib/browser_pageview_event_url_normalizer.rb@@ -1,14 +1,19 @@# frozen_string_literal: true-# Normalizes referrer URLs captured by the browser pageview middleware so the-# same logical referrer groups consistently in the top-referrers report. It-# strips scheme, `www.`, port, fragment, trailing slashes, and common tracking-# query params, converts the host to lowercase punycode, and truncates the-# result to 200 bytes.-class BrowserPageviewReferrerInspector- # Bump when the normalization logic changes significantly to trigger a- # re-backfill of rows stamped with an older version.- VERSION = 1+# Normalizes referrer URLs captured by browser pageview events so the same+# logical referrer groups consistently in the top-referrers report. It strips+# scheme, `www.`, port, fragment, trailing slashes, and common tracking query+# params, converts the host to lowercase punycode, and truncates the result to+# 2,000 bytes. It also normalizes pageview URLs to canonical site-relative+# paths for the Site Traffic Explorer.+class BrowserPageviewEventUrlNormalizer+ # Bump when referrer normalization changes significantly to re-backfill rows+ # stamped with an older version.+ REFERRER_VERSION = 1++ # Bump when site-path normalization changes significantly to re-backfill rows+ # stamped with an older version.+ SITE_PATH_VERSION = 1# TODO: consider vendoring DuckDuckGo's Tracker Radar tracking-parameter list# (https://github.com/duckduckgo/tracker-radar) for broader, maintained@@ -30,25 +35,29 @@ class BrowserPageviewReferrerInspectorMAX_LENGTH = 2000- def self.normalize(raw)- return nil if raw.blank?-- # Scheme is intentionally dropped: `http://example.com/x` and- # `https://example.com/x` collapse to the same key so the report groups- # cross-protocol traffic together.- uri = Addressable::URI.parse(raw.to_s.strip)+ def self.normalize_referrer(raw)+ uri = parse_uri(raw)return nil if uri.nil?host = normalize_host(uri.host)return nil if host.blank?- path = uri.path.to_s.sub(%r{/+\z}, "")+ path = normalized_path(uri)filtered_query = filter_query(uri.query)query_str = filtered_query.empty? ? "" : "?#{filtered_query}""#{host}#{path}#{query_str}".byteslice(0, MAX_LENGTH).scrub("")- rescue Addressable::URI::InvalidURIError, ArgumentError, TypeError- nil+ end++ def self.normalize_site_path(raw)+ uri = parse_uri(raw)+ return nil if uri.nil?+ return nil if uri.scheme.present? && (!%w[http https].include?(uri.scheme) || uri.host.blank?)++ path = normalized_path(uri)+ path = "/#{path}" if !path.start_with?("/")+ path = "/" if path.blank?+ path.byteslice(0, MAX_LENGTH).scrub("")enddef self.normalize_host(host)@@ -74,4 +83,18 @@ class BrowserPageviewReferrerInspector.join("&")endprivate_class_method :filter_query++ def self.normalized_path(uri)+ uri.path.to_s.sub(%r{/+\z}, "")+ end+ private_class_method :normalized_path++ def self.parse_uri(raw)+ return nil if raw.blank?++ Addressable::URI.parse(raw.to_s.strip)+ rescue Addressable::URI::InvalidURIError, ArgumentError, TypeError+ nil+ end+ private_class_method :parse_urienddiff --git a/lib/svg_sprite.rb b/lib/svg_sprite.rbindex 9393e959bb8..2dbc2d70b00 100644--- a/lib/svg_sprite.rb+++ b/lib/svg_sprite.rb@@ -125,6 +125,7 @@ module SvgSpritefab-discoursefab-facebookfab-facebook-square+ fab-firefox-browserfab-githubfab-googlefab-instagramdiff --git a/plugins/discourse-ai/lib/admin_dashboard/admin_dashboard_facts.rb b/plugins/discourse-ai/lib/admin_dashboard/admin_dashboard_facts.rbindex 0426a0f6241..c09e8de8437 100644--- a/plugins/discourse-ai/lib/admin_dashboard/admin_dashboard_facts.rb+++ b/plugins/discourse-ai/lib/admin_dashboard/admin_dashboard_facts.rb@@ -417,7 +417,7 @@ module DiscourseAienddef external_referrer?(referrer)- site_host = BrowserPageviewReferrerInspector.normalize_host(Discourse.current_hostname)+ site_host = BrowserPageviewEventUrlNormalizer.normalize_host(Discourse.current_hostname)referrer = referrer.to_sreturn false if referrer.blank? || site_host.blank?diff --git a/spec/fabricators/browser_pageview_event_fabricator.rb b/spec/fabricators/browser_pageview_event_fabricator.rbindex 0e10e47bd62..a3aaa548f70 100644--- a/spec/fabricators/browser_pageview_event_fabricator.rb+++ b/spec/fabricators/browser_pageview_event_fabricator.rb@@ -2,6 +2,8 @@Fabricator(:browser_pageview_event) dourl "https://example.com/"+ normalized_url { |attrs| BrowserPageviewEventUrlNormalizer.normalize_site_path(attrs[:url]) }+ normalized_url_version BrowserPageviewEventUrlNormalizer::SITE_PATH_VERSIONip_address "1.2.3.4"user_agent "test"session_id { SecureRandom.hex(16) }diff --git a/spec/jobs/maintain_browser_pageview_rollups_spec.rb b/spec/jobs/maintain_browser_pageview_rollups_spec.rbindex a86fe5fafcd..55197c963d2 100644--- a/spec/jobs/maintain_browser_pageview_rollups_spec.rb+++ b/spec/jobs/maintain_browser_pageview_rollups_spec.rb@@ -257,9 +257,11 @@ RSpec.describe Jobs::MaintainBrowserPageviewRollups dojob.execute({})expect(event.reload.normalized_referrer).to eq(- BrowserPageviewReferrerInspector.normalize(raw),+ BrowserPageviewEventUrlNormalizer.normalize_referrer(raw),+ )+ expect(event.normalized_referrer_version).to eq(+ BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,)- expect(event.normalized_referrer_version).to eq(BrowserPageviewReferrerInspector::VERSION)endit "only backfills referrers from the source that applies on the event's date" do@@ -299,7 +301,7 @@ RSpec.describe Jobs::MaintainBrowserPageviewRollups doexpect(post_cutover_piggyback.reload.normalized_referrer_version).to be_nilexpect(post_cutover_beacon.reload.normalized_referrer).to eq("reddit.com")expect(post_cutover_beacon.normalized_referrer_version).to eq(- BrowserPageviewReferrerInspector::VERSION,+ BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,)end@@ -315,7 +317,7 @@ RSpec.describe Jobs::MaintainBrowserPageviewRollups doexpect(direct_visit.reload.normalized_referrer_version).to be_nilexpect(referred.reload.normalized_referrer_version).to eq(- BrowserPageviewReferrerInspector::VERSION,+ BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,)expect { job.execute({}) }.not_to change { referred.reload.normalized_referrer_version }end@@ -327,7 +329,9 @@ RSpec.describe Jobs::MaintainBrowserPageviewRollups doevent.reloadexpect(event.normalized_referrer).to be_nil- expect(event.normalized_referrer_version).to eq(BrowserPageviewReferrerInspector::VERSION)+ expect(event.normalized_referrer_version).to eq(+ BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,+ )endit "is a no-op once every referrer row is current" do@@ -409,7 +413,7 @@ RSpec.describe Jobs::MaintainBrowserPageviewRollups doexpect(prunable.normalized_referrer_version).to be_nilexpect(near_cutoff.reload.normalized_referrer_version).to be_nilexpect(recent.reload.normalized_referrer_version).to eq(- BrowserPageviewReferrerInspector::VERSION,+ BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,)end@@ -425,7 +429,7 @@ RSpec.describe Jobs::MaintainBrowserPageviewRollups dojob.execute({})expect(event.reload.normalized_referrer_version).to eq(- BrowserPageviewReferrerInspector::VERSION,+ BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,)end@@ -476,7 +480,7 @@ RSpec.describe Jobs::MaintainBrowserPageviewRollups dojob.execute({})expect(event.reload.normalized_referrer_version).to eq(- BrowserPageviewReferrerInspector::VERSION,+ BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,)expect(BrowserPageviewReferrerDailyRollup.where(date:).pluck(:normalized_referrer, :count),@@ -491,20 +495,24 @@ RSpec.describe Jobs::MaintainBrowserPageviewRollups do:browser_pageview_event,referrer: raw,normalized_referrer: "google.com/?utm_source=newsletter",- normalized_referrer_version: BrowserPageviewReferrerInspector::VERSION,+ normalized_referrer_version: BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,created_at: date,)stub_const(- BrowserPageviewReferrerInspector,- "VERSION",- BrowserPageviewReferrerInspector::VERSION + 1,+ BrowserPageviewEventUrlNormalizer,+ "REFERRER_VERSION",+ BrowserPageviewEventUrlNormalizer::REFERRER_VERSION + 1,) dojob.execute({})event.reload- expect(event.normalized_referrer).to eq(BrowserPageviewReferrerInspector.normalize(raw))- expect(event.normalized_referrer_version).to eq(BrowserPageviewReferrerInspector::VERSION)+ expect(event.normalized_referrer).to eq(+ BrowserPageviewEventUrlNormalizer.normalize_referrer(raw),+ )+ expect(event.normalized_referrer_version).to eq(+ BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,+ )endexpect(BrowserPageviewReferrerDailyRollup.where(date:).pluck(:normalized_referrer)).to eq(@@ -512,5 +520,73 @@ RSpec.describe Jobs::MaintainBrowserPageviewRollups do)endend++ context "when backfilling URLs" do+ it "normalizes retained stale rows and stamps the current version" do+ event =+ Fabricate(+ :browser_pageview_event,+ url: "https://forum.example/latest/?sort=recent#section",+ )+ event.update_columns(normalized_url: nil, normalized_url_version: nil)++ job.execute({})++ event.reload+ expect(event.normalized_url).to eq("/latest")+ expect(event.normalized_url_version).to eq(+ BrowserPageviewEventUrlNormalizer::SITE_PATH_VERSION,+ )+ end++ it "resumes bounded batches without revisiting current rows" do+ SiteSetting.browser_pageview_referrer_backfill_batch_size = 1+ first = Fabricate(:browser_pageview_event, url: "/first")+ second = Fabricate(:browser_pageview_event, url: "/second")+ [first, second].each do |event|+ event.update_columns(normalized_url: nil, normalized_url_version: nil)+ end++ job.execute({})++ expect(+ BrowserPageviewEvent.where(normalized_url_version: nil).pluck(:id),+ ).to contain_exactly(second.id)++ job.execute({})++ expect(BrowserPageviewEvent.where(normalized_url_version: nil)).to be_empty+ end++ it "re-normalizes rows stamped with an older version" do+ raw = "https://forum.example/latest/?sort=recent#section"+ event = Fabricate(:browser_pageview_event, url: raw)++ stub_const(+ BrowserPageviewEventUrlNormalizer,+ "SITE_PATH_VERSION",+ BrowserPageviewEventUrlNormalizer::SITE_PATH_VERSION + 1,+ ) do+ job.execute({})++ event.reload+ expect(event.normalized_url).to eq(+ BrowserPageviewEventUrlNormalizer.normalize_site_path(raw),+ )+ expect(event.normalized_url_version).to eq(+ BrowserPageviewEventUrlNormalizer::SITE_PATH_VERSION,+ )+ end+ end++ it "does not backfill rows outside the retained window" do+ event = Fabricate(:browser_pageview_event, url: "/expired", created_at: 4.months.ago)+ event.update_columns(normalized_url: nil, normalized_url_version: nil)++ job.execute({})++ expect(event.reload.normalized_url_version).to be_nil+ end+ endendenddiff --git a/spec/lib/browser_pageview_event_url_normalizer_spec.rb b/spec/lib/browser_pageview_event_url_normalizer_spec.rbnew file mode 100644index 00000000000..209e4091062--- /dev/null+++ b/spec/lib/browser_pageview_event_url_normalizer_spec.rb@@ -0,0 +1,151 @@+# frozen_string_literal: true++describe BrowserPageviewEventUrlNormalizer do+ describe ".normalize_referrer" do+ it "returns nil for blank input" do+ expect(described_class.normalize_referrer(nil)).to be_nil+ expect(described_class.normalize_referrer("")).to be_nil+ expect(described_class.normalize_referrer(" ")).to be_nil+ end++ it "returns nil for malformed input or URLs without a host" do+ expect(described_class.normalize_referrer("not a url")).to be_nil+ expect(described_class.normalize_referrer("javascript:alert(1)")).to be_nil+ expect(described_class.normalize_referrer("data:text/html,<x>")).to be_nil+ expect(described_class.normalize_referrer("https://")).to be_nil+ expect(described_class.normalize_referrer("file:///etc/passwd")).to be_nil+ end++ it "strips scheme, lowercases host, strips www and trailing dot" do+ expect(described_class.normalize_referrer("https://www.Example.com/path")).to eq(+ "example.com/path",+ )+ expect(described_class.normalize_referrer("HTTP://EXAMPLE.COM/Path")).to eq(+ "example.com/Path",+ )+ expect(described_class.normalize_referrer("https://example.com./path")).to eq(+ "example.com/path",+ )+ end++ it "strips the port from any host" do+ expect(described_class.normalize_referrer("https://example.com:3000/path")).to eq(+ "example.com/path",+ )+ expect(described_class.normalize_referrer("https://example.com:443/path")).to eq(+ "example.com/path",+ )+ expect(described_class.normalize_referrer("https://example.com:80")).to eq("example.com")+ end++ it "strips fragment" do+ expect(described_class.normalize_referrer("https://example.com/path#section")).to eq(+ "example.com/path",+ )+ end++ it "strips trailing slash on path" do+ expect(described_class.normalize_referrer("https://example.com/path/")).to eq(+ "example.com/path",+ )+ expect(described_class.normalize_referrer("https://example.com/")).to eq("example.com")+ end++ it "strips multiple trailing slashes" do+ expect(described_class.normalize_referrer("https://example.com/path//")).to eq(+ "example.com/path",+ )+ expect(described_class.normalize_referrer("https://example.com/path///")).to eq(+ "example.com/path",+ )+ expect(described_class.normalize_referrer("https://example.com///")).to eq("example.com")+ end++ it "collapses IDN and punycode forms to the same host" do+ unicode = described_class.normalize_referrer("https://münchen.de/foo")+ punycode = described_class.normalize_referrer("https://xn--mnchen-3ya.de/foo")+ expect(unicode).to eq(punycode)+ expect(unicode).to eq("xn--mnchen-3ya.de/foo")+ end++ it "drops tracking query params but keeps others" do+ expect(described_class.normalize_referrer("https://example.com/p?utm_source=x&id=42")).to eq(+ "example.com/p?id=42",+ )+ expect(+ described_class.normalize_referrer(+ "https://news.ycombinator.com/item?id=12345&utm_campaign=x",+ ),+ ).to eq("news.ycombinator.com/item?id=12345")+ expect(described_class.normalize_referrer("https://example.com/p?utm_source=x")).to eq(+ "example.com/p",+ )+ expect(+ described_class.normalize_referrer("https://example.com/p?fbclid=abc&gclid=def"),+ ).to eq("example.com/p")+ end++ it "preserves original query encoding (does not re-encode)" do+ expect(described_class.normalize_referrer("https://example.com/p?q=foo%20bar")).to eq(+ "example.com/p?q=foo%20bar",+ )+ expect(described_class.normalize_referrer("https://example.com/p?q=foo+bar")).to eq(+ "example.com/p?q=foo+bar",+ )+ end++ it "preserves query order and duplicates among kept params" do+ expect(described_class.normalize_referrer("https://example.com/p?a=1&b=2&a=3")).to eq(+ "example.com/p?a=1&b=2&a=3",+ )+ end++ it "truncates to 2000 bytes and produces valid UTF-8" do+ long_path = "x" * 2200+ result = described_class.normalize_referrer("https://example.com/#{long_path}")+ expect(result.bytesize).to be <= 2000+ expect(result).to be_valid_encoding+ end++ it "scrubs invalid bytes left by mid-character truncation" do+ multibyte = "ü" * 1100+ result = described_class.normalize_referrer("https://example.com/#{multibyte}")+ expect(result.bytesize).to be <= 2000+ expect(result).to be_valid_encoding+ end+ end++ describe ".normalize_host" do+ it "lowercases, strips www prefix, and strips trailing dot" do+ expect(described_class.normalize_host("Forum.Example.Com")).to eq("forum.example.com")+ expect(described_class.normalize_host("www.example.com")).to eq("example.com")+ expect(described_class.normalize_host("example.com.")).to eq("example.com")+ end++ it "converts Unicode hosts to punycode" do+ expect(described_class.normalize_host("münchen.de")).to eq("xn--mnchen-3ya.de")+ expect(described_class.normalize_host("xn--mnchen-3ya.de")).to eq("xn--mnchen-3ya.de")+ end++ it "returns nil for blank input" do+ expect(described_class.normalize_host(nil)).to be_nil+ expect(described_class.normalize_host("")).to be_nil+ end+ end++ describe ".normalize_site_path" do+ it "returns a canonical site-relative path" do+ expect(+ described_class.normalize_site_path("https://forum.example/latest/?sort=recent#section"),+ ).to eq("/latest")+ expect(described_class.normalize_site_path("/top?sort=popular")).to eq("/top")+ expect(described_class.normalize_site_path("about/")).to eq("/about")+ expect(described_class.normalize_site_path("https://forum.example/")).to eq("/")+ end++ it "returns nil for an unusable URL" do+ expect(described_class.normalize_site_path(nil)).to be_nil+ expect(described_class.normalize_site_path("javascript:alert(1)")).to be_nil+ end+ end+enddiff --git a/spec/lib/browser_pageview_referrer_inspector_spec.rb b/spec/lib/browser_pageview_referrer_inspector_spec.rbdeleted file mode 100644index 0f0243f4c50..00000000000--- a/spec/lib/browser_pageview_referrer_inspector_spec.rb+++ /dev/null@@ -1,115 +0,0 @@-# frozen_string_literal: true--describe BrowserPageviewReferrerInspector do- describe ".normalize" do- it "returns nil for blank input" do- expect(described_class.normalize(nil)).to be_nil- expect(described_class.normalize("")).to be_nil- expect(described_class.normalize(" ")).to be_nil- end-- it "returns nil for malformed input or URLs without a host" do- expect(described_class.normalize("not a url")).to be_nil- expect(described_class.normalize("javascript:alert(1)")).to be_nil- expect(described_class.normalize("data:text/html,<x>")).to be_nil- expect(described_class.normalize("https://")).to be_nil- expect(described_class.normalize("file:///etc/passwd")).to be_nil- end-- it "strips scheme, lowercases host, strips www and trailing dot" do- expect(described_class.normalize("https://www.Example.com/path")).to eq("example.com/path")- expect(described_class.normalize("HTTP://EXAMPLE.COM/Path")).to eq("example.com/Path")- expect(described_class.normalize("https://example.com./path")).to eq("example.com/path")- end-- it "strips the port from any host" do- expect(described_class.normalize("https://example.com:3000/path")).to eq("example.com/path")- expect(described_class.normalize("https://example.com:443/path")).to eq("example.com/path")- expect(described_class.normalize("https://example.com:80")).to eq("example.com")- end-- it "strips fragment" do- expect(described_class.normalize("https://example.com/path#section")).to eq(- "example.com/path",- )- end-- it "strips trailing slash on path" do- expect(described_class.normalize("https://example.com/path/")).to eq("example.com/path")- expect(described_class.normalize("https://example.com/")).to eq("example.com")- end-- it "strips multiple trailing slashes" do- expect(described_class.normalize("https://example.com/path//")).to eq("example.com/path")- expect(described_class.normalize("https://example.com/path///")).to eq("example.com/path")- expect(described_class.normalize("https://example.com///")).to eq("example.com")- end-- it "collapses IDN and punycode forms to the same host" do- unicode = described_class.normalize("https://münchen.de/foo")- punycode = described_class.normalize("https://xn--mnchen-3ya.de/foo")- expect(unicode).to eq(punycode)- expect(unicode).to eq("xn--mnchen-3ya.de/foo")- end-- it "drops tracking query params but keeps others" do- expect(described_class.normalize("https://example.com/p?utm_source=x&id=42")).to eq(- "example.com/p?id=42",- )- expect(- described_class.normalize("https://news.ycombinator.com/item?id=12345&utm_campaign=x"),- ).to eq("news.ycombinator.com/item?id=12345")- expect(described_class.normalize("https://example.com/p?utm_source=x")).to eq("example.com/p")- expect(described_class.normalize("https://example.com/p?fbclid=abc&gclid=def")).to eq(- "example.com/p",- )- end-- it "preserves original query encoding (does not re-encode)" do- expect(described_class.normalize("https://example.com/p?q=foo%20bar")).to eq(- "example.com/p?q=foo%20bar",- )- expect(described_class.normalize("https://example.com/p?q=foo+bar")).to eq(- "example.com/p?q=foo+bar",- )- end-- it "preserves query order and duplicates among kept params" do- expect(described_class.normalize("https://example.com/p?a=1&b=2&a=3")).to eq(- "example.com/p?a=1&b=2&a=3",- )- end-- it "truncates to 2000 bytes and produces valid UTF-8" do- long_path = "x" * 2200- result = described_class.normalize("https://example.com/#{long_path}")- expect(result.bytesize).to be <= 2000- expect(result).to be_valid_encoding- end-- it "scrubs invalid bytes left by mid-character truncation" do- multibyte = "ü" * 1100- result = described_class.normalize("https://example.com/#{multibyte}")- expect(result.bytesize).to be <= 2000- expect(result).to be_valid_encoding- end- end-- describe ".normalize_host" do- it "lowercases, strips www prefix, and strips trailing dot" do- expect(described_class.normalize_host("Forum.Example.Com")).to eq("forum.example.com")- expect(described_class.normalize_host("www.example.com")).to eq("example.com")- expect(described_class.normalize_host("example.com.")).to eq("example.com")- end-- it "converts Unicode hosts to punycode" do- expect(described_class.normalize_host("münchen.de")).to eq("xn--mnchen-3ya.de")- expect(described_class.normalize_host("xn--mnchen-3ya.de")).to eq("xn--mnchen-3ya.de")- end-- it "returns nil for blank input" do- expect(described_class.normalize_host(nil)).to be_nil- expect(described_class.normalize_host("")).to be_nil- end- end-enddiff --git a/spec/lib/middleware/request_tracker_spec.rb b/spec/lib/middleware/request_tracker_spec.rbindex 1b827084fb1..f0ec6206c44 100644--- a/spec/lib/middleware/request_tracker_spec.rb+++ b/spec/lib/middleware/request_tracker_spec.rb@@ -968,7 +968,7 @@ RSpec.describe Middleware::RequestTracker do}end- it "populates normalized_referrer via BrowserPageviewReferrerInspector" do+ it "populates normalized_referrer via BrowserPageviewEventUrlNormalizer" dosession_id = "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx"data =@@ -989,7 +989,9 @@ RSpec.describe Middleware::RequestTracker doevent = BrowserPageviewEvent.lastexpect(event.referrer).to eq("https://www.example.com/path?utm_source=x")expect(event.normalized_referrer).to eq("example.com/path")- expect(event.normalized_referrer_version).to eq(BrowserPageviewReferrerInspector::VERSION)+ expect(event.normalized_referrer_version).to eq(+ BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,+ )endit "stores nil normalized_referrer when the referrer is blank" dodiff --git a/spec/models/browser_pageview_event_spec.rb b/spec/models/browser_pageview_event_spec.rbindex 30fcb11d9ff..40e4542c6cf 100644--- a/spec/models/browser_pageview_event_spec.rb+++ b/spec/models/browser_pageview_event_spec.rb@@ -188,6 +188,10 @@ RSpec.describe BrowserPageviewEvent doexpect(event.url).to eq(payload[:url])expect(event.country_code).to eq("AU")expect(event.asn).to eq(12_345)+ expect(event.normalized_url).to eq("/t/topic/1")+ expect(event.normalized_url_version).to eq(+ BrowserPageviewEventUrlNormalizer::SITE_PATH_VERSION,+ )expect(event.normalized_referrer).to eq("example.com/path")expect(event.created_at).to eq_time(occurred_at)expect(event.source).to eq("beacon")diff --git a/spec/models/browser_pageview_session_engagement_daily_rollup_spec.rb b/spec/models/browser_pageview_session_engagement_daily_rollup_spec.rbindex 9f58b960b66..60dce237463 100644--- a/spec/models/browser_pageview_session_engagement_daily_rollup_spec.rb+++ b/spec/models/browser_pageview_session_engagement_daily_rollup_spec.rb@@ -1,6 +1,12 @@# frozen_string_literal: trueRSpec.describe BrowserPageviewSessionEngagementDailyRollup do+ describe ".bounce_engaged_seconds_threshold" do+ it "exposes the dashboard bounce threshold" do+ expect(described_class.bounce_engaged_seconds_threshold).to eq(10)+ end+ end+describe ".aggregate" dolet(:start_date) { Date.new(2026, 6, 1) }let(:end_date) { Date.new(2026, 6, 30) }diff --git a/spec/requests/admin/dashboard_controller_spec.rb b/spec/requests/admin/dashboard_controller_spec.rbindex 87c221b66b4..96b4a2d4029 100644--- a/spec/requests/admin/dashboard_controller_spec.rb+++ b/spec/requests/admin/dashboard_controller_spec.rb@@ -1384,6 +1384,610 @@ RSpec.describe Admin::DashboardController doendend+ describe "#traffic" do+ let(:request_params) { { start_date: "2026-05-01", end_date: "2026-05-12" } }+ let(:series_colors) do+ {+ "logged_in_human_pageviews" => "#4B3CE0",+ "anonymous_human_pageviews" => "#9C8DEC",+ "likely_crawler_pageviews" => "#B3AAC9",+ }+ end++ before do+ freeze_time(Time.zone.local(2026, 5, 14, 12, 0, 0))+ SiteSetting.dashboard_improvements = true+ SiteSetting.improved_crawler_detection = true+ SiteSetting.persist_browser_pageview_events = true+ SiteSetting.use_legacy_pageviews = false+ BrowserPageviewEvent.stubs(:beacon_cutover_date).returns(Date.new(2026, 1, 1))+ Discourse.stubs(:current_hostname).returns("test.localhost")+ DiscourseIpInfo.stubs(:get).returns(asn: 64_496, organization: "Example Network")+ DiscourseIpInfo+ .stubs(:get)+ .with("192.0.2.1", locale: anything, resolve_hostname: false)+ .returns(+ country_code: "US",+ country: "United States",+ asn: 64_496,+ organization: "Example Network",+ )+ DiscourseIpInfo+ .stubs(:get)+ .with("198.51.100.2", locale: anything, resolve_hostname: false)+ .returns(+ country_code: "GB",+ country: "United Kingdom",+ asn: 64_496,+ organization: "Example Network",+ )+ end++ context "with traffic in the selected date range" do+ fab!(:landing_pageview) do+ Fabricate(+ :browser_pageview_event,+ url: "https://test.localhost/landing/?source=newsletter#section",+ country_code: "US",+ asn: 64_496,+ ip_address: "192.0.2.1",+ user_agent: "Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36",+ user_id: admin.id,+ session_id: "admin-session",+ normalized_referrer: "search.example/results?q=discourse",+ normalized_referrer_version: BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: "2026-05-10 10:00:00",+ )+ end++ fab!(:latest_pageview) do+ Fabricate(+ :browser_pageview_event,+ url: "https://test.localhost/latest?page=2",+ country_code: "US",+ asn: 64_496,+ ip_address: "192.0.2.1",+ user_agent: "Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36",+ user_id: admin.id,+ session_id: "admin-session",+ normalized_referrer: "ignored.example/return?from=latest",+ normalized_referrer_version: BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: "2026-05-10 10:01:00",+ )+ end++ fab!(:top_pageview) do+ Fabricate(+ :browser_pageview_event,+ url: "/top",+ country_code: "GB",+ asn: 64_496,+ ip_address: "198.51.100.2",+ user_agent: "Mozilla/5.0 Firefox/126.0",+ session_id: "anonymous-session",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: "2026-05-11 10:00:00",+ )+ end++ fab!(:admin_session_engagement) do+ Fabricate(+ :browser_pageview_session_engagement,+ session_id: "admin-session",+ engaged_seconds: 60,+ )+ end++ fab!(:anonymous_session_engagement) do+ Fabricate(+ :browser_pageview_session_engagement,+ session_id: "anonymous-session",+ engaged_seconds: 0,+ )+ end++ before { sign_in(admin) }++ it "summarizes every traffic dimension for the selected dates" do+ get "/admin/dashboard/site-traffic-explorer.json", params: request_params++ expect(status: response.status, body: response.parsed_body).to eq(+ status: 200,+ body: {+ "partial_data" => nil,+ "summary" => {+ "pageviews" => 3,+ "distinct_sessions" => 2,+ "logged_in_share" => 67,+ "bounce_rate" => 50,+ "average_session_duration_seconds" => 30,+ },+ "series" => [+ {+ "date" => "2026-05-10",+ "pageviews" => 2,+ "logged_in_human_pageviews" => 2,+ "anonymous_human_pageviews" => 0,+ "likely_crawler_pageviews" => 0,+ },+ {+ "date" => "2026-05-11",+ "pageviews" => 1,+ "logged_in_human_pageviews" => 0,+ "anonymous_human_pageviews" => 1,+ "likely_crawler_pageviews" => 0,+ },+ ],+ "series_colors" => series_colors,+ "dimensions" => {+ "top_urls" => [+ { "value" => "/landing", "label" => "/landing", "pageviews" => 1 },+ { "value" => "/latest", "label" => "/latest", "pageviews" => 1 },+ { "value" => "/top", "label" => "/top", "pageviews" => 1 },+ ],+ "entry_urls" => [+ { "value" => "/landing", "label" => "/landing", "pageviews" => 1 },+ { "value" => "/top", "label" => "/top", "pageviews" => 1 },+ ],+ "referrers" => [+ { "value" => "", "label" => "Direct / unknown", "pageviews" => 1 },+ {+ "value" => "search.example/results?q=discourse",+ "label" => "search.example/results?q=discourse",+ "pageviews" => 1,+ },+ ],+ "countries" => [+ { "value" => "US", "label" => "United States", "pageviews" => 2 },+ { "value" => "GB", "label" => "United Kingdom", "pageviews" => 1 },+ ],+ "networks" => [+ { "value" => "AS64496", "label" => "Example Network (AS64496)", "pageviews" => 3 },+ ],+ "browsers" => [+ { "value" => "chrome", "label" => "Chrome", "pageviews" => 2 },+ { "value" => "firefox", "label" => "Firefox", "pageviews" => 1 },+ ],+ "ip_addresses" => [+ { "value" => "192.0.2.1", "label" => "192.0.2.1", "pageviews" => 2 },+ { "value" => "198.51.100.2", "label" => "198.51.100.2", "pageviews" => 1 },+ ],+ },+ },+ )+ end++ it "applies the direct referrer filter" do+ get "/admin/dashboard/site-traffic-explorer.json",+ params: request_params.merge(referrer: "")++ expect(status: response.status, body: response.parsed_body).to eq(+ status: 200,+ body: {+ "partial_data" => nil,+ "summary" => {+ "pageviews" => 1,+ "distinct_sessions" => 1,+ "logged_in_share" => 0,+ "bounce_rate" => 100,+ "average_session_duration_seconds" => 0,+ },+ "series" => [+ {+ "date" => "2026-05-11",+ "pageviews" => 1,+ "logged_in_human_pageviews" => 0,+ "anonymous_human_pageviews" => 1,+ "likely_crawler_pageviews" => 0,+ },+ ],+ "series_colors" => series_colors,+ "dimensions" => {+ "top_urls" => [{ "value" => "/top", "label" => "/top", "pageviews" => 1 }],+ "entry_urls" => [{ "value" => "/top", "label" => "/top", "pageviews" => 1 }],+ "referrers" => [{ "value" => "", "label" => "Direct / unknown", "pageviews" => 1 }],+ "countries" => [{ "value" => "GB", "label" => "United Kingdom", "pageviews" => 1 }],+ "networks" => [+ { "value" => "AS64496", "label" => "Example Network (AS64496)", "pageviews" => 1 },+ ],+ "browsers" => [{ "value" => "firefox", "label" => "Firefox", "pageviews" => 1 }],+ "ip_addresses" => [+ { "value" => "198.51.100.2", "label" => "198.51.100.2", "pageviews" => 1 },+ ],+ },+ "active_filters" => [+ { "key" => "referrer", "value" => "", "label" => "Direct / unknown" },+ ],+ },+ )+ end+ end++ context "when the selected date range exceeds retention" do+ it "returns partial retained results and the available start date" do+ sign_in(admin)+ chrome = "Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36"+ firefox = "Mozilla/5.0 Firefox/126.0"++ Fabricate(+ :browser_pageview_event,+ url: "/first-retained",+ country_code: "US",+ asn: 64_496,+ ip_address: "192.0.2.1",+ user_agent: chrome,+ session_id: "first-retained",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: "2026-02-15 09:00:00",+ )+ Fabricate(+ :browser_pageview_event,+ url: "/latest-retained",+ country_code: "GB",+ asn: 64_496,+ ip_address: "198.51.100.2",+ user_agent: firefox,+ session_id: "latest-retained",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: "2026-05-10 10:00:00",+ )++ get "/admin/dashboard/site-traffic-explorer.json",+ params: request_params.merge(start_date: "2026-01-01")++ expect(status: response.status, body: response.parsed_body).to eq(+ status: 200,+ body: {+ "partial_data" => {+ "reason" => "retention",+ "available_start_date" => "2026-02-14",+ },+ "summary" => {+ "pageviews" => 2,+ "distinct_sessions" => 2,+ "logged_in_share" => 0,+ "bounce_rate" => 100,+ "average_session_duration_seconds" => 0,+ },+ "series" => [+ {+ "date" => "2026-02-15",+ "pageviews" => 1,+ "logged_in_human_pageviews" => 0,+ "anonymous_human_pageviews" => 1,+ "likely_crawler_pageviews" => 0,+ },+ {+ "date" => "2026-05-10",+ "pageviews" => 1,+ "logged_in_human_pageviews" => 0,+ "anonymous_human_pageviews" => 1,+ "likely_crawler_pageviews" => 0,+ },+ ],+ "series_colors" => series_colors,+ "dimensions" => {+ "top_urls" => [+ { "value" => "/first-retained", "label" => "/first-retained", "pageviews" => 1 },+ { "value" => "/latest-retained", "label" => "/latest-retained", "pageviews" => 1 },+ ],+ "entry_urls" => [+ { "value" => "/first-retained", "label" => "/first-retained", "pageviews" => 1 },+ { "value" => "/latest-retained", "label" => "/latest-retained", "pageviews" => 1 },+ ],+ "referrers" => [{ "value" => "", "label" => "Direct / unknown", "pageviews" => 2 }],+ "countries" => [+ { "value" => "GB", "label" => "United Kingdom", "pageviews" => 1 },+ { "value" => "US", "label" => "United States", "pageviews" => 1 },+ ],+ "networks" => [+ { "value" => "AS64496", "label" => "Example Network (AS64496)", "pageviews" => 2 },+ ],+ "browsers" => [+ { "value" => "chrome", "label" => "Chrome", "pageviews" => 1 },+ { "value" => "firefox", "label" => "Firefox", "pageviews" => 1 },+ ],+ "ip_addresses" => [+ { "value" => "192.0.2.1", "label" => "192.0.2.1", "pageviews" => 1 },+ { "value" => "198.51.100.2", "label" => "198.51.100.2", "pageviews" => 1 },+ ],+ },+ },+ )+ end+ end++ context "when traffic reaches the configured pageview cap" do+ let(:created_at) { Time.zone.parse("2026-05-10 10:00:00") }+ let(:chrome) { "Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36" }+ let(:event_attributes) do+ {+ country_code: "US",+ asn: 64_496,+ ip_address: "192.0.2.1",+ user_agent: chrome,+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: created_at,+ }+ end+ let!(:oldest) do+ Fabricate(+ :browser_pageview_event,+ url: "/oldest-id",+ session_id: "oldest-id",+ **event_attributes,+ )+ end+ let!(:middle) do+ Fabricate(+ :browser_pageview_event,+ url: "/middle-id",+ session_id: "middle-id",+ **event_attributes,+ )+ end+ let!(:newest) do+ Fabricate(+ :browser_pageview_event,+ url: "/newest-id",+ session_id: "newest-id",+ **event_attributes,+ )+ end++ before do+ sign_in(admin)+ SiteSetting.stubs(:admin_site_traffic_event_cap).returns(2)+ end++ it "returns no more than the configured pageview cap" do+ get "/admin/dashboard/site-traffic-explorer.json", params: request_params++ expect(status: response.status, body: response.parsed_body).to eq(+ status: 200,+ body: {+ "partial_data" => {+ "reason" => "pageview_limit",+ "pageview_limit" => 2,+ },+ "summary" => {+ "pageviews" => 2,+ "distinct_sessions" => 2,+ "logged_in_share" => 0,+ "bounce_rate" => 100,+ "average_session_duration_seconds" => 0,+ },+ "series" => [+ {+ "date" => "2026-05-10",+ "pageviews" => 2,+ "logged_in_human_pageviews" => 0,+ "anonymous_human_pageviews" => 2,+ "likely_crawler_pageviews" => 0,+ },+ ],+ "series_colors" => series_colors,+ "dimensions" => {+ "top_urls" => [+ { "value" => middle.url, "label" => middle.url, "pageviews" => 1 },+ { "value" => newest.url, "label" => newest.url, "pageviews" => 1 },+ ],+ "entry_urls" => [+ { "value" => middle.url, "label" => middle.url, "pageviews" => 1 },+ { "value" => newest.url, "label" => newest.url, "pageviews" => 1 },+ ],+ "referrers" => [{ "value" => "", "label" => "Direct / unknown", "pageviews" => 2 }],+ "countries" => [{ "value" => "US", "label" => "United States", "pageviews" => 2 }],+ "networks" => [+ { "value" => "AS64496", "label" => "Example Network (AS64496)", "pageviews" => 2 },+ ],+ "browsers" => [{ "value" => "chrome", "label" => "Chrome", "pageviews" => 2 }],+ "ip_addresses" => [+ { "value" => "192.0.2.1", "label" => "192.0.2.1", "pageviews" => 2 },+ ],+ },+ },+ )+ end+ end++ context "when the selected date range exceeds retention and traffic reaches the cap" do+ let(:event_attributes) { { asn: 64_496, source: BrowserPageviewEvent::SOURCE_BEACON } }+ let!(:first_retained) do+ Fabricate(+ :browser_pageview_event,+ url: "/first-retained",+ country_code: "US",+ ip_address: "192.0.2.1",+ user_agent: "Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36",+ session_id: "first-retained",+ created_at: "2026-02-15 09:00:00",+ **event_attributes,+ )+ end+ let!(:middle_retained) do+ Fabricate(+ :browser_pageview_event,+ url: "/middle-retained",+ country_code: "GB",+ ip_address: "198.51.100.2",+ user_agent: "Mozilla/5.0 Firefox/126.0",+ session_id: "middle-retained",+ created_at: "2026-05-10 10:00:00",+ **event_attributes,+ )+ end+ let!(:latest_retained) do+ Fabricate(+ :browser_pageview_event,+ url: "/latest-retained",+ country_code: "US",+ ip_address: "192.0.2.1",+ user_agent: "Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36",+ session_id: "latest-retained",+ created_at: "2026-05-11 10:00:00",+ **event_attributes,+ )+ end++ it "returns both partial-data reasons" do+ sign_in(admin)+ SiteSetting.stubs(:admin_site_traffic_event_cap).returns(2)++ get "/admin/dashboard/site-traffic-explorer.json",+ params: request_params.merge(start_date: "2026-01-01")++ expect(status: response.status, body: response.parsed_body).to eq(+ status: 200,+ body: {+ "partial_data" => {+ "reason" => "retention_and_pageview_limit",+ "available_start_date" => "2026-02-14",+ "pageview_limit" => 2,+ },+ "summary" => {+ "pageviews" => 2,+ "distinct_sessions" => 2,+ "logged_in_share" => 0,+ "bounce_rate" => 100,+ "average_session_duration_seconds" => 0,+ },+ "series" => [+ {+ "date" => "2026-05-10",+ "pageviews" => 1,+ "logged_in_human_pageviews" => 0,+ "anonymous_human_pageviews" => 1,+ "likely_crawler_pageviews" => 0,+ },+ {+ "date" => "2026-05-11",+ "pageviews" => 1,+ "logged_in_human_pageviews" => 0,+ "anonymous_human_pageviews" => 1,+ "likely_crawler_pageviews" => 0,+ },+ ],+ "series_colors" => series_colors,+ "dimensions" => {+ "top_urls" => [+ {+ "value" => latest_retained.url,+ "label" => latest_retained.url,+ "pageviews" => 1,+ },+ {+ "value" => middle_retained.url,+ "label" => middle_retained.url,+ "pageviews" => 1,+ },+ ],+ "entry_urls" => [+ {+ "value" => latest_retained.url,+ "label" => latest_retained.url,+ "pageviews" => 1,+ },+ {+ "value" => middle_retained.url,+ "label" => middle_retained.url,+ "pageviews" => 1,+ },+ ],+ "referrers" => [{ "value" => "", "label" => "Direct / unknown", "pageviews" => 2 }],+ "countries" => [+ { "value" => "GB", "label" => "United Kingdom", "pageviews" => 1 },+ { "value" => "US", "label" => "United States", "pageviews" => 1 },+ ],+ "networks" => [+ { "value" => "AS64496", "label" => "Example Network (AS64496)", "pageviews" => 2 },+ ],+ "browsers" => [+ { "value" => "chrome", "label" => "Chrome", "pageviews" => 1 },+ { "value" => "firefox", "label" => "Firefox", "pageviews" => 1 },+ ],+ "ip_addresses" => [+ { "value" => "192.0.2.1", "label" => "192.0.2.1", "pageviews" => 1 },+ { "value" => "198.51.100.2", "label" => "198.51.100.2", "pageviews" => 1 },+ ],+ },+ },+ )+ end+ end++ it "returns a service unavailable response when the traffic query times out" do+ sign_in(admin)+ DB.stubs(:query_hash).raises(ActiveRecord::QueryCanceled, "statement timeout")++ get "/admin/dashboard/site-traffic-explorer.json", params: request_params++ expect(status: response.status, body: response.parsed_body).to eq(+ status: 503,+ body: {+ "error_type" => "traffic_query_timeout",+ },+ )+ end++ it "does not allow moderators to read traffic details" do+ sign_in(moderator)++ get "/admin/dashboard/site-traffic-explorer.json", params: request_params++ expect(status: response.status, body: response.parsed_body).to eq(+ status: 404,+ body: {+ "errors" => ["The requested URL or resource could not be found."],+ "error_type" => "not_found",+ },+ )+ end++ it "does not allow regular users to read traffic details" do+ sign_in(user)++ get "/admin/dashboard/site-traffic-explorer.json", params: request_params++ expect(status: response.status, body: response.parsed_body).to eq(+ status: 404,+ body: {+ "errors" => ["The requested URL or resource could not be found."],+ "error_type" => "not_found",+ },+ )+ end++ it "does not allow anonymous users to read traffic details" do+ get "/admin/dashboard/site-traffic-explorer.json", params: request_params++ expect(status: response.status, body: response.parsed_body).to eq(+ status: 404,+ body: {+ "errors" => ["The requested URL or resource could not be found."],+ "error_type" => "not_found",+ },+ )+ end++ it "does not allow access while dashboard improvements are disabled" do+ sign_in(admin)+ SiteSetting.dashboard_improvements = false++ get "/admin/dashboard/site-traffic-explorer.json", params: request_params++ expect(status: response.status, body: response.parsed_body).to eq(+ status: 404,+ body: {+ "errors" => ["The requested URL or resource could not be found."],+ "error_type" => "not_found",+ },+ )+ end+ end+describe "#available_reports" dobefore { AdminDashboardReport.delete_all }diff --git a/spec/services/admin_dashboard_site_traffic_explorer_spec.rb b/spec/services/admin_dashboard_site_traffic_explorer_spec.rbnew file mode 100644index 00000000000..e760f2f835f--- /dev/null+++ b/spec/services/admin_dashboard_site_traffic_explorer_spec.rb@@ -0,0 +1,326 @@+# frozen_string_literal: true++RSpec.describe AdminDashboardSiteTrafficExplorer do+ describe described_class::Contract, type: :model do+ it { is_expected.to validate_presence_of(:start_date) }+ it { is_expected.to validate_presence_of(:end_date) }++ it "requires the start date to precede the end date" do+ contract =+ described_class.new(start_date: Date.new(2026, 5, 13), end_date: Date.new(2026, 5, 12))++ expect(contract).not_to be_valid+ end+ end++ describe ".call" do+ subject(:result) { described_class.call(params:, **dependencies) }++ let(:params) { { start_date: "2026-05-01", end_date: "2026-05-12" } }+ let(:dependencies) { {} }++ let(:user_agents) do+ [+ "Mozilla/5.0 Chrome/124.0 Safari/537.36 Edg/124.0",+ "Mozilla/5.0 Chrome/124.0 Safari/537.36 OPR/109.0",+ "Mozilla/5.0 Firefox/126.0",+ "Mozilla/5.0 FxiOS/126.0 Mobile/15E148 Safari/605.1.15",+ "Mozilla/5.0 CriOS/124.0 Mobile/15E148 Safari/604.1",+ "Mozilla/5.0 Version/17.0 Mobile/15E148 Safari/604.1",+ "Mozilla/5.0 SamsungBrowser/24.0 Chrome/120.0 Mobile Safari/537.36",+ "Mozilla/5.0 Vivaldi/6.7 Chrome/124.0 Safari/537.36",+ "Mozilla/5.0 Trident/7.0; rv:11.0",+ "Discourse/163 CFNetwork/978.0.7 Darwin/18.6.0",+ "ExampleBrowser/1.0",+ ]+ end++ let!(:pageviews) do+ user_agents.each_with_index do |user_agent, index|+ Fabricate(+ :browser_pageview_event,+ url: "/browser-#{index}",+ country_code: "US",+ asn: 64_496,+ ip_address: "192.0.2.#{index + 1}",+ session_id: "browser-#{index}",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ user_agent:,+ created_at: Time.zone.local(2026, 5, 10, 10, index),+ )+ end+ end++ before do+ freeze_time(Time.zone.local(2026, 5, 14, 12, 0, 0))+ SiteSetting.improved_crawler_detection = true+ SiteSetting.persist_browser_pageview_events = true+ SiteSetting.use_legacy_pageviews = false+ BrowserPageviewEvent.stubs(:beacon_cutover_date).returns(Date.new(2026, 1, 1))+ end++ context "when the contract is invalid" do+ let(:params) { super().except(:start_date) }++ it { is_expected.to fail_a_contract }+ end++ context "when the query is canceled by Active Record" do+ before { DB.stubs(:query_hash).raises(ActiveRecord::QueryCanceled, "statement timeout") }++ it "returns the timeout failure and leaves the connection usable" do+ transaction_depth = ActiveRecord::Base.connection.open_transactions++ expect(result).to fail_a_step(:load_traffic)+ expect(result["result.step.load_traffic"].error).to eq("traffic_query_timeout")+ expect(+ [ActiveRecord::Base.connection.open_transactions, DB.query_single("SELECT 1")],+ ).to eq([transaction_depth, [1]])+ end+ end++ context "when the query is canceled by PostgreSQL" do+ before { DB.stubs(:query_hash).raises(PG::QueryCanceled, "statement timeout") }++ it "returns the named timeout failure" do+ expect(result).to fail_a_step(:load_traffic)+ expect(result["result.step.load_traffic"].error).to eq("traffic_query_timeout")+ end+ end++ context "when the query succeeds" do+ it { is_expected.to run_successfully }++ it "classifies supported major browsers and groups other user agents as unknown" do+ browsers = result.traffic.dig(:dimensions, "browsers")++ expect(browsers).to eq(+ [+ { value: "unknown", label: "Unknown browser", pageviews: 4 },+ { value: "chrome", label: "Chrome", pageviews: 3 },+ { value: "safari", label: "Safari", pageviews: 2 },+ { value: "edge", label: "Microsoft Edge", pageviews: 1 },+ { value: "firefox", label: "Firefox", pageviews: 1 },+ ],+ )+ end++ it "uses only local IP data to produce country and network labels" do+ DiscourseIpInfo+ .expects(:get)+ .with("192.0.2.1", locale: I18n.locale, resolve_hostname: false)+ .twice+ .returns(+ country_code: "US",+ country: "United States",+ asn: 64_496,+ organization: "Example Network",+ )++ dimensions = result.traffic.fetch(:dimensions)++ expect(dimensions.slice("countries", "networks")).to eq(+ "countries" => [{ value: "US", label: "United States", pageviews: 11 }],+ "networks" => [{ value: "AS64496", label: "Example Network (AS64496)", pageviews: 11 }],+ )+ end++ it "uses the canonical country code when local IP data has no localized country name" do+ DiscourseIpInfo.stubs(:get).returns(country_code: "US", asn: 64_496)++ countries = I18n.with_locale(:de) { result.traffic.dig(:dimensions, "countries") }++ expect(countries).to eq([{ value: "US", label: "US", pageviews: 11 }])+ end++ it "labels active country and network filters independently of their intersection" do+ Fabricate(+ :browser_pageview_event,+ country_code: "GB",+ asn: 64_500,+ ip_address: "198.51.100.1",+ session_id: "other-network",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: Time.zone.local(2026, 5, 10, 11),+ )+ DiscourseIpInfo+ .stubs(:get)+ .with("192.0.2.1", locale: I18n.locale, resolve_hostname: false)+ .returns(country_code: "US", country: "United States", asn: 64_496)+ DiscourseIpInfo+ .stubs(:get)+ .with("198.51.100.1", locale: I18n.locale, resolve_hostname: false)+ .returns(asn: 64_500, organization: "Other Network")++ traffic =+ described_class.call(params: params.merge(country: "US", network: "AS64500")).traffic++ expect(traffic.slice(:summary, :active_filters)).to eq(+ summary: {+ "pageviews" => 0,+ "distinct_sessions" => 0,+ "logged_in_share" => 0,+ "bounce_rate" => 0,+ "average_session_duration_seconds" => 0,+ },+ active_filters: [+ { key: :country, value: "US", label: "United States" },+ { key: :network, value: "AS64500", label: "Other Network (AS64500)" },+ ],+ )+ end++ it "runs one read-only analytics statement with a ten-second deadline" do+ query_row = {+ "pageview_limited" => false,+ "summary" => {+ },+ "series" => [],+ "dimensions" => {+ },+ "active_filter_representative_ips" => {+ "country" => nil,+ "network" => nil,+ },+ }+ DB.expects(:exec).with("SET TRANSACTION READ ONLY").once+ DB.expects(:exec).with("SET LOCAL statement_timeout = 10000").once+ DB.expects(:query_hash).once.returns([query_row])++ expect(result).to run_successfully+ end++ it "includes newly persisted traffic in a later call" do+ before_pageviews = described_class.call(params:).traffic.dig(:summary, "pageviews")+ Fabricate(+ :browser_pageview_event,+ url: "/new-pageview",+ ip_address: "198.51.100.1",+ session_id: "new-pageview",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ user_agent: "ExampleBrowser/1.0",+ created_at: Time.zone.local(2026, 5, 11, 10),+ )+ after_pageviews = described_class.call(params:).traffic.dig(:summary, "pageviews")++ expect([before_pageviews, after_pageviews]).to eq([11, 12])+ end++ it "does not promote a continuing session to an entry within the selected range" do+ Fabricate(+ :browser_pageview_event,+ url: "/outside-range-entry",+ normalized_referrer: "external.example/path",+ session_id: "continuing-session",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: Time.zone.local(2026, 4, 30, 23, 59),+ )+ Fabricate(+ :browser_pageview_event,+ url: "/inside-range-continuation",+ normalized_referrer: "test.localhost/internal",+ session_id: "continuing-session",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: Time.zone.local(2026, 5, 10, 12),+ )++ traffic = result.traffic++ expect(+ [+ traffic.dig(:summary, "pageviews"),+ traffic.dig(:dimensions, "entry_urls").sum { |row| row[:pageviews] },+ traffic.dig(:dimensions, "referrers"),+ ],+ ).to eq([12, 11, [{ value: "", label: "Direct / unknown", pageviews: 11 }]])+ end++ it "groups session entries by normalized referrer" do+ Fabricate(+ :browser_pageview_event,+ normalized_referrer: "external.example?article=traffic",+ session_id: "external-referrer-query",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: Time.zone.local(2026, 5, 10, 12),+ )+ Fabricate(+ :browser_pageview_event,+ normalized_referrer: "test.localhost?view=latest",+ session_id: "local-referrer-query",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: Time.zone.local(2026, 5, 10, 13),+ )+ expect(result.traffic.dig(:dimensions, "referrers")).to eq(+ [+ { value: "", label: "Direct / unknown", pageviews: 11 },+ {+ value: "external.example?article=traffic",+ label: "external.example?article=traffic",+ pageviews: 1,+ },+ {+ value: "test.localhost?view=latest",+ label: "test.localhost?view=latest",+ pageviews: 1,+ },+ ],+ )+ end++ it "does not promote a capped session continuation to an entry" do+ SiteSetting.stubs(:admin_site_traffic_event_cap).returns(1)+ Fabricate(+ :browser_pageview_event,+ url: "/capped-session-entry",+ normalized_referrer: "external.example/path",+ session_id: "capped-session",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: Time.zone.local(2026, 5, 11, 10),+ )+ Fabricate(+ :browser_pageview_event,+ url: "/capped-session-continuation",+ normalized_referrer: "test.localhost/internal",+ session_id: "capped-session",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: Time.zone.local(2026, 5, 11, 11),+ )++ traffic = result.traffic++ expect(+ [+ traffic.dig(:summary, "pageviews"),+ traffic.dig(:dimensions, "entry_urls"),+ traffic.dig(:dimensions, "referrers"),+ traffic.dig(:partial_data, :reason),+ ],+ ).to eq([1, [], [], "pageview_limit"])+ end++ it "uses the earliest event id to break a session entry timestamp tie" do+ created_at = Time.zone.local(2026, 5, 11, 10)+ Fabricate(+ :browser_pageview_event,+ url: "/first-at-timestamp",+ session_id: "same-timestamp",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at:,+ )+ Fabricate(+ :browser_pageview_event,+ url: "/second-at-timestamp",+ session_id: "same-timestamp",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at:,+ )++ entry_urls = result.traffic.dig(:dimensions, "entry_urls")++ expect(entry_urls.select { |row| row[:value].end_with?("-at-timestamp") }).to eq(+ [{ value: "/first-at-timestamp", label: "/first-at-timestamp", pageviews: 1 }],+ )+ end+ end+ end+enddiff --git a/spec/system/admin_dashboard_redesign/site_traffic_explorer_spec.rb b/spec/system/admin_dashboard_redesign/site_traffic_explorer_spec.rbnew file mode 100644index 00000000000..3cdd51c0819--- /dev/null+++ b/spec/system/admin_dashboard_redesign/site_traffic_explorer_spec.rb@@ -0,0 +1,439 @@+# frozen_string_literal: true++RSpec.describe "Admin Dashboard Redesign | Site Traffic Explorer" do+ fab!(:admin)++ let(:traffic) { PageObjects::Pages::AdminSiteTrafficExplorer.new }++ before do+ SiteSetting.dashboard_improvements = true+ SiteSetting.improved_crawler_detection = true+ SiteSetting.persist_browser_pageview_events = true+ SiteSetting.use_legacy_pageviews = false+ BrowserPageviewEvent.stubs(:beacon_cutover_date).returns(Date.new(2026, 1, 1))+ Discourse.stubs(:current_hostname).returns("test.localhost")+ DiscourseIpInfo+ .stubs(:get)+ .with { |_ip, **options| options[:resolve_hostname] != false }+ .returns({})+ DiscourseIpInfo+ .stubs(:get)+ .with { |ip, **options| ip.to_s == "192.0.2.1" && options[:resolve_hostname] == false }+ .returns(+ country_code: "US",+ country: "United States",+ asn: 64_496,+ organization: "Example Network",+ )+ DiscourseIpInfo+ .stubs(:get)+ .with { |ip, **options| ip.to_s == "198.51.100.2" && options[:resolve_hostname] == false }+ .returns(+ country_code: "GB",+ country: "United Kingdom",+ asn: 64_497,+ organization: "Example Network",+ )+ DiscourseIpInfo+ .stubs(:get)+ .with { |ip, **options| ip.to_s == "203.0.113.3" && options[:resolve_hostname] == false }+ .returns(+ country_code: "US",+ country: "United States",+ asn: 64_498,+ organization: "Example Network",+ )+ end++ it "lets an admin see anonymous traffic without a crawler series when detection is disabled",+ time: Time.zone.local(2026, 5, 14, 12, 0, 0) do+ SiteSetting.improved_crawler_detection = false+ sign_in(admin)++ Fabricate(+ :browser_pageview_event,+ url: "/crawler-shaped-traffic",+ session_id: "crawler-shaped-session",+ score: CrawlerScorer::BOT_SCORE_THRESHOLD + 1,+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: "2026-05-10 10:00:00",+ )++ traffic.visit(start_date: "2026-05-01", end_date: "2026-05-12")++ expect(traffic).to have_metric(label: "Pageviews", value: "1")+ expect(traffic).to have_series_total(label: "anonymous-human", value: "1")+ expect(traffic).to have_no_series(label: "likely-crawler")+ end++ it "lets an admin see session KPIs formed from available raw pageviews",+ time: Time.zone.local(2026, 5, 14, 12, 0, 0) do+ sign_in(admin)++ Fabricate(+ :browser_pageview_event,+ url: "/engaged-session",+ session_id: "engaged-session",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: "2026-05-14 10:00:00",+ )+ Fabricate(+ :browser_pageview_session_engagement,+ session_id: "engaged-session",+ engaged_seconds: 20,+ )+ Fabricate(+ :browser_pageview_event,+ url: "/active-session",+ session_id: "active-session",+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: "2026-05-14 11:50:00",+ )+ Fabricate(+ :browser_pageview_session_engagement,+ session_id: "active-session",+ engaged_seconds: 0,+ )++ traffic.visit(start_date: "2026-05-14", end_date: "2026-05-14")++ expect(traffic).to have_metric(label: "Pageviews", value: "2")+ expect(traffic).to have_metric(label: "Distinct sessions", value: "2")+ expect(traffic).to have_metric(label: "Bounce rate", value: "50%")+ expect(traffic).to have_metric(label: "Average session duration", value: "10s")+ end++ it "lets an admin investigate traffic with dates and filters",+ time: Time.zone.local(2026, 5, 14, 12, 0, 0) do+ sign_in(admin)++ chrome = "Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36"+ firefox = "Mozilla/5.0 Firefox/126.0"++ events = [+ {+ url: "https://test.localhost/latest/?campaign=secret#section",+ country_code: "US",+ asn: 64_496,+ ip_address: "192.0.2.1",+ user_agent: chrome,+ user_id: admin.id,+ session_id: "logged-in-session",+ normalized_referrer: "search.example/results?q=discourse",+ normalized_referrer_version: BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,+ created_at: "2026-05-10 10:00:00",+ },+ {+ url: "https://test.localhost/top?sort=popular",+ country_code: "US",+ asn: 64_496,+ ip_address: "192.0.2.1",+ user_agent: chrome,+ user_id: admin.id,+ session_id: "logged-in-session",+ normalized_referrer: "test.localhost/latest",+ normalized_referrer_version: BrowserPageviewEventUrlNormalizer::REFERRER_VERSION,+ created_at: "2026-05-10 10:01:00",+ },+ {+ url: "/top",+ country_code: "GB",+ asn: 64_497,+ ip_address: "198.51.100.2",+ user_agent: firefox,+ session_id: "anonymous-session",+ created_at: "2026-05-11 10:00:00",+ },+ {+ url: "/hot",+ country_code: "US",+ asn: 64_498,+ ip_address: "203.0.113.3",+ user_agent: chrome,+ session_id: "crawler-session",+ score: CrawlerScorer::BOT_SCORE_THRESHOLD + 1,+ created_at: "2026-05-12 10:00:00",+ },+ ]+ events.each do |attributes|+ Fabricate(:browser_pageview_event, source: BrowserPageviewEvent::SOURCE_BEACON, **attributes)+ end++ Fabricate(+ :browser_pageview_session_engagement,+ session_id: "logged-in-session",+ engaged_seconds: 60,+ )+ Fabricate(+ :browser_pageview_session_engagement,+ session_id: "anonymous-session",+ engaged_seconds: 0,+ )+ Fabricate(+ :browser_pageview_session_engagement,+ session_id: "crawler-session",+ engaged_seconds: 0,+ )++ traffic.visit(start_date: "2026-05-01", end_date: "2026-05-12")++ expect(traffic).to have_page_title+ expect(traffic).to have_date_range("May 1, 2026 – May 12, 2026")+ expect(traffic).to have_no_filter_pills+ expect(traffic).to have_metric(label: "Pageviews", value: "3")+ expect(traffic).to have_metric(label: "Distinct sessions", value: "3")+ expect(traffic).to have_metric(label: "Logged-in share", value: "67%")+ expect(traffic).to have_metric(label: "Bounce rate", value: "67%")+ expect(traffic).to have_metric(label: "Average session duration", value: "20s")+ expect(traffic).to have_series_total(label: "logged-in-human", value: "2")+ expect(traffic).to have_series_total(label: "anonymous-human", value: "1")+ expect(traffic).to have_series_total(label: "likely-crawler", value: "1")+ expect(traffic).to have_no_partial_data_warning++ traffic.visit(+ start_date: "2026-05-01",+ end_date: "2026-05-12",+ traffic_type: "logged_in,anonymous",+ )+ expect(traffic).to have_filter_pill(dimension: "traffic_type", label: "Logged in")+ expect(traffic).to have_filter_pill(dimension: "traffic_type", label: "Anonymous")+ expect(traffic).to have_metric(label: "Pageviews", value: "3")+ expect(traffic).to have_metric(label: "Distinct sessions", value: "2")+ expect(traffic).to have_series_total(label: "logged-in-human", value: "2")+ expect(traffic).to have_series_total(label: "anonymous-human", value: "1")+ expect(traffic).to have_series_total(label: "likely-crawler", value: "0")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?end_date=2026-05-12&range=custom&start_date=2026-05-01&traffic_type=logged_in%2Canonymous",+ )++ traffic.go_back+ expect(traffic).to have_no_filter_pills+ expect(traffic).to have_metric(label: "Pageviews", value: "3")++ traffic.go_forward+ expect(traffic).to have_filter_pill(dimension: "traffic_type", label: "Logged in")+ expect(traffic).to have_filter_pill(dimension: "traffic_type", label: "Anonymous")+ expect(traffic).to have_metric(label: "Distinct sessions", value: "2")++ traffic.remove_filter("traffic_type", label: "Logged in")+ expect(traffic).to have_filter_pill(dimension: "traffic_type", label: "Anonymous")+ expect(traffic).to have_metric(label: "Pageviews", value: "1")++ traffic.remove_filter("traffic_type", label: "Anonymous")+ expect(traffic).to have_no_filter_pills+ expect(traffic).to have_metric(label: "Pageviews", value: "3")++ traffic.visit(start_date: "2026-05-01", end_date: "2026-05-12", traffic_type: "likely_crawler")+ expect(traffic).to have_filter_pill(dimension: "traffic_type", label: "Likely crawlers")+ expect(traffic).to have_metric(label: "Pageviews", value: "1")+ expect(traffic).to have_metric(label: "Distinct sessions", value: "1")+ expect(traffic).to have_series_total(label: "logged-in-human", value: "0")+ expect(traffic).to have_series_total(label: "anonymous-human", value: "0")+ expect(traffic).to have_series_total(label: "likely-crawler", value: "1")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?end_date=2026-05-12&range=custom&start_date=2026-05-01&traffic_type=likely_crawler",+ )+ traffic.remove_filter("traffic_type", label: "Likely crawlers")+ expect(traffic).to have_metric(label: "Pageviews", value: "3")++ expect(traffic).to have_card_tabs(card: "acquisition", tabs: %w[Referrers Countries Networks])+ expect(traffic).to have_card_tabs(card: "pages", tabs: ["Top URLs", "Entry URLs"])+ expect(traffic).to have_card_tabs(card: "visitors", tabs: ["Browsers", "IP addresses"])+ expect(traffic).to have_row(card: "acquisition", label: "Direct / unknown", count: "2")+ expect(traffic).to have_row(+ card: "acquisition",+ label: "search.example/results?q=discourse",+ count: "1",+ )+ expect(traffic).to have_row(card: "pages", label: "/top", count: "2")+ expect(traffic).to have_url_link(card: "pages", label: "/top", href: "/top")++ traffic.select_tab(card: "pages", tab: "Entry URLs")+ expect(traffic).to have_row(card: "pages", label: "/latest", count: "1")+ expect(traffic).to have_url_link(card: "pages", label: "/latest", href: "/latest")+ traffic.filter_row(card: "pages", label: "/latest")+ expect(traffic).to have_filter_pill(dimension: "entry_url", label: "/latest")+ expect(traffic).to have_metric(label: "Pageviews", value: "1")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?end_date=2026-05-12&entry_url=%2Flatest&range=custom&start_date=2026-05-01",+ )+ traffic.remove_filter("entry_url")++ traffic.filter_row(card: "acquisition", label: "Direct / unknown")+ expect(traffic).to have_filter_pill(dimension: "referrer", label: "Direct / unknown")+ expect(traffic).to have_metric(label: "Pageviews", value: "1")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?end_date=2026-05-12&range=custom&referrer=&start_date=2026-05-01",+ )+ traffic.remove_filter("referrer")++ traffic.filter_row(card: "acquisition", label: "search.example/results?q=discourse")+ expect(traffic).to have_filter_pill(+ dimension: "referrer",+ label: "search.example/results?q=discourse",+ )+ expect(traffic).to have_metric(label: "Pageviews", value: "1")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?end_date=2026-05-12&range=custom&referrer=search.example%2Fresults%3Fq%3Ddiscourse&start_date=2026-05-01",+ )+ traffic.remove_filter("referrer")++ traffic.select_tab(card: "acquisition", tab: "Networks")+ traffic.filter_row(card: "acquisition", label: "Example Network (AS64496)")+ expect(traffic).to have_filter_pill(dimension: "network", label: "Example Network (AS64496)")+ expect(traffic).to have_metric(label: "Pageviews", value: "2")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?end_date=2026-05-12&network=AS64496&range=custom&start_date=2026-05-01",+ )+ traffic.remove_filter("network")++ traffic.select_tab(card: "visitors", tab: "Browsers")+ traffic.filter_row(card: "visitors", label: "Chrome")+ expect(traffic).to have_filter_pill(dimension: "browser", label: "Chrome")+ expect(traffic).to have_metric(label: "Pageviews", value: "2")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?browser=chrome&end_date=2026-05-12&range=custom&start_date=2026-05-01",+ )+ traffic.remove_filter("browser")++ traffic.select_tab(card: "acquisition", tab: "Countries")+ traffic.filter_row(card: "acquisition", label: "United States")+ expect(traffic).to have_filter_pill(dimension: "country", label: "United States")+ expect(traffic).to have_metric(label: "Pageviews", value: "2")+ expect(traffic).to have_metric(label: "Logged-in share", value: "100%")+ expect(traffic).to have_metric(label: "Distinct sessions", value: "2")+ expect(traffic).to have_metric(label: "Bounce rate", value: "50%")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?country=US&end_date=2026-05-12&range=custom&start_date=2026-05-01",+ )++ traffic.remove_filter("country")+ expect(traffic).to have_metric(label: "Pageviews", value: "3")+ traffic.select_tab(card: "acquisition", tab: "Countries")+ traffic.filter_row(card: "acquisition", label: "United Kingdom")+ expect(traffic).to have_filter_pill(dimension: "country", label: "United Kingdom")+ expect(traffic).to have_metric(label: "Pageviews", value: "1")++ traffic.remove_filter("country")+ expect(traffic).to have_metric(label: "Pageviews", value: "3")+ traffic.select_tab(card: "acquisition", tab: "Countries")+ traffic.filter_row(card: "acquisition", label: "United States")+ expect(traffic).to have_filter_pill(dimension: "country", label: "United States")++ traffic.select_tab(card: "pages", tab: "Top URLs")+ traffic.filter_row(card: "pages", label: "/top")+ expect(traffic).to have_filter_pill(dimension: "country", label: "United States")+ expect(traffic).to have_filter_pill(dimension: "top_url", label: "/top")+ expect(traffic).to have_metric(label: "Pageviews", value: "1")+ expect(traffic).to have_metric(label: "Distinct sessions", value: "1")+ expect(traffic).to have_metric(label: "Bounce rate", value: "0%")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?country=US&end_date=2026-05-12&range=custom&start_date=2026-05-01&top_url=%2Ftop",+ )++ traffic.select_tab(card: "visitors", tab: "IP addresses")+ traffic.filter_row(card: "visitors", label: "192.0.2.1")+ expect(traffic).to have_filter_pill(dimension: "ip", label: "192.0.2.1")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?country=US&end_date=2026-05-12&ip=192.0.2.1&range=custom&start_date=2026-05-01&top_url=%2Ftop",+ )++ page.refresh++ expect(traffic).to have_filter_pill(dimension: "country", label: "United States")+ expect(traffic).to have_filter_pill(dimension: "top_url", label: "/top")+ expect(traffic).to have_filter_pill(dimension: "ip", label: "192.0.2.1")+ expect(traffic).to have_metric(label: "Pageviews", value: "1")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?country=US&end_date=2026-05-12&ip=192.0.2.1&range=custom&start_date=2026-05-01&top_url=%2Ftop",+ )++ traffic.remove_filter("country")+ traffic.remove_filter("top_url")+ traffic.remove_filter("ip")++ expect(traffic).to have_no_filter_pills+ expect(traffic).to have_metric(label: "Pageviews", value: "3")++ traffic.select_date_preset("Last 7 days")++ expect(traffic).to have_date_range("Last 7 days")+ expect(page).to have_current_path("/admin/dashboard/site-traffic-explorer?range=last_7_days")++ traffic.select_custom_date_range(start_date: "2026-05-01", end_date: "2026-05-12")++ expect(traffic).to have_date_range("May 1, 2026 – May 12, 2026")+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?end_date=2026-05-12&range=custom&start_date=2026-05-01",+ )+ end++ it "lets an admin filter additional results from an expanded table",+ time: Time.zone.local(2026, 5, 14, 12, 0, 0) do+ sign_in(admin)++ additional_path = nil+ 9.times do |index|+ topic = Fabricate(:topic, title: "Traffic topic #{index}")+ path = "/t/#{topic.slug}/#{topic.id}"+ additional_path = path if index == 8+ event_count = 9 - index+ event_count.times do |event_index|+ Fabricate(+ :browser_pageview_event,+ url: path,+ topic_id: topic.id,+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: Time.zone.local(2026, 5, 10, 10, index, event_index),+ )+ end+ end++ traffic.visit(start_date: "2026-05-01", end_date: "2026-05-12")++ traffic.expand("pages")++ expect(traffic).to have_expanded_table(title: "Top URLs", column: "URL")++ traffic.filter_expanded_row(label: additional_path)++ expect(traffic).to have_filter_pill(dimension: "top_url", label: additional_path)+ expect(traffic).to have_no_expanded_table+ expect(traffic).to have_focused_filter_pill(dimension: "top_url")+ end++ it "warns an admin when the selected range has incomplete traffic data",+ time: Time.zone.local(2026, 5, 14, 12, 0, 0) do+ sign_in(admin)+ SiteSetting.stubs(:admin_site_traffic_event_cap).returns(2)++ [+ ["/first-retained", "2026-02-15 09:00:00"],+ ["/middle-retained", "2026-05-10 10:00:00"],+ ["/latest-retained", "2026-05-12 10:00:00"],+ ].each do |url, created_at|+ Fabricate(+ :browser_pageview_event,+ url: url,+ session_id: url.delete_prefix("/"),+ source: BrowserPageviewEvent::SOURCE_BEACON,+ created_at: created_at,+ )+ end++ traffic.visit(start_date: "2026-01-01", end_date: "2026-05-12")++ expect(traffic).to have_partial_data_warning(+ reason:+ "Detailed pageview activity before Feb 14, 2026 is no longer available, so these results cover only part of the selected date range. They are also limited to the most recent 2 pageviews. Choose a shorter date range within the available dates to include all available pageview activity.",+ )+ end++ it "tells an admin when no pageviews match" do+ sign_in(admin)++ traffic.visit(start_date: "2026-05-01", end_date: "2026-05-12")++ expect(traffic).to have_empty_state+ end+enddiff --git a/spec/system/admin_dashboard_redesign/site_traffic_section_spec.rb b/spec/system/admin_dashboard_redesign/site_traffic_section_spec.rbindex 0dac9b64b26..6f0d0d3f8bd 100644--- a/spec/system/admin_dashboard_redesign/site_traffic_section_spec.rb+++ b/spec/system/admin_dashboard_redesign/site_traffic_section_spec.rb@@ -2,6 +2,7 @@describe "Admin Dashboard Redesign | Site Traffic section" dofab!(:current_user, :admin)+ fab!(:moderator)let(:dashboard) { PageObjects::Pages::AdminDashboard.new }@@ -136,8 +137,8 @@ describe "Admin Dashboard Redesign | Site Traffic section" doexpect(traffic).to have_chartend- it "takes staff to the full site traffic report scoped to the same period when they click See details",- time: Time.zone.local(2026, 5, 14, 12, 0, 0) do+ it "takes an admin to the traffic explorer with the selected period when they click See details" do+ SiteSetting.enable_site_traffic_explorer = trueFabricate(:logged_in_browser_application_request, date: "2026-05-05", count: 10)dashboard.visit_with_query(range: "custom", start_date: "2026-05-01", end_date: "2026-05-12")@@ -148,6 +149,29 @@ describe "Admin Dashboard Redesign | Site Traffic section" dotraffic.click_see_details+ expect(page).to have_current_path(+ "/admin/dashboard/site-traffic-explorer?end_date=2026-05-12&range=custom&start_date=2026-05-01",+ )+ end++ it "links to the aggregate traffic report by default" do+ Fabricate(:logged_in_browser_application_request, date: "2026-05-05", count: 10)++ dashboard.visit_with_query(range: "custom", start_date: "2026-05-01", end_date: "2026-05-12")+ dashboard.site_traffic.click_see_details++ expect(page).to have_current_path(+ "/admin/reports/site_traffic?end_date=2026-05-12&start_date=2026-05-01",+ )+ end++ it "keeps the aggregate traffic report available to moderators" do+ SiteSetting.enable_site_traffic_explorer = true+ sign_in(moderator)++ dashboard.visit_with_query(range: "custom", start_date: "2026-05-01", end_date: "2026-05-12")+ dashboard.site_traffic.click_see_details+expect(page).to have_current_path("/admin/reports/site_traffic?end_date=2026-05-12&start_date=2026-05-01",)diff --git a/spec/system/page_objects/components/admin_dashboard_site_traffic.rb b/spec/system/page_objects/components/admin_dashboard_site_traffic.rbindex 2cde2de3e07..e8f398710c4 100644--- a/spec/system/page_objects/components/admin_dashboard_site_traffic.rb+++ b/spec/system/page_objects/components/admin_dashboard_site_traffic.rb@@ -40,6 +40,10 @@ module PageObjectshas_css?("a.db-traffic__see-details")end+ def has_no_see_details_link?+ has_no_css?("a.db-traffic__see-details")+ end+def click_see_detailsfind("a.db-traffic__see-details").clickenddiff --git a/spec/system/page_objects/pages/admin_site_traffic_explorer.rb b/spec/system/page_objects/pages/admin_site_traffic_explorer.rbnew file mode 100644index 00000000000..434a79e840d--- /dev/null+++ b/spec/system/page_objects/pages/admin_site_traffic_explorer.rb@@ -0,0 +1,188 @@+# frozen_string_literal: true++module PageObjects+ module Pages+ class AdminSiteTrafficExplorer < PageObjects::Pages::Base+ PATH = "/admin/dashboard/site-traffic-explorer"+ METRIC_KEYS = {+ "Pageviews" => "pageviews",+ "Distinct sessions" => "distinct_sessions",+ "Logged-in share" => "logged_in_share",+ "Bounce rate" => "bounce_rate",+ "Average session duration" => "average_session_duration",+ }.freeze+ FILTER_LABELS = {+ "traffic_type" => "Traffic type",+ "top_url" => "Top URL",+ "entry_url" => "Entry URL",+ "referrer" => "Referrer",+ "country" => "Country",+ "network" => "Network",+ "browser" => "Browser",+ "ip" => "IP address",+ }.freeze++ def visit(start_date:, end_date:, traffic_type: nil)+ path = "#{PATH}?end_date=#{end_date}&range=custom&start_date=#{start_date}"+ path += "&traffic_type=#{traffic_type}" if traffic_type+ page.visit(path)+ self+ end++ def go_back+ page.go_back+ self+ end++ def go_forward+ page.go_forward+ self+ end++ def has_page_title?+ has_css?("h1", exact_text: "Site Traffic Explorer")+ end++ def has_date_range?(text)+ has_button?(text)+ end++ def select_date_preset(label)+ open_date_range.select_preset(label)+ self+ end++ def select_custom_date_range(start_date:, end_date:)+ picker = open_date_range+ picker.pick_day(start_date)+ picker.pick_day(end_date)+ picker.apply+ self+ end++ def has_metric?(label:, value:)+ selector = "[data-test-site-traffic-metric='#{METRIC_KEYS.fetch(label)}']"+ has_css?(selector, text: label) && has_css?(selector, text: value)+ end++ def has_series_total?(label:, value:)+ has_css?("[data-test-traffic-series='#{label}']", text: value, visible: :all)+ end++ def has_no_series?(label:)+ has_no_css?("[data-test-traffic-series='#{label}']", visible: :all)+ end++ def has_partial_data_warning?(reason:)+ selector = "[data-test-site-traffic-partial-warning]"++ has_css?(selector, exact_text: reason) && has_no_css?("#{selector} button")+ end++ def has_no_partial_data_warning?+ has_no_css?("[data-test-site-traffic-partial-warning]")+ end++ def has_card_tabs?(card:, tabs:)+ tabs.all? do |tab|+ has_css?("[data-test-site-traffic-card='#{card}'] [role='tab']", exact_text: tab)+ end+ end++ def select_tab(card:, tab:)+ within("[data-test-site-traffic-card='#{card}']") do+ find("[role='tab']", exact_text: tab).click+ end+ self+ end++ def has_row?(card:, label:, count:)+ within("[data-test-site-traffic-card='#{card}']") do+ has_css?(+ "[data-test-site-traffic-row]",+ text: /#{Regexp.escape(label)}.*#{Regexp.escape(count)}/m,+ )+ 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)+ end+ end++ def filter_row(card:, label:)+ within("[data-test-site-traffic-card='#{card}']") do+ find("button[aria-label^='Filter by #{label},']").click+ end+ self+ end++ def has_filter_pill?(dimension:, label:)+ selector = "[data-test-site-traffic-filter-pill='#{dimension}']"+ remove_label =+ if dimension == "traffic_type"+ "Remove #{label} traffic filter"+ else+ "Remove #{FILTER_LABELS.fetch(dimension)} filter"+ end++ has_css?(selector, text: "#{FILTER_LABELS.fetch(dimension)} is #{label}", count: 1) &&+ has_css?("#{selector} button[aria-label='#{remove_label}']")+ end++ def has_no_filter_pills?+ has_no_css?("[data-test-site-traffic-filter-pill]")+ end++ def remove_filter(name, label: nil)+ remove_label =+ if name == "traffic_type"+ "Remove #{label} traffic filter"+ else+ "Remove #{FILTER_LABELS.fetch(name)} filter"+ end+ find("button[aria-label='#{remove_label}']").click+ self+ end++ def expand(card)+ within("[data-test-site-traffic-card='#{card}']") { find_button("View more").click }+ self+ end++ def has_expanded_table?(title:, column:)+ selector = ".site-traffic-breakdown-modal[role='dialog']"+ has_css?(selector, text: title) &&+ has_css?("#{selector} .d-table__header-cell", exact_text: column) &&+ has_css?("#{selector} .d-table__body .d-table__row")+ end++ def filter_expanded_row(label:)+ within(".site-traffic-breakdown-modal[role='dialog']") do+ within("tr", text: label) { find("button[aria-label^='Filter by #{label},']").click }+ end+ self+ end++ def has_no_expanded_table?+ has_no_css?(".site-traffic-breakdown-modal[role='dialog']")+ end++ def has_focused_filter_pill?(dimension:)+ has_css?("[data-test-site-traffic-filter-pill='#{dimension}'] button:focus")+ end++ def has_empty_state?+ has_css?("[data-test-site-traffic-empty]", exact_text: "No matching pageviews")+ end++ private++ def open_date_range+ find(".db-date-range__trigger").click+ PageObjects::Components::AdminDashboardDateRangePicker.new.tap(&:open?)+ end+ end+ end+end