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
Skip meta.key/meta.value reads when no goal has custom props (#6144)
Uku Taht965986c3ac0c
diff --git a/lib/plausible/stats/query.ex b/lib/plausible/stats/query.exindex 3a5ba04d89..80239f4d01 100644--- a/lib/plausible/stats/query.ex+++ b/lib/plausible/stats/query.ex@@ -31,7 +31,8 @@ defmodule Plausible.Stats.Query do# Contains information to determine how to combine legacy and new time on page metricstime_on_page_data: %{},sql_join_type: :left,- smear_session_metrics: false+ smear_session_metrics: false,+ optimized_conversions: falserequire OpenTelemetry.Tracer, as: Tracerdiff --git a/lib/plausible/stats/query_builder.ex b/lib/plausible/stats/query_builder.exindex 05c91c65f2..71cfb63ef1 100644--- a/lib/plausible/stats/query_builder.ex+++ b/lib/plausible/stats/query_builder.ex@@ -182,7 +182,8 @@ defmodule Plausible.Stats.QueryBuilder dopreloaded_goals: preloaded_goals,revenue_warning: revenue_warning,revenue_currencies: revenue_currencies,- debug_metadata: debug_metadata+ debug_metadata: debug_metadata,+ optimized_conversions: FunWithFlags.enabled?(:optimized_conversions, for: site))enddiff --git a/lib/plausible/stats/sql/expression.ex b/lib/plausible/stats/sql/expression.exindex 1db9dcb386..c48ae9e0d0 100644--- a/lib/plausible/stats/sql/expression.ex+++ b/lib/plausible/stats/sql/expression.ex@@ -486,4 +486,38 @@ defmodule Plausible.Stats.SQL.Expression do)endend++ @doc """+ Optimized variant of `event_goal_join/1` for use when no goals have custom+ property filters. Omits all references to `meta.key` and `meta.value`,+ preventing ClickHouse from reading those expensive Array(String) columns at+ scan time.+ """+ defmacro event_goal_join_no_props(goal_join_data) do+ quote do+ fragment(+ """+ arrayIntersect(+ multiMatchAllIndices(?, ?),+ arrayMap(+ (expected_name, threshold, index) -> if(+ expected_name = ? and ? between threshold and 100,+ index, -1+ ),+ ?,+ ?,+ ?+ )+ )+ """,+ e.pathname,+ type(^unquote(goal_join_data).page_regexes, {:array, :string}),+ e.name,+ e.scroll_depth,+ type(^unquote(goal_join_data).event_names_by_type, {:array, :string}),+ type(^unquote(goal_join_data).scroll_thresholds, {:array, :integer}),+ type(^unquote(goal_join_data).indices, {:array, :integer})+ )+ end+ endenddiff --git a/lib/plausible/stats/sql/query_builder.ex b/lib/plausible/stats/sql/query_builder.exindex a31d3bffbc..04c6d69380 100644--- a/lib/plausible/stats/sql/query_builder.ex+++ b/lib/plausible/stats/sql/query_builder.ex@@ -171,15 +171,27 @@ defmodule Plausible.Stats.SQL.QueryBuilder dodefp dimension_group_by(q, :events, query, "event:goal" = dimension) dogoal_join_data = Plausible.Stats.Goals.goal_join_data(query)- from(e in q,- join: goal in Expression.event_goal_join(goal_join_data),- hints: "ARRAY",- on: true,- select_merge: %{- ^shortname(query, dimension) => fragment("?", goal)- },- group_by: goal- )+ if query.optimized_conversions and Enum.all?(goal_join_data.custom_props_keys, &Enum.empty?/1) do+ from(e in q,+ join: goal in Expression.event_goal_join_no_props(goal_join_data),+ hints: "ARRAY",+ on: true,+ select_merge: %{+ ^shortname(query, dimension) => fragment("?", goal)+ },+ group_by: goal+ )+ else+ from(e in q,+ join: goal in Expression.event_goal_join(goal_join_data),+ hints: "ARRAY",+ on: true,+ select_merge: %{+ ^shortname(query, dimension) => fragment("?", goal)+ },+ group_by: goal+ )+ endenddefp dimension_group_by(q, table, query, dimension) dodiff --git a/test/plausible_web/controllers/api/external_stats_controller/query_goal_dimension_test.exs b/test/plausible_web/controllers/api/external_stats_controller/query_goal_dimension_test.exsindex 9deb1ee2ce..2974a41ffc 100644--- a/test/plausible_web/controllers/api/external_stats_controller/query_goal_dimension_test.exs+++ b/test/plausible_web/controllers/api/external_stats_controller/query_goal_dimension_test.exs@@ -651,4 +651,70 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryGoalDimensionTest do]endend++ describe "optimized_conversions flag" do+ test "returns same results as baseline for goals without custom props", %{+ conn: conn,+ site: site+ } do+ FunWithFlags.enable(:optimized_conversions, for_actor: site)++ insert(:goal, %{site: site, event_name: "Purchase"})+ insert(:goal, %{site: site, page_path: "/test"})++ populate_stats(site, [+ build(:pageview, timestamp: ~N[2021-01-01 00:00:01], pathname: "/test"),+ build(:event, name: "Purchase", timestamp: ~N[2021-01-01 00:00:03]),+ build(:event, name: "Purchase", timestamp: ~N[2021-01-01 00:00:03])+ ])++ conn =+ post(conn, "/api/v2/query", %{+ "site_id" => site.domain,+ "date_range" => "all",+ "metrics" => ["visitors"],+ "dimensions" => ["event:goal"]+ })++ assert json_response(conn, 200)["results"] == [+ %{"dimensions" => ["Purchase"], "metrics" => [2]},+ %{"dimensions" => ["Visit /test"], "metrics" => [1]}+ ]+ end++ test "falls back to full query when goals have custom props", %{conn: conn, site: site} do+ FunWithFlags.enable(:optimized_conversions, for_actor: site)++ {:ok, _goal} =+ Plausible.Goals.create(site, %{+ "event_name" => "Purchase",+ "custom_props" => %{"plan" => "premium"}+ })++ populate_stats(site, [+ build(:event,+ name: "Purchase",+ "meta.key": ["plan"],+ "meta.value": ["premium"]+ ),+ build(:event,+ name: "Purchase",+ "meta.key": ["plan"],+ "meta.value": ["free"]+ )+ ])++ conn =+ post(conn, "/api/v2/query", %{+ "site_id" => site.domain,+ "date_range" => "all",+ "metrics" => ["visitors"],+ "dimensions" => ["event:goal"]+ })++ assert json_response(conn, 200)["results"] == [+ %{"dimensions" => ["Purchase"], "metrics" => [1]}+ ]+ end+ endend