diff --git a/test/plausible_web/controllers/api/stats_controller/pages_test.exs b/test/plausible_web/controllers/api/stats_controller/pages_test.exs index 903b0e432..fc28a2997 100644 --- a/test/plausible_web/controllers/api/stats_controller/pages_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/pages_test.exs @@ -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 diff --git a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs index 48a7891cf..4a911d644 100644 --- a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs @@ -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