Skip to content

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

Return warning when imported stats are skipped in Stats API due to unsupported query (#4116)

Adrian Gruntkowski2d23db298203
diff --git a/lib/plausible_web/controllers/api/external_stats_controller.ex b/lib/plausible_web/controllers/api/external_stats_controller.ex
index 01424f50a2..77e3d51ca5 100644
--- a/lib/plausible_web/controllers/api/external_stats_controller.ex
+++ b/lib/plausible_web/controllers/api/external_stats_controller.ex
@@ -41,7 +41,9 @@ defmodule PlausibleWeb.Api.ExternalStatsController do
Plausible.Stats.aggregate(site, query, metrics)
end
- json(conn, %{results: results})
+ payload = maybe_add_warning(%{results: results}, query)
+
+ json(conn, payload)
else
err_tuple -> send_json_error_response(conn, err_tuple)
end
@@ -60,8 +62,9 @@ defmodule PlausibleWeb.Api.ExternalStatsController do
:ok <- ensure_custom_props_access(site, query) do
page = String.to_integer(Map.get(params, "page", "1"))
results = Plausible.Stats.breakdown(site, query, metrics, {limit, page})
+ payload = maybe_add_warning(%{results: results}, query)
- json(conn, %{results: results})
+ json(conn, payload)
else
err_tuple -> send_json_error_response(conn, err_tuple)
end
@@ -264,8 +267,9 @@ defmodule PlausibleWeb.Api.ExternalStatsController do
{:ok, metrics} <- parse_and_validate_metrics(params, query),
:ok <- ensure_custom_props_access(site, query) do
graph = Plausible.Stats.timeseries(site, query, metrics)
+ payload = maybe_add_warning(%{results: graph}, query)
- json(conn, %{results: graph})
+ json(conn, payload)
else
err_tuple -> send_json_error_response(conn, err_tuple)
end
@@ -375,6 +379,20 @@ defmodule PlausibleWeb.Api.ExternalStatsController do
"The goal `#{goal}` is not configured for this site. "
end
+ defp maybe_add_warning(payload, %{skip_imported_reason: reason})
+ when reason in [nil, :no_imported_data, :out_of_range] do
+ payload
+ end
+
+ defp maybe_add_warning(payload, %{skip_imported_reason: :unsupported_query}) do
+ Map.put(
+ payload,
+ :warning,
+ "Imported stats are not included in the results because query parameters are not supported. " <>
+ "For more information, see: https://plausible.io/docs/stats-api#filtering-imported-stats"
+ )
+ end
+
defp send_json_error_response(conn, {:error, {status, msg}}) do
conn
|> put_status(status)
diff --git a/test/plausible_web/controllers/api/external_stats_controller/aggregate_test.exs b/test/plausible_web/controllers/api/external_stats_controller/aggregate_test.exs
index cb248f1f66..5dcbe84601 100644
--- a/test/plausible_web/controllers/api/external_stats_controller/aggregate_test.exs
+++ b/test/plausible_web/controllers/api/external_stats_controller/aggregate_test.exs
@@ -514,6 +514,8 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AggregateTest do
assert json_response(conn2, 200)["results"] == %{
"pageviews" => %{"value" => 2}
}
+
+ refute json_response(conn2, 200)["warning"]
end
test "counts imported stats when comparing with previous period", %{
@@ -588,6 +590,97 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AggregateTest do
"visitors" => %{"value" => 1, "change" => 100}
}
end
+
+ test "adds a warning when query params are not supported for imported data", %{
+ conn: conn,
+ site: site,
+ site_import: site_import
+ } do
+ populate_stats(site, site_import.id, [
+ build(:event,
+ name: "Signup",
+ "meta.key": ["package"],
+ "meta.value": ["large"]
+ ),
+ build(:imported_visitors, visitors: 9)
+ ])
+
+ conn =
+ get(conn, "/api/v1/stats/aggregate", %{
+ "site_id" => site.domain,
+ "period" => "day",
+ "metrics" => "visitors",
+ "filters" => "event:props:package==large",
+ "compare" => "previous_period",
+ "with_imported" => "true"
+ })
+
+ assert json_response(conn, 200)["results"] == %{
+ "visitors" => %{"change" => 100, "value" => 1}
+ }
+
+ assert json_response(conn, 200)["warning"] =~
+ "Imported stats are not included in the results because query parameters are not supported."
+ end
+
+ test "does not add a warning when there are no site imports", %{
+ conn: conn,
+ site: site,
+ site_import: site_import
+ } do
+ Plausible.Repo.delete!(site_import)
+
+ populate_stats(site, [
+ build(:event,
+ name: "Signup",
+ "meta.key": ["package"],
+ "meta.value": ["large"]
+ )
+ ])
+
+ conn =
+ get(conn, "/api/v1/stats/aggregate", %{
+ "site_id" => site.domain,
+ "period" => "day",
+ "metrics" => "visitors",
+ "filters" => "event:props:package==large",
+ "compare" => "previous_period",
+ "with_imported" => "true"
+ })
+
+ refute json_response(conn, 200)["warning"]
+ end
+
+ test "does not add a warning when import is out of queried date range", %{
+ conn: conn,
+ site: site,
+ site_import: site_import
+ } do
+ site_import
+ |> Ecto.Changeset.change(end_date: Date.add(Date.utc_today(), -3))
+ |> Plausible.Repo.update!()
+
+ populate_stats(site, site_import.id, [
+ build(:event,
+ name: "Signup",
+ "meta.key": ["package"],
+ "meta.value": ["large"]
+ ),
+ build(:imported_visitors, visitors: 9)
+ ])
+
+ conn =
+ get(conn, "/api/v1/stats/aggregate", %{
+ "site_id" => site.domain,
+ "period" => "day",
+ "metrics" => "visitors",
+ "filters" => "event:props:package==large",
+ "compare" => "previous_period",
+ "with_imported" => "true"
+ })
+
+ refute json_response(conn, 200)["warning"]
+ end
end
describe "filters" do
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 acc074480c..1125722da2 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
@@ -3322,6 +3322,8 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
"conversion_rate" => 30.0
}
]
+
+ refute json_response(conn, 200)["warning"]
end
end
@@ -3380,7 +3382,101 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
"conversion_rate" => 30.0
}
]
+
+ refute json_response(conn, 200)["warning"]
end
end
+
+ test "adds a warning when query params are not supported for imported data", %{
+ conn: conn,
+ site: site
+ } do
+ site_import = insert(:site_import, site: site)
+
+ insert(:goal, event_name: "Signup", site: site)
+
+ populate_stats(site, site_import.id, [
+ build(:event,
+ name: "Signup",
+ "meta.key": ["package"],
+ "meta.value": ["large"]
+ ),
+ build(:imported_visitors, visitors: 9)
+ ])
+
+ conn =
+ get(conn, "/api/v1/stats/breakdown", %{
+ "site_id" => site.domain,
+ "period" => "day",
+ "property" => "event:props:package",
+ "filters" => "event:goal==Signup",
+ "metrics" => "visitors",
+ "with_imported" => "true"
+ })
+
+ assert json_response(conn, 200)["results"] == [
+ %{
+ "package" => "large",
+ "visitors" => 1
+ }
+ ]
+
+ assert json_response(conn, 200)["warning"] =~
+ "Imported stats are not included in the results because query parameters are not supported."
+ end
+
+ test "does not add a warning when there are no site imports", %{conn: conn, site: site} do
+ insert(:goal, event_name: "Signup", site: site)
+
+ populate_stats(site, [
+ build(:event,
+ name: "Signup",
+ "meta.key": ["package"],
+ "meta.value": ["large"]
+ )
+ ])
+
+ conn =
+ get(conn, "/api/v1/stats/breakdown", %{
+ "site_id" => site.domain,
+ "period" => "day",
+ "property" => "event:props:package",
+ "filters" => "event:goal==Signup",
+ "metrics" => "visitors",
+ "with_imported" => "true"
+ })
+
+ refute json_response(conn, 200)["warning"]
+ end
+
+ test "does not add a warning when import is out of queried date range", %{
+ conn: conn,
+ site: site
+ } do
+ site_import = insert(:site_import, site: site, end_date: Date.add(Date.utc_today(), -3))
+
+ insert(:goal, event_name: "Signup", site: site)
+
+ populate_stats(site, site_import.id, [
+ build(:event,
+ name: "Signup",
+ "meta.key": ["package"],
+ "meta.value": ["large"]
+ ),
+ build(:imported_visitors, visitors: 9)
+ ])
+
+ conn =
+ get(conn, "/api/v1/stats/breakdown", %{
+ "site_id" => site.domain,
+ "period" => "day",
+ "property" => "event:props:package",
+ "filters" => "event:goal==Signup",
+ "metrics" => "visitors",
+ "with_imported" => "true"
+ })
+
+ refute json_response(conn, 200)["warning"]
+ end
end
end
diff --git a/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs b/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs
index 75bc9655ef..d1ecccfa32 100644
--- a/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs
+++ b/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs
@@ -1537,7 +1537,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.TimeseriesTest do
build(:imported_visitors, pageviews: 1, date: ~D[2021-01-01])
])
- first_result =
+ conn =
conn
|> get("/api/v1/stats/timeseries", %{
"site_id" => site.domain,
@@ -1546,11 +1546,106 @@ defmodule PlausibleWeb.Api.ExternalStatsController.TimeseriesTest do
"date" => "2021-01-07",
"with_imported" => "true"
})
+
+ first_result =
+ conn
+ |> json_response(200)
+ |> Map.get("results")
+ |> List.first()
+
+ assert first_result == %{"date" => "2021-01-01", "events" => 1}
+
+ refute json_response(conn, 200)["warning"]
+ end
+
+ test "adds a warning when query params are not supported for imported data", %{
+ conn: conn,
+ site: site
+ } do
+ site_import = insert(:site_import, site: site)
+
+ populate_stats(site, site_import.id, [
+ build(:pageview,
+ "meta.key": ["package"],
+ "meta.value": ["large"],
+ timestamp: ~N[2021-01-01 12:00:00]
+ ),
+ build(:imported_visitors, pageviews: 1, date: ~D[2021-01-01])
+ ])
+
+ conn =
+ conn
+ |> get("/api/v1/stats/timeseries", %{
+ "site_id" => site.domain,
+ "period" => "7d",
+ "metrics" => "events",
+ "date" => "2021-01-07",
+ "with_imported" => "true",
+ "filters" => "event:props:package==large"
+ })
+
+ first_result =
+ conn
|> json_response(200)
|> Map.get("results")
|> List.first()
assert first_result == %{"date" => "2021-01-01", "events" => 1}
+
+ assert json_response(conn, 200)["warning"] =~
+ "Imported stats are not included in the results because query parameters are not supported."
+ end
+
+ test "does not add a warning when there are no site imports", %{conn: conn, site: site} do
+ populate_stats(site, [
+ build(:pageview,
+ "meta.key": ["package"],
+ "meta.value": ["large"],
+ timestamp: ~N[2021-01-01 12:00:00]
+ )
+ ])
+
+ conn =
+ conn
+ |> get("/api/v1/stats/timeseries", %{
+ "site_id" => site.domain,
+ "period" => "7d",
+ "metrics" => "events",
+ "date" => "2021-01-07",
+ "with_imported" => "true",
+ "filters" => "event:props:package==large"
+ })
+
+ refute json_response(conn, 200)["warning"]
+ end
+
+ test "does not add a warning when import is out of queried date range", %{
+ conn: conn,
+ site: site
+ } do
+ site_import = insert(:site_import, site: site, end_date: ~D[2020-12-29])
+
+ populate_stats(site, site_import.id, [
+ build(:pageview,
+ "meta.key": ["package"],
+ "meta.value": ["large"],
+ timestamp: ~N[2021-01-01 12:00:00]
+ ),
+ build(:imported_visitors, pageviews: 1, date: ~D[2020-12-29])
+ ])
+
+ conn =
+ conn
+ |> get("/api/v1/stats/timeseries", %{
+ "site_id" => site.domain,
+ "period" => "7d",
+ "metrics" => "events",
+ "date" => "2021-01-07",
+ "with_imported" => "true",
+ "filters" => "event:props:package==large"
+ })
+
+ refute json_response(conn, 200)["warning"]
end
end
end