0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-08 17:53:55 +08:00
discourse/lib/tasks
Alan Guo Xiang Tan 437ab337d2
FEATURE: Add top countries and top referrers cards to the admin dashboard (#40215)
This commit adds two new cards to the redesigned admin dashboard's Site
Traffic section: top countries and top referrers, both sourced from
`browser_pageview_events`.

Key technical decisions:

1. Gate the cards on the `persist_browser_pageview_events` site setting.
The cards have no data source unless browser pageview events are being
persisted, so they are omitted from the dashboard.

2. Normalize referrers at write time. A new `normalized_referrer` column
on `browser_pageview_events` is populated by
`BrowserPageviewReferrerInspector`, which strips scheme, `www.`, port,
fragment, trailing slashes, and common tracking query params. Doing this
at insert time avoids per-row string operations at query time.

3. Count browser pageviews by country and by referrer in two new report
concerns. `Reports::TopCountriesByBrowserPageviews` groups by
`country_code` and `Reports::TopReferrersByBrowserPageviews` groups by
`normalized_referrer`. Both compute share of total browser pageviews and
rank the top 5 in SQL. The country report drops MaxMind reserved codes
(unknown, anonymous proxy, satellite). The referrer report drops
same-host referrals. Both also exclude anonymous browser pageviews
(`user_id IS NULL`) when the `login_required` site setting is enabled,
since only logged-in browser pageviews are meaningful on a closed forum.

4. Fetch each report through the existing dashboard service.
`AdminDashboardSiteTraffic#build` returns one entry per card with a `{
rows:, error: }` shape, e.g.:

   ```ruby
   {
     top_countries: {
       rows: [
         { country_code: "US", count: 142, percent: 35 },
         { country_code: "GB", count: 89, percent: 22 }
       ],
       error: nil
     },
     top_referrers: {
       rows: [
{ normalized_referrer: "news.ycombinator.com/item?id=1", count: 47,
percent: 12 },
{ normalized_referrer: "reddit.com/r/discourse", count: 31, percent: 8 }
       ],
       error: nil
     }
   }
   ```

On report failure, `rows: []` and `error: :timeout` (or another symbol).
This lets the UI render rows, error, or empty state independently.
Healthy responses are cached via `Report.find_cached`.
`SiteSetting.login_required` and `Discourse.current_hostname` flow into
`opts[:filters]` so toggling either invalidates the cache. Timeouts skip
the cache so the next request retries.

5. Use `Intl.DisplayNames` for country names instead of locale files.
`Intl.DisplayNames` is a built-in browser API that returns a localized
country name for an ISO 3166-1 alpha-2 code, avoiding ~250 translation
strings per locale.
2026-05-22 12:59:16 +08:00
..
add_topic_to_quotes.rake DEV: Apply syntax_tree formatting to lib/* 2023-01-09 12:10:19 +00:00
admin.rake DEV: Enable Style/RedundantParentheses rubocop rule (#40095) 2026-05-19 15:48:09 +02:00
annotate.rake DEV: Load all bundled plugins when running annotate:clean (#39978) 2026-05-13 17:19:15 +01:00
annotate_rb.rake DEV: Upgrade Rails to version 8.0.2 2025-07-22 09:59:44 +02:00
api.rake FEATURE: Hash API keys in the database (#8438) 2019-12-12 11:45:00 +00:00
api_docs.rake FIX: only load the api-docs rake task definition when required gems are available (#35432) 2025-10-16 12:36:53 +08:00
assets.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
autospec.rake DEV: Apply syntax_tree formatting to lib/* 2023-01-09 12:10:19 +00:00
avatars.rake DEV: Update to lastest rubocop-discourse 2024-05-27 18:06:14 +02:00
backfill.thor DEV: Format .thor files (#23059) 2023-08-10 13:59:13 +02:00
categories.rake FIX: Update category tag stats with new or deleted (#21531) 2023-05-18 12:46:44 +03:00
cdn.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
compatibility.rake DEV: Introduce rake task to validate discourse-compatibility file (#26158) 2024-03-13 13:57:41 +00:00
db.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
db_structure.rake DEV: Check & resolve sequence issues during db:check_structure_dump (#40118) 2026-05-18 15:23:41 +01:00
destroy.rake DEV: Add rake task to bulk delete posts (#31642) 2025-03-05 09:31:23 +08:00
dev.rake DEV: Enable Style/RedundantParentheses rubocop rule (#40095) 2026-05-19 15:48:09 +02:00
docker.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
documentation.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
emails.rake DEV: Enable Style/RedundantBegin rubocop rule (#40096) 2026-05-19 18:44:54 +02:00
export.rake DEV: Apply syntax_tree formatting to lib/* 2023-01-09 12:10:19 +00:00
groups.rake DEV: Apply syntax_tree formatting to lib/* 2023-01-09 12:10:19 +00:00
hashtags.rake DEV: Capture output in hashtags spec (#20773) 2023-03-23 11:47:14 +10:00
i18n.rake DEV: Apply syntax_tree formatting to lib/* 2023-01-09 12:10:19 +00:00
images.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
import.rake DEV: Enable Style/RedundantBegin rubocop rule (#40096) 2026-05-19 18:44:54 +02:00
incoming_emails.rake DEV: Apply syntax_tree formatting to lib/* 2023-01-09 12:10:19 +00:00
javascript.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
list_email_templates_strings.rake FIX: Add new/missing email templates to the email templates editor (#28075) 2024-07-30 00:27:41 +03:00
log.rake FIX: whoops, linting 2020-09-03 12:10:11 +01:00
maxminddb.rake DEV: remove unused maxmind_thread variable (#37436) 2026-02-02 18:50:04 +11:00
migrate_advanced_search_banner_to_welcome_banner.rake FIX: improves search banner migration scripts (#36192) 2025-11-24 17:37:53 +08:00
plugin.rake DEV: Silence expected error/debug output in core specs (#39247) 2026-04-14 11:04:10 +02:00
populate.rake FEATURE: Add top countries and top referrers cards to the admin dashboard (#40215) 2026-05-22 12:59:16 +08:00
populate.thor DEV: Fix assigned but unused variable Prism warnings (#39436) 2026-04-22 12:42:14 +02:00
posts.rake DEV: Enable Style/RedundantBegin rubocop rule (#40096) 2026-05-19 18:44:54 +02:00
profile.rake DEV: Fix benchmark script 2024-11-20 14:36:44 +01:00
qunit.rake DEV: Refactor all js testing into bin/qunit (#35785) 2025-11-15 07:29:18 +11:00
redis.rake DEV: Apply syntax_tree formatting to lib/* 2023-01-09 12:10:19 +00:00
release.rake DEV: Fixup release task logic (#39612) 2026-05-13 10:59:41 +01:00
release_note.rake DEV: Fix already initialized constant warnings (#35854) 2025-11-06 01:02:10 +01:00
reviewables.rake DEV: fix a large amount of typos (#37428) 2026-02-02 16:31:58 +11:00
revisions.rake DEV: Apply syntax_tree formatting to lib/* 2023-01-09 12:10:19 +00:00
rspec.rake DEV: Update rubocop-discourse to 3.13 and autofix issues (#35073) 2025-10-06 16:11:01 +02:00
s3.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
scheduler.rake FEATURE: call hub API to update Discourse discover enrollment. (#25634) 2024-02-23 11:42:28 +05:30
search.rake DEV: Apply syntax_tree formatting to lib/* 2023-01-09 12:10:19 +00:00
site.rake FEATURE: extract text from document uploads for LLM prompts (#39634) 2026-05-05 08:16:23 +10:00
site_settings.rake FIX: type: objects uploads should be stored as IDs (#40178) 2026-05-21 13:45:27 -03:00
smoke_test.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
svg_icons.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
svg_sprites.rake DEV: Store theme sprites in the DB (#20501) 2023-03-14 13:11:45 -05:00
tags.rake DEV: Apply syntax_tree formatting to lib/* 2023-01-09 12:10:19 +00:00
themes.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
topic_localizations.rake DEV: Add rake task to backfill localization excerpts which were empty (#36901) 2025-12-30 17:04:36 +08:00
topics.rake DEV: Hand-pick Rails/WhereNot autofixes (#35117) 2025-10-03 13:29:22 +02:00
turbo.rake DEV: Fix bin/turbo_rspec runtime recording (#20407) 2023-02-23 07:47:11 +10:00
typepad.thor DEV: Enable Style/RedundantParentheses rubocop rule (#40095) 2026-05-19 15:48:09 +02:00
uploads.rake DEV: Enable Rails/FilePath rubocop rule (#40097) 2026-05-19 19:07:54 +02:00
users.rake DEV: Enable Style/RedundantBegin rubocop rule (#40096) 2026-05-19 18:44:54 +02:00