From 8be9397199d2f2b1ae424b198cd9e60f82446cdc Mon Sep 17 00:00:00 2001 From: hq1 Date: Tue, 2 Jan 2024 13:19:04 +0100 Subject: [PATCH] Skip breakdown of imported pages with pageviews=0 (#3655) * Skip breakdown of imported pages with pageviews=0 * Fixup * Prove good impoted records are merged --- lib/plausible/stats/imported.ex | 1 + .../breakdown_test.exs | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/lib/plausible/stats/imported.ex b/lib/plausible/stats/imported.ex index 7e44b2005..394ba6fe8 100644 --- a/lib/plausible/stats/imported.ex +++ b/lib/plausible/stats/imported.ex @@ -368,6 +368,7 @@ defmodule Plausible.Stats.Imported do defp select_imported_metrics(q, [:pageviews | rest]) do q + |> where([i], i.pageviews > 0) |> select_merge([i], %{pageviews: sum(i.pageviews)}) |> select_imported_metrics(rest) end diff --git a/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exs b/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exs index 3d9c9542e..eb603f158 100644 --- a/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exs +++ b/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exs @@ -523,6 +523,53 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do } end + test "pageviews breakdown by event:page - imported data having pageviews=0 and visitors=n should be bypassed", + %{conn: conn, site: site} do + site = + site + |> Plausible.Site.start_import(~D[2005-01-01], Timex.today(), "Google Analytics", "ok") + |> Plausible.Repo.update!() + + populate_stats(site, [ + build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:00:00]), + build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:25:00]), + build(:pageview, + pathname: "/plausible.io", + timestamp: ~N[2021-01-01 00:00:00] + ), + build(:imported_pages, + page: "/skip-me", + date: ~D[2021-01-01], + visitors: 1, + pageviews: 0 + ), + build(:imported_pages, + page: "/include-me", + date: ~D[2021-01-01], + visitors: 1, + pageviews: 1 + ) + ]) + + conn = + get(conn, "/api/v1/stats/breakdown", %{ + "site_id" => site.domain, + "period" => "day", + "date" => "2021-01-01", + "property" => "event:page", + "with_imported" => "true", + "metrics" => "pageviews" + }) + + assert json_response(conn, 200) == %{ + "results" => [ + %{"page" => "/", "pageviews" => 2}, + %{"page" => "/plausible.io", "pageviews" => 1}, + %{"page" => "/include-me", "pageviews" => 1} + ] + } + end + test "breakdown by event:page", %{conn: conn, site: site} do populate_stats(site, [ build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:00:00]),