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
Silently ignore missing goals (#6162)
Artur Patadbd4131581bd
diff --git a/lib/plausible/stats/dashboard/query_parser.ex b/lib/plausible/stats/dashboard/query_parser.exindex 641ddbc3e3..a969224ba1 100644--- a/lib/plausible/stats/dashboard/query_parser.ex+++ b/lib/plausible/stats/dashboard/query_parser.ex@@ -26,7 +26,8 @@ defmodule Plausible.Stats.Dashboard.QueryParser dorelative_date: relative_date,filters: filters,metrics: metrics,- include: include+ include: include,+ skip_goal_existence_check: true})}endenddiff --git a/lib/plausible/stats/parsed_query_params.ex b/lib/plausible/stats/parsed_query_params.exindex dbf7100767..3a7405e1bd 100644--- a/lib/plausible/stats/parsed_query_params.ex+++ b/lib/plausible/stats/parsed_query_params.ex@@ -14,7 +14,11 @@ defmodule Plausible.Stats.ParsedQueryParams doorder_by: nil,pagination: nil,now: nil,- include: %Plausible.Stats.QueryInclude{}+ include: %Plausible.Stats.QueryInclude{},+ # When true, skips the validation that goal names in `is` filters must be+ # configured for the site. Missing goals simply match nothing in SQL, matching+ # the behaviour of legacy endpoints like top_stats.+ skip_goal_existence_check: falsedef new!(params) when is_map(params) dostruct!(__MODULE__, Map.to_list(params))diff --git a/lib/plausible/stats/query_builder.ex b/lib/plausible/stats/query_builder.exindex 05c91c65f2..ac98c17405 100644--- a/lib/plausible/stats/query_builder.ex+++ b/lib/plausible/stats/query_builder.ex@@ -35,7 +35,7 @@ defmodule Plausible.Stats.QueryBuilder do:ok <- validate_toplevel_only_filter_dimension(query),:ok <- validate_special_metrics_filters(query),:ok <- validate_behavioral_filters(query),- :ok <- validate_filtered_goals_exist(query),+ :ok <- validate_filtered_goals_exist(query, parsed_query_params),:ok <- validate_revenue_metrics_access(site, query),:ok <- validate_metrics(query),:ok <- validate_include(query) do@@ -382,7 +382,10 @@ defmodule Plausible.Stats.QueryBuilder doend)end- defp validate_filtered_goals_exist(query) do+ defp validate_filtered_goals_exist(_query, %ParsedQueryParams{skip_goal_existence_check: true}),+ do: :ok++ defp validate_filtered_goals_exist(query, %ParsedQueryParams{}) do# Note: We don't check :contains goal filters since it's acceptable if they match nothing.goal_filter_clauses =query.filtersdiff --git a/test/plausible_web/controllers/api/stats_controller/query_controller_test.exs b/test/plausible_web/controllers/api/stats_controller/query_controller_test.exsindex d7bab5c17d..69cf7fde7c 100644--- a/test/plausible_web/controllers/api/stats_controller/query_controller_test.exs+++ b/test/plausible_web/controllers/api/stats_controller/query_controller_test.exs@@ -133,5 +133,71 @@ defmodule PlausibleWeb.Api.InternalController.QueryTest doassert json_response(conn, 200)["results"] == [%{"metrics" => [3], "dimensions" => []}]end++ test "silently ignores missing event goal in filter", %{conn: conn, site: site} do+ populate_stats(site, [+ build(:event, name: "Signup", timestamp: ~N[2021-01-01 00:00:00])+ ])++ conn =+ post(conn, "/api/stats/#{URI.encode(site.domain)}/query", %{+ "metrics" => ["visitors"],+ "date_range" => "all",+ "filters" => [["is", "event:goal", ["Missing goal"]]]+ })++ assert json_response(conn, 200)["results"] == [%{"metrics" => [0], "dimensions" => []}]+ end++ test "silently ignores missing pageview goal in filter", %{conn: conn, site: site} do+ populate_stats(site, [+ build(:pageview, pathname: "/register", timestamp: ~N[2021-01-01 00:00:00])+ ])++ conn =+ post(conn, "/api/stats/#{URI.encode(site.domain)}/query", %{+ "metrics" => ["visitors"],+ "date_range" => "all",+ "filters" => [["is", "event:goal", ["Visit /"]]]+ })++ assert json_response(conn, 200)["results"] == [%{"metrics" => [0], "dimensions" => []}]+ end++ test "silently ignores missing goal and still returns results for existing goal in filter",+ %{conn: conn, site: site} do+ insert(:goal, %{site: site, event_name: "Signup"})++ populate_stats(site, [+ build(:event, name: "Signup", user_id: 1, timestamp: ~N[2021-01-01 00:00:00]),+ build(:event, name: "Purchase", user_id: 2, timestamp: ~N[2021-01-01 00:00:00])+ ])++ conn =+ post(conn, "/api/stats/#{URI.encode(site.domain)}/query", %{+ "metrics" => ["visitors"],+ "date_range" => "all",+ "filters" => [["is", "event:goal", ["Signup", "Missing goal"]]]+ })++ assert json_response(conn, 200)["results"] == [%{"metrics" => [1], "dimensions" => []}]+ end++ test "silently ignores missing goal in has_done behavioral filter",+ %{conn: conn, site: site} do+ populate_stats(site, [+ build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),+ build(:pageview, timestamp: ~N[2021-01-01 00:00:00])+ ])++ conn =+ post(conn, "/api/stats/#{URI.encode(site.domain)}/query", %{+ "metrics" => ["visitors"],+ "date_range" => "all",+ "filters" => [["has_done", ["is", "event:goal", ["Missing goal"]]]]+ })++ assert json_response(conn, 200)["results"] == [%{"metrics" => [0], "dimensions" => []}]+ endendend