Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
add tests for time_on_page edge cases (#3494)
ruslandoga2917cfaf6528
diff --git a/test/plausible_web/controllers/api/stats_controller/pages_test.exs b/test/plausible_web/controllers/api/stats_controller/pages_test.exsindex 903b0e432f..fc28a29970 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: sitediff --git a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exsindex 48a7891cf2..4a911d6446 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 doassert %{"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"))+ endenddescribe "GET /api/stats/top-stats - with imported data" do