Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Fix custom props in conversions
Uku Tahtf9ea3cda6b85
diff --git a/assets/js/dashboard/filters.js b/assets/js/dashboard/filters.jsindex 738c87017f..02242e3ce0 100644--- a/assets/js/dashboard/filters.js+++ b/assets/js/dashboard/filters.js@@ -30,7 +30,7 @@ function clearAllFilters(history, query) {}function filterType(val) {- if (val.startsWith('!')) {+ if (typeof(val) === 'string' && val.startsWith('!')) {return ['is not', val.substr(1)]}diff --git a/lib/plausible/stats/breakdown.ex b/lib/plausible/stats/breakdown.exindex a7ab661a70..ad04d06040 100644--- a/lib/plausible/stats/breakdown.ex+++ b/lib/plausible/stats/breakdown.ex@@ -16,38 +16,46 @@ defmodule Plausible.Stats.Breakdown doevents = Enum.map(event_goals, & &1.event_name)event_query = %Query{query | filters: Map.put(query.filters, "event:name", {:member, events})}- event_goals =- breakdown(site, event_query, "event:name", metrics, pagination)- |> transform_keys(%{"name" => "goal"})-- page_exprs = Enum.map(pageview_goals, & &1.page_path)- page_regexes = Enum.map(page_exprs, &page_regex/1)+ event_results =+ if Enum.any?(event_goals) do+ breakdown(site, event_query, "event:name", metrics, pagination)+ |> transform_keys(%{"name" => "goal"})+ else+ []+ end{limit, page} = paginationoffset = (page - 1) * limit- page_goals =- from(e in base_event_query(site, query),- order_by: [desc: fragment("uniq(?)", e.user_id)],- limit: ^limit,- offset: ^offset,- where:- fragment(- "notEmpty(multiMatchAllIndices(?, array(?)) as indices)",- e.pathname,- ^page_regexes- ),- group_by: fragment("index"),- select: %{- "index" => fragment("arrayJoin(indices) as index"),- "goal" => fragment("concat('Visit ', array(?)[index])", ^page_exprs)- }- )- |> select_event_metrics(metrics)- |> ClickhouseRepo.all()- |> Enum.map(fn row -> Map.delete(row, "index") end)+ page_results =+ if Enum.any?(pageview_goals) do+ page_exprs = Enum.map(pageview_goals, & &1.page_path)+ page_regexes = Enum.map(page_exprs, &page_regex/1)++ from(e in base_event_query(site, query),+ order_by: [desc: fragment("uniq(?)", e.user_id)],+ limit: ^limit,+ offset: ^offset,+ where:+ fragment(+ "notEmpty(multiMatchAllIndices(?, array(?)) as indices)",+ e.pathname,+ ^page_regexes+ ),+ group_by: fragment("index"),+ select: %{+ "index" => fragment("arrayJoin(indices) as index"),+ "goal" => fragment("concat('Visit ', array(?)[index])", ^page_exprs)+ }+ )+ |> select_event_metrics(metrics)+ |> ClickhouseRepo.all()+ |> Enum.map(fn row -> Map.delete(row, "index") end)+ else+ []+ end- zip_results(event_goals, page_goals, "event:goal", metrics)+ zip_results(event_results, page_results, "event:goal", metrics)enddef breakdown(site, query, "event:props:" <> custom_prop, metrics, pagination) dodiff --git a/lib/plausible/stats/props.ex b/lib/plausible/stats/props.exindex 68b9d8c4bf..09278a0673 100644--- a/lib/plausible/stats/props.ex+++ b/lib/plausible/stats/props.ex@@ -3,15 +3,30 @@ defmodule Plausible.Stats.Props doalias Plausible.Stats.Queryimport Plausible.Stats.Base- def props(site, %Query{filters: %{"props" => meta}} = query) when is_map(meta) do- [{key, val}] = meta |> Enum.into([])+ def props(site, query) do+ prop_filter =+ Enum.find(query.filters, fn {key, _} -> String.starts_with?(key, "event:props:") end)++ if prop_filter do+ {key, val} = prop_filter- if val == "(none)" do- goal = query.filters["goal"]- %{goal => [key]}+ if val == "(none)" do+ goal = query.filters["goal"]+ %{goal => [key]}+ else+ ClickhouseRepo.all(+ from [e, meta: meta] in base_event_query(site, query),+ select: {e.name, meta.key},+ distinct: true+ )+ |> Enum.reduce(%{}, fn {goal_name, meta_key}, acc ->+ Map.update(acc, goal_name, [meta_key], fn list -> [meta_key | list] end)+ end)+ endelseClickhouseRepo.all(- from [e, meta: meta] in base_event_query(site, query),+ from e in base_event_query(site, query),+ inner_lateral_join: meta in fragment("meta as m"),select: {e.name, meta.key},distinct: true)@@ -20,16 +35,4 @@ defmodule Plausible.Stats.Props doend)endend-- def props(site, query) do- ClickhouseRepo.all(- from e in base_event_query(site, query),- inner_lateral_join: meta in fragment("meta as m"),- select: {e.name, meta.key},- distinct: true- )- |> Enum.reduce(%{}, fn {goal_name, meta_key}, acc ->- Map.update(acc, goal_name, [meta_key], fn list -> [meta_key | list] end)- end)- endenddiff --git a/lib/plausible/stats/query.ex b/lib/plausible/stats/query.exindex 37c006e527..7a8dfcc8fa 100644--- a/lib/plausible/stats/query.ex+++ b/lib/plausible/stats/query.ex@@ -171,6 +171,19 @@ defmodule Plausible.Stats.Query dodef treat_page_filter_as_entry_page(q), do: q+ def remove_goal(query) do+ props =+ Enum.map(query.filters, fn {key, val} -> key end)+ |> Enum.filter(fn filter_key -> String.starts_with?(filter_key, "event:props:") end)++ new_filters =+ query.filters+ |> Map.drop(props)+ |> Map.delete("event:goal")++ %__MODULE__{query | filters: new_filters}+ end+defp today(tz) doTimex.now(tz) |> Timex.to_date()enddiff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.exindex 3b6f57e2ae..0fe26b8ed8 100644--- a/lib/plausible_web/controllers/api/stats_controller.ex+++ b/lib/plausible_web/controllers/api/stats_controller.ex@@ -87,16 +87,17 @@ defmodule PlausibleWeb.Api.StatsController doenddefp fetch_top_stats(site, %Query{filters: %{"event:goal" => _goal}} = query) do- total_filter = Map.merge(query.filters, %{"event:goal" => nil})+ total_q = Query.remove_goal(query)prev_query = Query.shift_back(query, site)+ prev_total_query = Query.shift_back(total_q, site)%{"visitors" => %{"value" => unique_visitors}- } = Stats.aggregate(site, %{query | filters: total_filter}, ["visitors"])+ } = Stats.aggregate(site, total_q, ["visitors"])%{"visitors" => %{"value" => prev_unique_visitors}- } = Stats.aggregate(site, %{prev_query | filters: total_filter}, ["visitors"])+ } = Stats.aggregate(site, prev_total_query, ["visitors"])%{"visitors" => %{"value" => converted_visitors},@@ -483,10 +484,9 @@ defmodule PlausibleWeb.Api.StatsController doquery = Query.from(site.timezone, params) |> Filters.add_prefix()pagination = parse_pagination(params)- total_filter = Map.merge(query.filters, %{"event:goal" => nil})+ total_q = Query.remove_goal(query)- %{"visitors" => %{"value" => total_visitors}} =- Stats.aggregate(site, %{query | filters: total_filter}, ["visitors"])+ %{"visitors" => %{"value" => total_visitors}} = Stats.aggregate(site, total_q, ["visitors"])prop_names = Stats.props(site, query)@@ -507,10 +507,9 @@ defmodule PlausibleWeb.Api.StatsController doquery = Query.from(site.timezone, params) |> Filters.add_prefix()pagination = parse_pagination(params)- total_filter = Map.merge(query.filters, %{"event:goal" => nil})+ total_q = Query.remove_goal(query)- %{"visitors" => %{"value" => unique_visitors}} =- Stats.aggregate(site, %{query | filters: total_filter}, ["visitors"])+ %{"visitors" => %{"value" => unique_visitors}} = Stats.aggregate(site, total_q, ["visitors"])prop_name = "event:props:" <> params["prop_name"]