add tests for time_on_page edge cases (#3494)

pull/3498/head
ruslandoga 3 years ago committed by GitHub
parent f977351ae2
commit 2917cfaf65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -772,6 +772,35 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do
]
end
test "doesn't calculate time on page with only single page visits", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, pathname: "/", user_id: @user_id, timestamp: ~N[2021-01-01 00:00:00]),
build(:pageview, pathname: "/", user_id: @user_id, timestamp: ~N[2021-01-01 00:10:00])
])
assert [%{"name" => "/", "time_on_page" => nil}] =
conn
|> get("/api/stats/#{site.domain}/pages?period=day&date=2021-01-01&detailed=true")
|> json_response(200)
end
test "ignores page refresh when calculating time on page", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, user_id: @user_id, timestamp: ~N[2021-01-01 00:00:00], pathname: "/"),
build(:pageview, user_id: @user_id, timestamp: ~N[2021-01-01 00:01:00], pathname: "/"),
build(:pageview, user_id: @user_id, timestamp: ~N[2021-01-01 00:02:00], pathname: "/"),
build(:pageview, user_id: @user_id, timestamp: ~N[2021-01-01 00:03:00], pathname: "/exit")
])
assert [
%{"name" => "/", "time_on_page" => _three_minutes = 180.0},
%{"name" => "/exit", "time_on_page" => nil}
] =
conn
|> get("/api/stats/#{site.domain}/pages?period=day&date=2021-01-01&detailed=true")
|> json_response(200)
end
test "calculates bounce rate and time on page for pages with imported data", %{
conn: conn,
site: site

@ -296,6 +296,42 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
assert %{"name" => "Time on page", "value" => 600} in res["top_stats"]
end
test "doesn't calculate time on page with only single page visits", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, pathname: "/", user_id: @user_id, timestamp: ~N[2021-01-01 00:00:00]),
build(:pageview, pathname: "/", user_id: @user_id, timestamp: ~N[2021-01-01 00:10:00])
])
filters = Jason.encode!(%{page: "/"})
path = "/api/stats/#{site.domain}/top-stats?period=day&date=2021-01-01&filters=#{filters}"
assert %{"name" => "Time on page", "value" => 0} ==
conn
|> get(path)
|> json_response(200)
|> Map.fetch!("top_stats")
|> Enum.find(&(&1["name"] == "Time on page"))
end
test "ignores page refresh when calculating time on page", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, user_id: @user_id, timestamp: ~N[2021-01-01 00:00:00], pathname: "/"),
build(:pageview, user_id: @user_id, timestamp: ~N[2021-01-01 00:01:00], pathname: "/"),
build(:pageview, user_id: @user_id, timestamp: ~N[2021-01-01 00:02:00], pathname: "/"),
build(:pageview, user_id: @user_id, timestamp: ~N[2021-01-01 00:03:00], pathname: "/exit")
])
filters = Jason.encode!(%{page: "/"})
path = "/api/stats/#{site.domain}/top-stats?period=day&date=2021-01-01&filters=#{filters}"
assert %{"name" => "Time on page", "value" => _three_minutes = 180} ==
conn
|> get(path)
|> json_response(200)
|> Map.fetch!("top_stats")
|> Enum.find(&(&1["name"] == "Time on page"))
end
end
describe "GET /api/stats/top-stats - with imported data" do

Loading…
Cancel
Save