From 8fcf4d3304e75c31d44be85b9e8084cc219e59db Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Tue, 14 Feb 2023 13:57:38 +0200 Subject: [PATCH] Fix 'field key does not exist' error (#2674) --- lib/plausible/stats/breakdown.ex | 5 ++- .../api/stats_controller/conversions_test.exs | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/plausible/stats/breakdown.ex b/lib/plausible/stats/breakdown.ex index 395b2a70c..52db5b3d7 100644 --- a/lib/plausible/stats/breakdown.ex +++ b/lib/plausible/stats/breakdown.ex @@ -336,12 +336,13 @@ defmodule Plausible.Stats.Breakdown do else from( e in q, - inner_lateral_join: meta in fragment("meta") + inner_lateral_join: meta in fragment("meta"), + as: :meta ) end from( - [e, meta] in q, + [e, meta: meta] in q, where: meta.key == ^prop, group_by: meta.value, select_merge: %{^prop => meta.value}, diff --git a/test/plausible_web/controllers/api/stats_controller/conversions_test.exs b/test/plausible_web/controllers/api/stats_controller/conversions_test.exs index eb468b7ca..6cc20f951 100644 --- a/test/plausible_web/controllers/api/stats_controller/conversions_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/conversions_test.exs @@ -430,6 +430,51 @@ defmodule PlausibleWeb.Api.StatsController.ConversionsTest do } ] end + + test "Property breakdown with goal and source filter", %{conn: conn, site: site} do + populate_stats(site, [ + build(:pageview, user_id: 1, referrer_source: "Google"), + build(:event, + user_id: 1, + name: "ButtonClick", + "meta.key": ["variant"], + "meta.value": ["A"] + ), + build(:pageview, user_id: 2, referrer_source: "Google"), + build(:pageview, user_id: 3, referrer_source: "ignore"), + build(:event, + user_id: 3, + name: "ButtonClick", + "meta.key": ["variant"], + "meta.value": ["B"] + ) + ]) + + insert(:goal, %{domain: site.domain, event_name: "ButtonClick"}) + + filters = + Jason.encode!(%{ + goal: "ButtonClick", + source: "Google" + }) + + prop_key = "variant" + + conn = + get( + conn, + "/api/stats/#{site.domain}/property/#{prop_key}?period=day&filters=#{filters}" + ) + + assert json_response(conn, 200) == [ + %{ + "name" => "A", + "unique_conversions" => 1, + "total_conversions" => 1, + "conversion_rate" => 50.0 + } + ] + end end describe "GET /api/stats/:domain/conversions - with glob goals" do