mirror of
https://github.com/plausible/analytics.git
synced 2024-11-23 11:12:15 +03:00
Implement wildcard filters for the API
This commit is contained in:
parent
ebb9476b56
commit
0a5f6c5886
@ -233,9 +233,12 @@ defmodule Plausible.Stats.Query do
|
||||
|
||||
is_negated = String.contains?(str, "!=")
|
||||
is_list = String.contains?(val, "|")
|
||||
is_wildcard = String.contains?(val, "*")
|
||||
|
||||
cond do
|
||||
key == "event:goal" -> {key, parse_goal_filter(val)}
|
||||
is_wildcard && is_negated -> {key, {:does_not_match, val}}
|
||||
is_wildcard -> {key, {:matches, val}}
|
||||
is_list -> {key, {:member, String.split(val, "|")}}
|
||||
is_negated -> {key, {:is_not, val}}
|
||||
true -> {key, {:is, val}}
|
||||
|
@ -729,5 +729,57 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AggregateTest do
|
||||
"visit_duration" => %{"value" => 1500}
|
||||
}
|
||||
end
|
||||
|
||||
test "wildcard page filter", %{conn: conn, site: site} do
|
||||
populate_stats(site, [
|
||||
build(:pageview, pathname: "/en/page1"),
|
||||
build(:pageview, pathname: "/en/page2"),
|
||||
build(:pageview, pathname: "/pl/page1")
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"metrics" => "visitors",
|
||||
"filters" => "event:page==/en/**"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200)["results"] == %{"visitors" => %{"value" => 2}}
|
||||
end
|
||||
|
||||
test "negated wildcard page filter", %{conn: conn, site: site} do
|
||||
populate_stats(site, [
|
||||
build(:pageview, pathname: "/en/page1"),
|
||||
build(:pageview, pathname: "/en/page2"),
|
||||
build(:pageview, pathname: "/pl/page1")
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"metrics" => "visitors",
|
||||
"filters" => "event:page!=/en/**"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200)["results"] == %{"visitors" => %{"value" => 1}}
|
||||
end
|
||||
|
||||
test "wildcard and member filter combined", %{conn: conn, site: site} do
|
||||
populate_stats(site, [
|
||||
build(:pageview, pathname: "/en/page1"),
|
||||
build(:pageview, pathname: "/en/page2"),
|
||||
build(:pageview, pathname: "/pl/page1"),
|
||||
build(:pageview, pathname: "/ee/page1")
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/aggregate", %{
|
||||
"site_id" => site.domain,
|
||||
"metrics" => "visitors",
|
||||
"filters" => "event:page==/en/**|/pl/**"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200)["results"] == %{"visitors" => %{"value" => 3}}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -830,6 +830,33 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
|
||||
}
|
||||
end
|
||||
|
||||
test "can filter event:page with a wildcard", %{
|
||||
conn: conn,
|
||||
site: site
|
||||
} do
|
||||
populate_stats(site, [
|
||||
build(:pageview, pathname: "/en/page1"),
|
||||
build(:pageview, pathname: "/en/page2"),
|
||||
build(:pageview, pathname: "/en/page2"),
|
||||
build(:pageview, pathname: "/pl/page1")
|
||||
])
|
||||
|
||||
conn =
|
||||
get(conn, "/api/v1/stats/breakdown", %{
|
||||
"site_id" => site.domain,
|
||||
"period" => "day",
|
||||
"property" => "event:page",
|
||||
"filters" => "event:page==/en/**"
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"results" => [
|
||||
%{"page" => "/en/page2", "visitors" => 2},
|
||||
%{"page" => "/en/page1", "visitors" => 1}
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
test "breakdown by custom event property", %{conn: conn, site: site} do
|
||||
populate_stats([
|
||||
build(:event,
|
||||
|
Loading…
Reference in New Issue
Block a user