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 handling filtering by imported special properties (#4179)
Adrian Gruntkowskia76493892849
diff --git a/lib/plausible/imported.ex b/lib/plausible/imported.exindex 35fa348922..47e73c3837 100644--- a/lib/plausible/imported.ex+++ b/lib/plausible/imported.ex@@ -57,6 +57,7 @@ defmodule Plausible.Imported do@spec imported_custom_props() :: [String.t()]def imported_custom_props doPlausible.Props.internal_keys()+ |> Enum.map(&("event:props:" <> &1))end@spec goals_with_url() :: [String.t()]diff --git a/lib/plausible/stats/imported/base.ex b/lib/plausible/stats/imported/base.exindex e15e09d62c..539f3fb83c 100644--- a/lib/plausible/stats/imported/base.ex+++ b/lib/plausible/stats/imported/base.ex@@ -29,8 +29,6 @@ defmodule Plausible.Stats.Imported.Base do"visit:os_version" => "imported_operating_systems","event:page" => "imported_pages","event:name" => "imported_custom_events",- "event:props:url" => "imported_custom_events",- "event:props:path" => "imported_custom_events",# NOTE: these properties can be only filtered by"visit:screen" => "imported_devices",@@ -78,9 +76,13 @@ defmodule Plausible.Stats.Imported.Base doenddef decide_table(query) do- query- |> transform_filters()- |> do_decide_table()+ query = transform_filters(query)++ if custom_prop_query?(query) do+ do_decide_custom_prop_table(query)+ else+ do_decide_table(query)+ endenddefp transform_filters(query) do@@ -114,28 +116,58 @@ defmodule Plausible.Stats.Imported.Base dostruct!(query, filters: new_filters)end- defp do_decide_table(%Query{filters: [], property: nil}), do: "imported_visitors"+ defp custom_prop_query?(query) do+ query.filters+ |> Enum.map(&Enum.at(&1, 1))+ |> Enum.concat([query.property])+ |> Enum.any?(&(&1 in @imported_custom_props))+ end++ defp do_decide_custom_prop_table(%{property: property} = query)+ when property in @imported_custom_props do+ do_decide_custom_prop_table(query, property)+ end- defp do_decide_table(%Query{filters: filters, property: "event:props:" <> prop_key = property})- when prop_key in @imported_custom_props do+ defp do_decide_custom_prop_table(%{property: property} = query)+ when property in [nil, "event:goal", "event:name"] do+ custom_prop_filters =+ query.filters+ |> Enum.map(&Enum.at(&1, 1))+ |> Enum.filter(&(&1 in @imported_custom_props))+ |> Enum.uniq()++ case custom_prop_filters do+ [custom_prop_filter] ->+ do_decide_custom_prop_table(query, custom_prop_filter)++ _ ->+ nil+ end+ end++ defp do_decide_custom_prop_table(_query), do: nil++ defp do_decide_custom_prop_table(query, property) dohas_required_name_filter? =- Enum.any?(filters, fn- [:is, "event:name", name] -> name in special_goals_for(prop_key)+ Enum.any?(query.filters, fn+ [:is, "event:name", name] -> name in special_goals_for(property)_ -> falseend)has_unsupported_filters? =- Enum.any?(filters, fn [_, filtered_prop | _] ->+ Enum.any?(query.filters, fn [_, filtered_prop | _] ->filtered_prop not in [property, "event:name"]end)- if has_required_name_filter? && not has_unsupported_filters? do+ if has_required_name_filter? and not has_unsupported_filters? do"imported_custom_events"elsenilendend+ defp do_decide_table(%Query{filters: [], property: nil}), do: "imported_visitors"+defp do_decide_table(%Query{filters: [], property: "event:goal"}) do"imported_custom_events"end@@ -186,6 +218,6 @@ defmodule Plausible.Stats.Imported.Base doend)end- def special_goals_for("url"), do: Imported.goals_with_url()- def special_goals_for("path"), do: Imported.goals_with_path()+ def special_goals_for("event:props:url"), do: Imported.goals_with_url()+ def special_goals_for("event:props:path"), do: Imported.goals_with_path()enddiff --git a/lib/plausible/stats/imported/imported.ex b/lib/plausible/stats/imported/imported.exindex 2589a23873..a387ab1a24 100644--- a/lib/plausible/stats/imported/imported.ex+++ b/lib/plausible/stats/imported/imported.ex@@ -14,7 +14,8 @@ defmodule Plausible.Stats.Imported do@property_to_table_mappings Imported.Base.property_to_table_mappings()- @imported_properties Map.keys(@property_to_table_mappings)+ @imported_properties Map.keys(@property_to_table_mappings) +++ Plausible.Imported.imported_custom_props()@goals_with_url Plausible.Imported.goals_with_url()@@ -273,7 +274,6 @@ defmodule Plausible.Stats.Imported dosite|> Imported.Base.query_imported(query)|> where([i], i.visitors > 0)- |> maybe_apply_filter(query, property, dim)|> group_imported_by(dim)|> select_imported_metrics(metrics)@@ -357,13 +357,6 @@ defmodule Plausible.Stats.Imported do|> select_merge([i], %{total_visitors: fragment("sum(?)", i.visitors)})end- defp maybe_apply_filter(q, query, property, dim) do- case Query.get_filter(query, property) do- [:member, _, list] -> where(q, [i], field(i, ^dim) in ^list)- _ -> q- end- end-defp select_imported_metrics(q, []), do: qdefp select_imported_metrics(q, [:visitors | rest]) dodiff --git a/test/plausible/stats/query_test.exs b/test/plausible/stats/query_test.exsindex 43419ef782..629ea03449 100644--- a/test/plausible/stats/query_test.exs+++ b/test/plausible/stats/query_test.exs@@ -278,5 +278,143 @@ defmodule Plausible.Stats.QueryTest do"property" => "event:props:url"})end++ test "is false when breaking down by url but with a mismatched special goal filter",+ %{site: site} do+ insert(:site_import, site: site)+ site = Plausible.Imported.load_import_data(site)++ assert %{include_imported: false} =+ Query.from(site, %{+ "period" => "day",+ "with_imported" => "true",+ "property" => "event:props:url",+ "filters" => Jason.encode!(%{"goal" => "404"})+ })+ end++ test "is false when breaking down by url but with a special goal filter and an arbitrary filter",+ %{site: site} do+ insert(:site_import, site: site)+ site = Plausible.Imported.load_import_data(site)++ assert %{include_imported: false} =+ Query.from(site, %{+ "period" => "day",+ "with_imported" => "true",+ "property" => "event:props:url",+ "filters" =>+ Jason.encode!(%{"goal" => "Outbound Link: Click", "page" => "/example"})+ })+ end++ for property <- [nil, "event:goal", "event:name"] do+ test "is true when filtering by custom prop and special goal when breakdown prop is #{property}",+ %{site: site} do+ insert(:site_import, site: site)+ site = Plausible.Imported.load_import_data(site)++ assert %{include_imported: true} =+ Query.from(site, %{+ "period" => "day",+ "with_imported" => "true",+ "property" => unquote(property),+ "filters" =>+ Jason.encode!(%{+ "goal" => "Outbound Link: Click",+ "props" => %{"url" => "https://example.com"}+ })+ })+ end+ end++ test "is true when filtering by matching multiple custom prop values and special goal", %{+ site: site+ } do+ insert(:site_import, site: site)+ site = Plausible.Imported.load_import_data(site)++ assert %{include_imported: true} =+ Query.from(site, %{+ "period" => "day",+ "with_imported" => "true",+ "property" => nil,+ "filters" =>+ Jason.encode!(%{+ "goal" => "Outbound Link: Click",+ "props" => %{"url" => "https://example.com|https://another.example.com"}+ })+ })+ end++ test "is false when filtering by mismatched custom prop values and special goal", %{+ site: site+ } do+ insert(:site_import, site: site)+ site = Plausible.Imported.load_import_data(site)++ assert %{include_imported: false} =+ Query.from(site, %{+ "period" => "day",+ "with_imported" => "true",+ "property" => nil,+ "filters" =>+ Jason.encode!(%{+ "goal" => "Outbound Link: Click",+ "props" => %{"url" => "https://example.com", "path" => "/whatever"}+ })+ })+ end++ test "is false with a custom prop + mismatched special goal filter",+ %{site: site} do+ insert(:site_import, site: site)+ site = Plausible.Imported.load_import_data(site)++ assert %{include_imported: false} =+ Query.from(site, %{+ "period" => "day",+ "with_imported" => "true",+ "property" => nil,+ "filters" =>+ Jason.encode!(%{"goal" => "404", "props" => %{"url" => "https://example.com"}})+ })+ end++ test "is false with a custom prop + required goal + arbitrary filter",+ %{site: site} do+ insert(:site_import, site: site)+ site = Plausible.Imported.load_import_data(site)++ assert %{include_imported: false} =+ Query.from(site, %{+ "period" => "day",+ "with_imported" => "true",+ "property" => nil,+ "filters" =>+ Jason.encode!(%{+ "goal" => "Outbound Link: Click",+ "page" => "/example",+ "props" => %{"url" => "https://example.com"}+ })+ })+ end++ test "is false with a custom prop filter and non-matching property", %{site: site} do+ insert(:site_import, site: site)+ site = Plausible.Imported.load_import_data(site)++ assert %{include_imported: false} =+ Query.from(site, %{+ "period" => "day",+ "with_imported" => "true",+ "property" => "visit:source",+ "filters" =>+ Jason.encode!(%{+ "goal" => "Outbound Link: Click",+ "props" => %{"url" => "https://example.com"}+ })+ })+ endendend