mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 00:23:40 +08:00
2f2da72747
Our 'page_view_crawler' / 'page_view_anon' metrics are based purely on the User Agent sent by clients. This means that 'badly behaved' bots which are imitating real user agents are counted towards 'anon' page views. This commit introduces a new method of tracking visitors. When an initial HTML request is made, we assume it is a 'non-browser' request (i.e. a bot). Then, once the JS application has booted, we notify the server to count it as a 'browser' request. This reliance on a JavaScript-capable browser matches up more closely to dedicated analytics systems like Google Analytics. Existing data collection and graphs are unchanged. Data collected via the new technique is available in a new 'experimental' report.
189 lines
4.8 KiB
Ruby
189 lines
4.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
describe "request tracking", type: :system do
|
|
before do
|
|
ApplicationRequest.enable
|
|
CachedCounting.reset
|
|
CachedCounting.enable
|
|
end
|
|
|
|
after do
|
|
CachedCounting.reset
|
|
ApplicationRequest.disable
|
|
CachedCounting.disable
|
|
end
|
|
|
|
it "tracks an anonymous visit correctly" do
|
|
visit "/"
|
|
|
|
try_until_success do
|
|
CachedCounting.flush
|
|
expect(ApplicationRequest.stats).to include(
|
|
"page_view_anon_total" => 1,
|
|
"page_view_anon_browser_total" => 1,
|
|
"page_view_logged_in_total" => 0,
|
|
"page_view_crawler_total" => 0,
|
|
)
|
|
end
|
|
|
|
find(".nav-item_categories a").click
|
|
|
|
try_until_success do
|
|
CachedCounting.flush
|
|
expect(ApplicationRequest.stats).to include(
|
|
"page_view_anon_total" => 2,
|
|
"page_view_anon_browser_total" => 2,
|
|
"page_view_logged_in_total" => 0,
|
|
"page_view_crawler_total" => 0,
|
|
)
|
|
end
|
|
end
|
|
|
|
it "tracks a crawler visit correctly" do
|
|
# Can't change Selenium's user agent... so change site settings to make Discourse detect chrome as a crawler
|
|
SiteSetting.crawler_user_agents += "|chrome"
|
|
|
|
visit "/"
|
|
|
|
try_until_success do
|
|
CachedCounting.flush
|
|
expect(ApplicationRequest.stats).to include(
|
|
"page_view_anon_total" => 0,
|
|
"page_view_anon_browser_total" => 0,
|
|
"page_view_logged_in_total" => 0,
|
|
"page_view_crawler_total" => 1,
|
|
)
|
|
end
|
|
end
|
|
|
|
it "tracks a logged-in session correctly" do
|
|
sign_in Fabricate(:user)
|
|
|
|
visit "/"
|
|
|
|
try_until_success do
|
|
CachedCounting.flush
|
|
expect(ApplicationRequest.stats).to include(
|
|
"page_view_anon_total" => 0,
|
|
"page_view_anon_browser_total" => 0,
|
|
"page_view_logged_in_total" => 1,
|
|
"page_view_crawler_total" => 0,
|
|
"page_view_logged_in_browser_total" => 1,
|
|
)
|
|
end
|
|
|
|
find(".nav-item_categories a").click
|
|
|
|
try_until_success do
|
|
CachedCounting.flush
|
|
expect(ApplicationRequest.stats).to include(
|
|
"page_view_anon_total" => 0,
|
|
"page_view_anon_browser_total" => 0,
|
|
"page_view_logged_in_total" => 2,
|
|
"page_view_crawler_total" => 0,
|
|
"page_view_logged_in_browser_total" => 2,
|
|
)
|
|
end
|
|
end
|
|
|
|
it "tracks normal error pages correctly" do
|
|
SiteSetting.bootstrap_error_pages = false
|
|
|
|
visit "/foobar"
|
|
|
|
try_until_success do
|
|
CachedCounting.flush
|
|
|
|
# Does not count error as a pageview
|
|
expect(ApplicationRequest.stats).to include(
|
|
"http_4xx_total" => 1,
|
|
"page_view_anon_total" => 0,
|
|
"page_view_anon_browser_total" => 0,
|
|
"page_view_logged_in_total" => 0,
|
|
"page_view_crawler_total" => 0,
|
|
)
|
|
end
|
|
|
|
find("#site-logo").click
|
|
|
|
try_until_success do
|
|
CachedCounting.flush
|
|
expect(ApplicationRequest.stats).to include(
|
|
"http_4xx_total" => 1,
|
|
"page_view_anon_total" => 1,
|
|
"page_view_anon_browser_total" => 1,
|
|
"page_view_logged_in_total" => 0,
|
|
"page_view_crawler_total" => 0,
|
|
)
|
|
end
|
|
end
|
|
|
|
it "tracks non-ember pages correctly" do
|
|
visit "/safe-mode"
|
|
|
|
try_until_success do
|
|
CachedCounting.flush
|
|
|
|
# Does not count error as a pageview
|
|
expect(ApplicationRequest.stats).to include(
|
|
"page_view_anon_total" => 1,
|
|
"page_view_anon_browser_total" => 1,
|
|
"page_view_logged_in_total" => 0,
|
|
"page_view_crawler_total" => 0,
|
|
)
|
|
end
|
|
end
|
|
|
|
it "tracks bootstrapped error pages correctly" do
|
|
SiteSetting.bootstrap_error_pages = true
|
|
|
|
visit "/foobar"
|
|
|
|
try_until_success do
|
|
CachedCounting.flush
|
|
|
|
# Does not count error as a pageview
|
|
expect(ApplicationRequest.stats).to include(
|
|
"http_4xx_total" => 1,
|
|
"page_view_anon_total" => 0,
|
|
"page_view_anon_browser_total" => 0,
|
|
"page_view_logged_in_total" => 0,
|
|
"page_view_crawler_total" => 0,
|
|
)
|
|
end
|
|
|
|
find("#site-logo").click
|
|
|
|
try_until_success do
|
|
CachedCounting.flush
|
|
expect(ApplicationRequest.stats).to include(
|
|
"http_4xx_total" => 1,
|
|
"page_view_anon_total" => 1,
|
|
"page_view_anon_browser_total" => 1,
|
|
"page_view_logged_in_total" => 0,
|
|
"page_view_crawler_total" => 0,
|
|
)
|
|
end
|
|
end
|
|
|
|
it "tracks published pages correctly" do
|
|
SiteSetting.enable_page_publishing = true
|
|
page =
|
|
Fabricate(:published_page, public: true, slug: "some-page", topic: Fabricate(:post).topic)
|
|
|
|
visit "/pub/some-page"
|
|
|
|
try_until_success do
|
|
CachedCounting.flush
|
|
|
|
# Does not count error as a pageview
|
|
expect(ApplicationRequest.stats).to include(
|
|
"page_view_anon_total" => 1,
|
|
"page_view_anon_browser_total" => 1,
|
|
"page_view_logged_in_total" => 0,
|
|
"page_view_crawler_total" => 0,
|
|
)
|
|
end
|
|
end
|
|
end
|