Skip to content

Commit

Permalink
Fix percentage and conversion_rate calculation in presence of custom …
Browse files Browse the repository at this point in the history
…props
  • Loading branch information
zoldar committed May 29, 2024
1 parent 2564be1 commit a7753eb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
40 changes: 28 additions & 12 deletions lib/plausible/stats/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,17 @@ defmodule Plausible.Stats.Base do

def add_percentage_metric(q, site, query, metrics) do
if :percentage in metrics do
query =
query
|> Query.set_property(nil)
|> Query.exclude_imported()
total_query = Query.set_property(query, nil)

total_query =
if has_custom_property?(query) do
Query.exclude_imported(total_query)
else
total_query
end

q
|> select_merge(^%{__total_visitors: total_visitors_subquery(site, query)})
|> select_merge(^%{__total_visitors: total_visitors_subquery(site, total_query)})
|> select_merge(%{
percentage:
fragment(
Expand All @@ -339,14 +343,8 @@ defmodule Plausible.Stats.Base do
|> Query.remove_filters(["event:goal", "event:props"])
|> Query.set_property(nil)

has_custom_prop_filters? =
Enum.any?(query.filters, fn
[_, "event:props:" <> prop, _] -> prop not in Plausible.Imported.imported_custom_props()
_ -> false
end)

total_query =
if has_custom_prop_filters? do
if has_custom_prop_filters?(query) do
Query.exclude_imported(total_query)
else
total_query
Expand All @@ -368,4 +366,22 @@ defmodule Plausible.Stats.Base do
q
end
end

defp has_custom_property?(query) do
case query.property do
"event:props:" <> prop -> prop not in Plausible.Imported.imported_custom_props()
_ -> false
end
end

defp has_custom_prop_filters?(query) do
if query.filters == [] do
false
else
Enum.any?(query.filters, fn
[_, "event:props:" <> prop, _] -> prop not in Plausible.Imported.imported_custom_props()
_ -> false
end)
end
end
end
2 changes: 1 addition & 1 deletion lib/plausible/stats/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ defmodule Plausible.Stats.Query do
struct!(query, filters: Filters.parse(params["filters"]))
end

@spec set_property(t(), String.t()) :: t()
@spec set_property(t(), String.t() | nil) :: t()
def set_property(query, property) do
query
|> struct!(property: property)
Expand Down

0 comments on commit a7753eb

Please sign in to comment.