Skip to content

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

Reveal segment filters in v2 Stats API (#5145)

Artur Pata0f62ad0e98b3
diff --git a/priv/json-schemas/query-api-schema.json b/priv/json-schemas/query-api-schema.json
index 0c95244ce9..2a8d54088e 100644
--- a/priv/json-schemas/query-api-schema.json
+++ b/priv/json-schemas/query-api-schema.json
@@ -462,10 +462,7 @@
{ "$ref": "#/definitions/filter_without_goals" },
{ "$ref": "#/definitions/filter_with_goals" },
{ "$ref": "#/definitions/filter_with_pattern" },
- {
- "$ref": "#/definitions/filter_for_segment",
- "$comment": "only :internal"
- }
+ { "$ref": "#/definitions/filter_for_segment" }
]
},
"filter_tree": {
diff --git a/test/plausible/stats/query_parser_test.exs b/test/plausible/stats/query_parser_test.exs
index 5615a0ebd5..a359d829a0 100644
--- a/test/plausible/stats/query_parser_test.exs
+++ b/test/plausible/stats/query_parser_test.exs
@@ -2283,8 +2283,7 @@ defmodule Plausible.Stats.Filters.QueryParserTest do
}
|> check_error(
site,
- "Invalid filters. You can only use up to 10 segment filters in a query.",
- :internal
+ "Invalid filters. You can only use up to 10 segment filters in a query."
)
end
@@ -2310,8 +2309,7 @@ defmodule Plausible.Stats.Filters.QueryParserTest do
}
|> check_error(
site,
- "Invalid filters. Some segments don't exist or aren't accessible.",
- :internal
+ "Invalid filters. Some segments don't exist or aren't accessible."
)
end
@@ -2339,8 +2337,7 @@ defmodule Plausible.Stats.Filters.QueryParserTest do
}
|> check_error(
site,
- "The owner of this site does not have access to the custom properties feature.",
- :internal
+ "The owner of this site does not have access to the custom properties feature."
)
end
@@ -2376,8 +2373,7 @@ defmodule Plausible.Stats.Filters.QueryParserTest do
}
|> check_error(
site,
- "Invalid filters. Dimension `event:goal` can only be filtered at the top level.",
- :internal
+ "Invalid filters. Dimension `event:goal` can only be filtered at the top level."
)
end
@@ -2460,8 +2456,46 @@ defmodule Plausible.Stats.Filters.QueryParserTest do
timezone: site.timezone,
include: @default_include,
pagination: %{limit: 10_000, offset: 0}
- },
- :internal
+ }
+ )
+ end
+
+ test "resolves segments containing otherwise internal features", %{site: site, user: user} do
+ insert(:goal, %{site: site, event_name: "Signup"})
+
+ segment_from_dashboard =
+ insert(:segment,
+ name: "A segment that contains :internal features",
+ type: :site,
+ owner: user,
+ site: site,
+ segment_data: %{
+ "filters" => [["has_not_done", ["is", "event:goal", ["Signup"]]]]
+ }
+ )
+
+ %{
+ "site_id" => site.domain,
+ "metrics" => ["visitors", "events"],
+ "date_range" => "all",
+ "filters" => [
+ ["is", "segment", [segment_from_dashboard.id]]
+ ]
+ }
+ |> check_success(
+ site,
+ %{
+ metrics: [:visitors, :events],
+ utc_time_range: @date_range_day,
+ filters: [
+ [:has_not_done, [:is, "event:goal", ["Signup"]]]
+ ],
+ dimensions: [],
+ order_by: nil,
+ timezone: site.timezone,
+ include: @default_include,
+ pagination: %{limit: 10_000, offset: 0}
+ }
)
end
end
diff --git a/test/plausible_web/controllers/api/external_stats_controller/query_test.exs b/test/plausible_web/controllers/api/external_stats_controller/query_test.exs
index 104fa08176..fa62fc7a83 100644
--- a/test/plausible_web/controllers/api/external_stats_controller/query_test.exs
+++ b/test/plausible_web/controllers/api/external_stats_controller/query_test.exs
@@ -4900,20 +4900,6 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryTest do
describe "segment filters" do
setup [:create_user, :create_site, :create_api_key, :use_api_key]
- test "segment filters are (not yet) available in public API", %{conn: conn, site: site} do
- conn =
- post(conn, "/api/v2/query", %{
- "site_id" => site.domain,
- "filters" => [["is", "segment", [1]]],
- "date_range" => "all",
- "metrics" => ["visitors"]
- })
-
- assert json_response(conn, 400) == %{
- "error" => "#/filters/0: Invalid filter [\"is\", \"segment\", [1]]"
- }
- end
-
test "site segments of other sites don't resolve", %{
conn: conn,
site: site
@@ -4933,7 +4919,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryTest do
)
conn =
- post(conn, "/api/v2/query-internal-test", %{
+ post(conn, "/api/v2/query", %{
"site_id" => site.domain,
"filters" => [["is", "segment", [segment.id]]],
"date_range" => "all",
@@ -4945,6 +4931,63 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryTest do
}
end
+ test ":has_not_done filter resolves in /api/v2/query endpoint when it is part of a segment",
+ %{
+ conn: conn,
+ site: site
+ } do
+ other_user = add_guest(site, role: :editor)
+
+ segment =
+ insert(:segment,
+ type: :site,
+ owner: other_user,
+ site: site,
+ name: "Signups",
+ segment_data: %{
+ "filters" => [["has_not_done", ["is", "event:goal", ["Signup"]]]]
+ }
+ )
+
+ insert(:goal, %{site: site, event_name: "Signup"})
+
+ populate_stats(site, [
+ build(:event,
+ name: "Signup",
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:event,
+ name: "Signup",
+ user_id: @user_id,
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:event,
+ name: "Signup",
+ user_id: @user_id,
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:event,
+ name: "AnyOtherEvent",
+ timestamp: ~N[2021-01-01 00:00:00]
+ )
+ ])
+
+ conn =
+ post(conn, "/api/v2/query", %{
+ "site_id" => site.domain,
+ "filters" => [["is", "segment", [segment.id]]],
+ "date_range" => "all",
+ "metrics" => ["events"]
+ })
+
+ assert json_response(conn, 200)["results"] == [%{"dimensions" => [], "metrics" => [1]}]
+
+ # response shows what filters the segment was resolved to
+ assert json_response(conn, 200)["query"]["filters"] == [
+ ["has_not_done", ["is", "event:goal", ["Signup"]]]
+ ]
+ end
+
test "even personal segments of other users of the same site resolve to filters, with segments expanded in response",
%{
conn: conn,
@@ -4987,7 +5030,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryTest do
])
conn =
- post(conn, "/api/v2/query-internal-test", %{
+ post(conn, "/api/v2/query", %{
"site_id" => site.domain,
"filters" => [["is", "segment", [segment.id]]],
"date_range" => "all",