Skip to content

Commit

Permalink
still show url breakdown when special goal + url in filter
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertJoonas committed May 30, 2024
1 parent 5940c40 commit 0db19ec
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 20 deletions.
38 changes: 19 additions & 19 deletions lib/plausible/stats/imported/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ defmodule Plausible.Stats.Imported.Base do
"event:hostname" => "imported_pages"
}

@goals_with_url Imported.goals_with_url()
@goals_with_path Imported.goals_with_path()
@imported_custom_props Imported.imported_custom_props()

@db_field_mappings %{
referrer_source: :source,
Expand Down Expand Up @@ -117,26 +116,24 @@ defmodule Plausible.Stats.Imported.Base do
end

defp do_decide_table(%Query{filters: [], property: nil}), do: "imported_visitors"
defp do_decide_table(%Query{filters: [], property: "event:props:url"}), do: nil
defp do_decide_table(%Query{filters: [], property: "event:props:path"}), do: nil

defp do_decide_table(%Query{filters: filters, property: "event:props:url"}) do
case filters do
[[:is, "event:name", name]] when name in @goals_with_url ->
"imported_custom_events"

_ ->
nil
end
end
defp do_decide_table(%Query{filters: filters, property: "event:props:" <> prop_key = property})
when prop_key in @imported_custom_props do
has_required_name_filter? =
Enum.any?(filters, fn
[:is, "event:name", name] -> name in special_goals_for(prop_key)
_ -> false
end)

defp do_decide_table(%Query{filters: filters, property: "event:props:path"}) do
case filters do
[[:is, "event:name", name]] when name in @goals_with_path ->
"imported_custom_events"
has_unsupported_filters? =
Enum.any?(filters, fn [_, filtered_prop | _] ->
filtered_prop not in [property, "event:name"]
end)

_ ->
nil
if has_required_name_filter? && not has_unsupported_filters? do
"imported_custom_events"
else
nil
end
end

Expand Down Expand Up @@ -189,4 +186,7 @@ defmodule Plausible.Stats.Imported.Base do
where(q, ^condition)
end)
end

def special_goals_for("url"), do: Imported.goals_with_url()
def special_goals_for("path"), do: Imported.goals_with_path()
end
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ defmodule PlausibleWeb.Api.StatsController.CustomPropBreakdownTest do
describe "with imported data" do
setup [:create_user, :log_in, :create_new_site]

for goal_name <- ["Outbound Link: Click", "File Download"] do
for goal_name <- ["Outbound Link: Click", "File Download", "Cloaked Link: Click"] do
test "returns url breakdown for #{goal_name} goal", %{conn: conn, site: site} do
insert(:goal, event_name: unquote(goal_name), site: site)
site_import = insert(:site_import, site: site)
Expand Down Expand Up @@ -1196,5 +1196,58 @@ defmodule PlausibleWeb.Api.StatsController.CustomPropBreakdownTest do
]
end
end

for goal_name <- ["Outbound Link: Click", "File Download", "Cloaked Link: Click"] do
test "returns url breakdown for #{goal_name} goal with a url filter", %{
conn: conn,
site: site
} do
insert(:goal, event_name: unquote(goal_name), site: site)
site_import = insert(:site_import, site: site)

populate_stats(site, site_import.id, [
build(:event,
name: unquote(goal_name),
"meta.key": ["url"],
"meta.value": ["https://one.com"]
),
build(:imported_custom_events,
name: unquote(goal_name),
visitors: 2,
events: 5,
link_url: "https://one.com"
),
build(:imported_custom_events,
name: unquote(goal_name),
visitors: 5,
events: 10,
link_url: "https://two.com"
),
build(:imported_custom_events,
name: "view_search_results",
visitors: 100,
events: 200
),
build(:imported_visitors, visitors: 9)
])

filters = Jason.encode!(%{goal: unquote(goal_name), props: %{url: "https://two.com"}})

conn =
get(
conn,
"/api/stats/#{site.domain}/custom-prop-values/url?period=day&with_imported=true&filters=#{filters}"
)

assert json_response(conn, 200)["results"] == [
%{
"visitors" => 5,
"name" => "https://two.com",
"events" => 10,
"conversion_rate" => 50.0
}
]
end
end
end
end

0 comments on commit 0db19ec

Please sign in to comment.