Skip to content

Commit

Permalink
Implement wildcard filters for the API
Browse files Browse the repository at this point in the history
  • Loading branch information
ukutaht committed Jan 21, 2022
1 parent ebb9476 commit 0a5f6c5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/plausible/stats/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0a5f6c5

Please sign in to comment.