Skip to content

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

Filter out GA imported data with no visitors recorded (#3123)

hq1cf5cc8de94c2
diff --git a/lib/plausible/stats/imported.ex b/lib/plausible/stats/imported.ex
index 1243200533..ef7d70eaa0 100644
--- a/lib/plausible/stats/imported.ex
+++ b/lib/plausible/stats/imported.ex
@@ -114,6 +114,7 @@ defmodule Plausible.Stats.Imported do
group_by: field(i, ^dim),
where: i.site_id == ^site.id,
where: i.date >= ^query.date_range.first and i.date <= ^query.date_range.last,
+ where: i.visitors > 0,
select: %{}
)
|> select_imported_metrics(metrics)
diff --git a/test/plausible_web/controllers/api/stats_controller/browsers_test.exs b/test/plausible_web/controllers/api/stats_controller/browsers_test.exs
index c5f670a837..155cba2b75 100644
--- a/test/plausible_web/controllers/api/stats_controller/browsers_test.exs
+++ b/test/plausible_web/controllers/api/stats_controller/browsers_test.exs
@@ -130,6 +130,27 @@ defmodule PlausibleWeb.Api.StatsController.BrowsersTest do
]
end
+ test "skips breakdown when visitors=0 (possibly due to 'Enable Users Metric' in GA)", %{
+ conn: conn,
+ site: site
+ } do
+ populate_stats(site, [
+ build(:imported_browsers, browser: "Chrome", visitors: 0, visits: 14),
+ build(:imported_browsers, browser: "Firefox", visitors: 0, visits: 14),
+ build(:imported_browsers,
+ browser: "''",
+ visitors: 0,
+ visits: 14,
+ visit_duration: 0,
+ bounces: 14
+ )
+ ])
+
+ conn = get(conn, "/api/stats/#{site.domain}/browsers?period=day&with_imported=true")
+
+ assert json_response(conn, 200) == []
+ end
+
test "returns (not set) when appropriate", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview,