Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Fix division by zero in imported queries (#3890)

RobertJoonase8f3946dded2
diff --git a/lib/plausible/stats/compare.ex b/lib/plausible/stats/compare.ex
index 091f56ccf7..aa03c3961c 100644
--- a/lib/plausible/stats/compare.ex
+++ b/lib/plausible/stats/compare.ex
@@ -4,9 +4,7 @@ defmodule Plausible.Stats.Compare do
end
def calculate_change(:bounce_rate, old_count, new_count) do
- if is_integer(old_count) && is_integer(new_count) && old_count > 0 do
- new_count - old_count
- end
+ if old_count > 0, do: new_count - old_count
end
def calculate_change(_metric, old_count, new_count) do
diff --git a/lib/plausible/stats/imported.ex b/lib/plausible/stats/imported.ex
index 4ab5f50d0f..52f7f92eb6 100644
--- a/lib/plausible/stats/imported.ex
+++ b/lib/plausible/stats/imported.ex
@@ -488,7 +488,15 @@ defmodule Plausible.Stats.Imported do
|> select_merge([s, i], %{
views_per_visit:
fragment(
- "round((? + ? * coalesce(?, 0)) / (coalesce(?, 0) + coalesce(?, 0)), 2)",
+ """
+ if(
+ coalesce(?, 0) + coalesce(?, 0) > 0,
+ round((? + ? * coalesce(?, 0)) / (coalesce(?, 0) + coalesce(?, 0)), 2),
+ 0
+ )
+ """,
+ s.__internal_visits,
+ i.__internal_visits,
i.pageviews,
s.views_per_visit,
s.__internal_visits,
@@ -504,7 +512,15 @@ defmodule Plausible.Stats.Imported do
|> select_merge([s, i], %{
bounce_rate:
fragment(
- "round(100 * (coalesce(?, 0) + coalesce((? * ? / 100), 0)) / (coalesce(?, 0) + coalesce(?, 0)))",
+ """
+ if(
+ coalesce(?, 0) + coalesce(?, 0) > 0,
+ round(100 * (coalesce(?, 0) + coalesce((? * ? / 100), 0)) / (coalesce(?, 0) + coalesce(?, 0))),
+ 0
+ )
+ """,
+ s.__internal_visits,
+ i.__internal_visits,
i.bounces,
s.bounce_rate,
s.__internal_visits,
@@ -520,7 +536,15 @@ defmodule Plausible.Stats.Imported do
|> select_merge([s, i], %{
visit_duration:
fragment(
- "round((? + ? * ?) / (? + ?), 1)",
+ """
+ if(
+ ? + ? > 0,
+ round((? + ? * ?) / (? + ?), 1),
+ 0
+ )
+ """,
+ s.__internal_visits,
+ i.__internal_visits,
i.visit_duration,
s.visit_duration,
s.__internal_visits,
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 97f8b91353..6ef32c3226 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
@@ -423,6 +423,23 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
describe "GET /api/stats/top-stats - with imported data" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data]
+ test "returns divisible metrics as 0 when no stats exist", %{
+ site: site,
+ conn: conn
+ } do
+ conn =
+ get(
+ conn,
+ "/api/stats/#{site.domain}/top-stats?period=day&date=2021-01-01&with_imported=true"
+ )
+
+ res = json_response(conn, 200)
+
+ assert %{"name" => "Bounce rate", "value" => 0} in res["top_stats"]
+ assert %{"name" => "Views per visit", "value" => 0.0} in res["top_stats"]
+ assert %{"name" => "Visit duration", "value" => 0} in res["top_stats"]
+ end
+
test "merges imported data into all top stat metrics", %{
conn: conn,
site: site