diff --git a/assets/js/dashboard/stats/bar.js b/assets/js/dashboard/stats/bar.js
index 1acb6f977..7ab44bda7 100644
--- a/assets/js/dashboard/stats/bar.js
+++ b/assets/js/dashboard/stats/bar.js
@@ -10,13 +10,12 @@ function barWidth(count, all, plot) {
return count / maxVal * 100
}
-export default function Bar({count, all, bg, maxWidthDeduction, children, plot = "visitors"}) {
+export default function Bar({count, all, bg, children, plot = "visitors"}) {
const width = barWidth(count, all, plot)
return (
- <%= label f, :event_name, class: "block text-sm font-bold dark:text-gray-100" %>
- <%= text_input f, :event_name, class: "transition mt-3 bg-gray-100 dark:bg-gray-900 outline-none appearance-none border border-transparent rounded w-full p-2 text-gray-700 dark:text-gray-300 leading-normal focus:outline-none focus:bg-white dark:focus:bg-gray-800 focus:border-gray-300 dark:focus:border-gray-500", placeholder: "Signup" %>
- <%= error_tag f, :event_name %>
+
+ <%= label f, :event_name, class: "block text-sm font-bold dark:text-gray-100" %>
+ <%= text_input f, :event_name, class: "transition mt-3 bg-gray-100 dark:bg-gray-900 outline-none appearance-none border border-transparent rounded w-full p-2 text-gray-700 dark:text-gray-300 leading-normal focus:outline-none focus:bg-white dark:focus:bg-gray-800 focus:border-gray-300 dark:focus:border-gray-500", placeholder: "Signup" %>
+ <%= error_tag f, :event_name %>
+
+
+
+ <%= label f, :currency, "Reporting currency for revenue tracking (optional)", class: "block text-sm font-bold dark:text-gray-100" %>
+ <%= select f, :currency, Plausible.Goal.currency_options(), class: "transition mt-3 bg-gray-100 dark:bg-gray-900 outline-none appearance-none border border-transparent rounded w-full p-2 text-gray-700 dark:text-gray-300 leading-normal focus:outline-none focus:bg-white dark:focus:bg-gray-800 focus:border-gray-300 dark:focus:border-gray-500" %>
+ <%= error_tag f, :currency %>
+
<%= label f, :page_path, class: "block text-sm font-bold dark:text-gray-100" %>
diff --git a/mix.exs b/mix.exs
index e7323f085..4eae9d830 100644
--- a/mix.exs
+++ b/mix.exs
@@ -116,7 +116,8 @@ defmodule Plausible.MixProject do
{:siphash, "~> 3.2"},
{:timex, "~> 3.7"},
{:ua_inspector, "~> 3.0"},
- {:ex_doc, "~> 0.28", only: :dev, runtime: false}
+ {:ex_doc, "~> 0.28", only: :dev, runtime: false},
+ {:ex_money, "~> 5.12"}
]
end
diff --git a/mix.lock b/mix.lock
index cd7b5ac85..206cc4e9b 100644
--- a/mix.lock
+++ b/mix.lock
@@ -13,6 +13,7 @@
"ch": {:git, "https://github.com/plausible/ch.git", "f411daa07c6310d3308f81397905df65330aeb64", []},
"chatterbox": {:hex, :ts_chatterbox, "0.13.0", "6f059d97bcaa758b8ea6fffe2b3b81362bd06b639d3ea2bb088335511d691ebf", [:rebar3], [{:hpack, "~> 0.2.3", [hex: :hpack_erl, repo: "hexpm", optional: false]}], "hexpm", "b93d19104d86af0b3f2566c4cba2a57d2e06d103728246ba1ac6c3c0ff010aa7"},
"chto": {:git, "https://github.com/plausible/chto.git", "6afe76281e617c19df9b7a705c8401f604fe091f", []},
+ "cldr_utils": {:hex, :cldr_utils, "2.22.0", "5df60df2603dfeeffe26e40ab1ee565df34da288a53bb2db0d0dbd243fd646ef", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:decimal, "~> 1.9 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "ea14e8a6aa89ffd59a5d49baebe7ebf852cc024ac50dc2b3dabcd3786eeed657"},
"combination": {:hex, :combination, "0.0.3", "746aedca63d833293ec6e835aa1f34974868829b1486b1e1cb0685f0b2ae1f41", [:mix], [], "hexpm", "72b099f463df42ef7dc6371d250c7070b57b6c5902853f69deb894f79eda18ca"},
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"},
"comeonin": {:hex, :comeonin, "5.3.3", "2c564dac95a35650e9b6acfe6d2952083d8a08e4a89b93a481acb552b325892e", [:mix], [], "hexpm", "3e38c9c2cb080828116597ca8807bb482618a315bfafd98c90bc22a821cc84df"},
@@ -27,6 +28,7 @@
"db_connection": {:hex, :db_connection, "2.5.0", "bb6d4f30d35ded97b29fe80d8bd6f928a1912ca1ff110831edcd238a1973652c", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c92d5ba26cd69ead1ff7582dbb860adeedfff39774105a4f1c92cbb654b55aa2"},
"decimal": {:hex, :decimal, "2.1.0", "4a607736e18d9a77ea44fa18388bf43bb7f7df70ff2e0d23d4238f9d18a3ad7d", [:mix], [], "hexpm", "6396853b6b4bf8b1ff2c2217121f1fad4a1aff999014a10fba60e6009194f91b"},
"dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"},
+ "digital_token": {:hex, :digital_token, "0.4.0", "2ad6894d4a40be8b2890aad286ecd5745fa473fa5699d80361a8c94428edcd1f", [:mix], [{:cldr_utils, "~> 2.17", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "a178edf61d1fee5bb3c34e14b0f4ee21809ee87cade8738f87337e59e5e66e26"},
"double": {:hex, :double, "0.8.2", "8e1cfcccdaef76c18846bc08e555555a2a699b806fa207b6468572a60513cc6a", [:mix], [], "hexpm", "90287642b2ec86125e0457aaba2ab0e80f7d7050cc80a0cef733e59bd70aa67c"},
"earmark_parser": {:hex, :earmark_parser, "1.4.31", "a93921cdc6b9b869f519213d5bc79d9e218ba768d7270d46fdcf1c01bacff9e2", [:mix], [], "hexpm", "317d367ee0335ef037a87e46c91a2269fef6306413f731e8ec11fc45a7efd059"},
"ecto": {:hex, :ecto, "3.9.5", "9f0aa7ae44a1577b651c98791c6988cd1b69b21bc724e3fd67090b97f7604263", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d4f3115d8cbacdc0bfa4b742865459fb1371d0715515842a1fb17fe31920b74c"},
@@ -35,8 +37,12 @@
"envy": {:hex, :envy, "1.1.1", "0bc9bd654dec24fcdf203f7c5aa1b8f30620f12cfb28c589d5e9c38fe1b07475", [:mix], [], "hexpm", "7061eb1a47415fd757145d8dec10dc0b1e48344960265cb108f194c4252c3a89"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"eternal": {:hex, :eternal, "1.2.2", "d1641c86368de99375b98d183042dd6c2b234262b8d08dfd72b9eeaafc2a1abd", [:mix], [], "hexpm", "2c9fe32b9c3726703ba5e1d43a1d255a4f3f2d8f8f9bc19f094c7cb1a7a9e782"},
+ "ex_cldr": {:hex, :ex_cldr, "2.36.0", "ccd7c61c4126aa42d3cd9d93097642930f1b8a10c9c8351fc58bb2c627bad839", [:mix], [{:cldr_utils, "~> 2.21", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.19", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: true]}], "hexpm", "5a56c66cd61ebde42277baa828cd1587959b781ac7e2aee135328a78a4de3fe9"},
+ "ex_cldr_currencies": {:hex, :ex_cldr_currencies, "2.15.0", "aadd34e91cfac7ef6b03fe8f47f8c6fa8c5daf3f89b5d9fee64ec545ded839cf", [:mix], [{:ex_cldr, "~> 2.34", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "0521316396c66877a2d636219767560bb2397c583341fcb154ecf9f3000e6ff8"},
+ "ex_cldr_numbers": {:hex, :ex_cldr_numbers, "2.30.1", "9acd7adb30079057ba606d73ffdaccb86020b07b734f98a229b5674032181668", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:digital_token, "~> 0.3 or ~> 1.0", [hex: :digital_token, repo: "hexpm", optional: false]}, {:ex_cldr, "~> 2.35", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_currencies, ">= 2.14.2", [hex: :ex_cldr_currencies, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "8a81b63d595a589e72c8b629653755c8b88edd6405a657eba236477f7a85939a"},
"ex_doc": {:hex, :ex_doc, "0.29.4", "6257ecbb20c7396b1fe5accd55b7b0d23f44b6aa18017b415cb4c2b91d997729", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "2c6699a737ae46cb61e4ed012af931b57b699643b24dabe2400a8168414bc4f5"},
"ex_machina": {:hex, :ex_machina, "2.7.0", "b792cc3127fd0680fecdb6299235b4727a4944a09ff0fa904cc639272cd92dc7", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}], "hexpm", "419aa7a39bde11894c87a615c4ecaa52d8f107bbdd81d810465186f783245bf8"},
+ "ex_money": {:hex, :ex_money, "5.12.4", "2c9778ccac56eb16796dac4cebc02eb7d093639aed08c904444a8d9fa7e69e29", [:mix], [{:cldr_utils, "~> 2.21", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ex_cldr_numbers, "~> 2.27", [hex: :ex_cldr_numbers, repo: "hexpm", optional: false]}, {:gringotts, "~> 1.1", [hex: :gringotts, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.0 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:poison, "~> 3.0 or ~> 4.0 or ~> 5.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm", "2b7977fb58af4f79bb7861741314b26b954b8e4d771471490ad68942fcd11619"},
"exactor": {:hex, :exactor, "2.2.4", "5efb4ddeb2c48d9a1d7c9b465a6fffdd82300eb9618ece5d34c3334d5d7245b1", [:mix], [], "hexpm", "1222419f706e01bfa1095aec9acf6421367dcfab798a6f67c54cf784733cd6b5"},
"excoveralls": {:hex, :excoveralls, "0.14.4", "295498f1ae47bdc6dce59af9a585c381e1aefc63298d48172efaaa90c3d251db", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "e3ab02f2df4c1c7a519728a6f0a747e71d7d6e846020aae338173619217931c1"},
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm", "32e95820a97cffea67830e91514a2ad53b888850442d6d395f53a1ac60c82e07"},
diff --git a/priv/ingest_repo/migrations/20230417104025_add_revenue_to_events.exs b/priv/ingest_repo/migrations/20230417104025_add_revenue_to_events.exs
new file mode 100644
index 000000000..b19e9ac0f
--- /dev/null
+++ b/priv/ingest_repo/migrations/20230417104025_add_revenue_to_events.exs
@@ -0,0 +1,13 @@
+defmodule Plausible.IngestRepo.Migrations.AddRevenueToEvents do
+ use Ecto.Migration
+
+ def change do
+ alter table(:events_v2) do
+ add :revenue_reporting_amount, :"Decimal64(4)"
+ add :revenue_reporting_currency, :"LowCardinality(FixedString(3))"
+
+ add :revenue_source_amount, :"Decimal64(4)"
+ add :revenue_source_currency, :"LowCardinality(FixedString(3))"
+ end
+ end
+end
diff --git a/priv/repo/migrations/20230417092745_add_monetary_value_to_goals.exs b/priv/repo/migrations/20230417092745_add_monetary_value_to_goals.exs
new file mode 100644
index 000000000..a37fc5d8f
--- /dev/null
+++ b/priv/repo/migrations/20230417092745_add_monetary_value_to_goals.exs
@@ -0,0 +1,9 @@
+defmodule Plausible.Repo.Migrations.AddMonetaryValueToGoals do
+ use Ecto.Migration
+
+ def change do
+ alter table(:goals) do
+ add :currency, :string
+ end
+ end
+end
diff --git a/test/plausible/ingestion/request_test.exs b/test/plausible/ingestion/request_test.exs
index 7de51d1ee..7ceb5f470 100644
--- a/test/plausible/ingestion/request_test.exs
+++ b/test/plausible/ingestion/request_test.exs
@@ -158,6 +158,91 @@ defmodule Plausible.Ingestion.RequestTest do
assert request.props["custom2"] == "property2"
end
+ test "parses revenue source field from a json string" do
+ payload = %{
+ name: "pageview",
+ domain: "dummy.site",
+ url: "http://dummy.site/index.html",
+ revenue: "{\"amount\":20.2,\"currency\":\"EUR\"}"
+ }
+
+ conn = build_conn(:post, "/api/events", payload)
+
+ assert {:ok, request} = Request.build(conn)
+ assert %Money{amount: amount, currency: :EUR} = request.revenue_source
+ assert Decimal.new("20.2") == amount
+ end
+
+ test "sets revenue source with integer amount" do
+ payload = %{
+ name: "pageview",
+ domain: "dummy.site",
+ url: "http://dummy.site/index.html",
+ revenue: %{
+ "amount" => 20,
+ "currency" => "USD"
+ }
+ }
+
+ conn = build_conn(:post, "/api/events", payload)
+
+ assert {:ok, request} = Request.build(conn)
+ assert %Money{amount: amount, currency: :USD} = request.revenue_source
+ assert Decimal.equal?(amount, Decimal.new("20.0"))
+ end
+
+ test "sets revenue source with float amount" do
+ payload = %{
+ name: "pageview",
+ domain: "dummy.site",
+ url: "http://dummy.site/index.html",
+ revenue: %{
+ "amount" => 20.1,
+ "currency" => "USD"
+ }
+ }
+
+ conn = build_conn(:post, "/api/events", payload)
+
+ assert {:ok, request} = Request.build(conn)
+ assert %Money{amount: amount, currency: :USD} = request.revenue_source
+ assert Decimal.equal?(amount, Decimal.new("20.1"))
+ end
+
+ test "parses string amounts into money structs" do
+ payload = %{
+ name: "pageview",
+ domain: "dummy.site",
+ url: "http://dummy.site/index.html",
+ revenue: %{
+ "amount" => "12.3",
+ "currency" => "USD"
+ }
+ }
+
+ conn = build_conn(:post, "/api/events", payload)
+
+ assert {:ok, request} = Request.build(conn)
+ assert %Money{amount: amount, currency: :USD} = request.revenue_source
+ assert Decimal.equal?(amount, Decimal.new("12.3"))
+ end
+
+ test "rejects the event when currency is invalid" do
+ payload = %{
+ name: "pageview",
+ domain: "dummy.site",
+ url: "http://dummy.site/index.html",
+ revenue: %{
+ "amount" => 1233.2,
+ "currency" => "EEEE"
+ }
+ }
+
+ conn = build_conn(:post, "/api/events", payload)
+ assert {:error, changeset} = Request.build(conn)
+ assert {"currency is not supported or invalid", _} = changeset.errors[:revenue_source]
+ end
+
test "pathname is set" do
payload = %{
name: "pageview",
diff --git a/test/plausible/site/cache_test.exs b/test/plausible/site/cache_test.exs
index f150e3ad9..9f6233a82 100644
--- a/test/plausible/site/cache_test.exs
+++ b/test/plausible/site/cache_test.exs
@@ -1,7 +1,7 @@
defmodule Plausible.Site.CacheTest do
use Plausible.DataCase, async: true
- alias Plausible.Site
+ alias Plausible.{Site, Goal}
alias Plausible.Site.Cache
import ExUnit.CaptureLog
@@ -53,6 +53,31 @@ defmodule Plausible.Site.CacheTest do
refute Cache.get("site3.example.com", cache_name: test, force?: true)
end
+ test "cache caches revenue goals", %{test: test} do
+ {:ok, _} =
+ Supervisor.start_link([{Cache, [cache_name: test, child_id: :test_cache_caches_id]}],
+ strategy: :one_for_one,
+ name: Test.Supervisor.Cache
+ )
+
+ %{id: site_id} = site = insert(:site, domain: "site1.example.com")
+ insert(:goal, site: site, event_name: "Purchase", currency: :BRL)
+ insert(:goal, site: site, event_name: "Add to Cart", currency: :USD)
+ insert(:goal, site: site, event_name: "Click", currency: nil)
+
+ :ok = Cache.refresh_all(cache_name: test)
+
+ {:ok, _} = Plausible.Repo.delete(site)
+
+ assert %Site{from_cache?: true, id: ^site_id, revenue_goals: cached_goals} =
+ Cache.get("site1.example.com", force?: true, cache_name: test)
+
+ assert [
+ %Goal{event_name: "Add to Cart", currency: :USD},
+ %Goal{event_name: "Purchase", currency: :BRL}
+ ] = Enum.sort_by(cached_goals, & &1.event_name)
+ end
+
test "cache is ready when no sites exist in the db", %{test: test} do
{:ok, _} = start_test_cache(test)
assert Cache.ready?(test)
diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exs
index 495e1ffb7..ca6ab1fb0 100644
--- a/test/plausible_web/controllers/api/external_controller_test.exs
+++ b/test/plausible_web/controllers/api/external_controller_test.exs
@@ -671,6 +671,91 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
assert Map.get(event, :"meta.value") == ["true"]
end
+ test "converts revenue values into the goal currency", %{conn: conn, site: site} do
+ params = %{
+ name: "Payment",
+ url: "http://gigride.live/",
+ domain: site.domain,
+ revenue: %{amount: 10.2, currency: "USD"}
+ }
+
+ insert(:goal, event_name: "Payment", currency: "BRL", site: site)
+
+ assert %{status: 202} = post(conn, "/api/event", params)
+ assert %{revenue_reporting_amount: amount} = get_event(site)
+
+ assert Decimal.equal?(Decimal.new("7.14"), amount)
+ end
+
+ test "revenue values can be sent with minified keys", %{conn: conn, site: site} do
+ params = %{
+ "n" => "Payment",
+ "u" => "http://gigride.live/",
+ "d" => site.domain,
+ "$" => Jason.encode!(%{amount: 10.2, currency: "USD"})
+ }
+
+ insert(:goal, event_name: "Payment", currency: "BRL", site: site)
+
+ assert %{status: 202} = post(conn, "/api/event", params)
+ assert %{revenue_reporting_amount: amount} = get_event(site)
+
+ assert Decimal.equal?(Decimal.new("7.14"), amount)
+ end
+
+ test "saves the exact same amount when goal currency is the same as the event", %{
+ conn: conn,
+ site: site
+ } do
+ params = %{
+ name: "Payment",
+ url: "http://gigride.live/",
+ domain: site.domain,
+ revenue: %{amount: 10, currency: "BRL"}
+ }
+
+ insert(:goal, event_name: "Payment", currency: "BRL", site: site)
+
+ assert %{status: 202} = post(conn, "/api/event", params)
+ assert %{revenue_reporting_amount: amount} = get_event(site)
+
+ assert Decimal.equal?(Decimal.new("10.0"), amount)
+ end
+
+ test "fails when revenue value is invalid", %{conn: conn, site: site} do
+ params = %{
+ name: "Payment",
+ url: "http://gigride.live/",
+ domain: site.domain,
+ revenue: %{amount: "1831d", currency: "ADSIE"}
+ }
+
+ insert(:goal, event_name: "Payment", currency: "BRL", site: site)
+
+ conn = post(conn, "/api/event", params)
+
+ assert json_response(conn, 400) == %{
+ "errors" => %{"revenue_source" => ["currency is not supported or invalid"]}
+ }
+ end
+
+ test "does not save revenue value if no goal matches", %{conn: conn, site: site} do
+ params = %{
+ name: "Add to Cart",
+ url: "http://gigride.live/",
+ domain: site.domain,
+ revenue: %{amount: 10.2, currency: "USD"}
+ }
+
+ insert(:goal, event_name: "Checkout", currency: "BRL", site: site)
+ insert(:goal, event_name: "Payment", currency: "USD", site: site)
+
+ assert %{status: 202} = post(conn, "/api/event", params)
+ assert %{revenue_reporting_amount: amount} = get_event(site)
+
+ assert Decimal.equal?(Decimal.new("0.0"), amount)
+ end
+
test "ignores a malformed referrer URL", %{conn: conn, site: site} do
params = %{
name: "pageview",
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 b0f15dff2..69715e930 100644
--- a/test/plausible_web/controllers/api/stats_controller/conversions_test.exs
+++ b/test/plausible_web/controllers/api/stats_controller/conversions_test.exs
@@ -224,6 +224,93 @@ defmodule PlausibleWeb.Api.StatsController.ConversionsTest do
assert resp == []
end
+
+ test "returns formatted average and total values for a conversion with revenue value", %{
+ conn: conn,
+ site: site
+ } do
+ populate_stats(site, [
+ build(:event, name: "Payment", revenue_reporting_amount: Decimal.new("200100300.123")),
+ build(:event, name: "Payment", revenue_reporting_amount: Decimal.new("300100400.123")),
+ build(:event, name: "Payment")
+ ])
+
+ insert(:goal, %{site: site, event_name: "Payment", currency: :EUR})
+
+ conn = get(conn, "/api/stats/#{site.domain}/conversions?period=day")
+
+ assert json_response(conn, 200) == [
+ %{
+ "name" => "Payment",
+ "unique_conversions" => 3,
+ "total_conversions" => 3,
+ "prop_names" => [],
+ "conversion_rate" => 100.0,
+ "average_revenue" => %{"short" => "€250.1M", "long" => "€250,100,350.12"},
+ "total_revenue" => %{"short" => "€500.2M", "long" => "€500,200,700.25"}
+ }
+ ]
+ end
+
+ test "returns revenue metrics as nil for non-revenue goals", %{
+ conn: conn,
+ site: site
+ } do
+ populate_stats(site, [
+ build(:event, name: "Signup"),
+ build(:event, name: "Signup"),
+ build(:event, name: "Payment")
+ ])
+
+ insert(:goal, %{site: site, event_name: "Signup"})
+ insert(:goal, %{site: site, event_name: "Payment", currency: :EUR})
+
+ conn = get(conn, "/api/stats/#{site.domain}/conversions?period=day")
+
+ assert json_response(conn, 200) == [
+ %{
+ "name" => "Signup",
+ "unique_conversions" => 2,
+ "total_conversions" => 2,
+ "prop_names" => [],
+ "conversion_rate" => 66.7,
+ "average_revenue" => nil,
+ "total_revenue" => nil
+ },
+ %{
+ "name" => "Payment",
+ "unique_conversions" => 1,
+ "total_conversions" => 1,
+ "prop_names" => [],
+ "conversion_rate" => 33.3,
+ "average_revenue" => %{"long" => "€0.00", "short" => "€0.0"},
+ "total_revenue" => %{"long" => "€0.00", "short" => "€0.0"}
+ }
+ ]
+ end
+
+ test "does not return revenue metrics if no revenue goals are returned", %{
+ conn: conn,
+ site: site
+ } do
+ populate_stats(site, [
+ build(:event, name: "Signup")
+ ])
+
+ insert(:goal, %{site: site, event_name: "Signup"})
+
+ conn = get(conn, "/api/stats/#{site.domain}/conversions?period=day")
+
+ assert json_response(conn, 200) == [
+ %{
+ "name" => "Signup",
+ "unique_conversions" => 1,
+ "total_conversions" => 1,
+ "prop_names" => [],
+ "conversion_rate" => 100.0
+ }
+ ]
+ end
end
describe "GET /api/stats/:domain/conversions - with goal filter" do
diff --git a/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs b/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs
index b077bf369..9d3d3d0e7 100644
--- a/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs
+++ b/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs
@@ -912,4 +912,84 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
assert 0 == Enum.sum(comparison_plot)
end
end
+
+ describe "GET /api/stats/main-graph - monetary metrics" do
+ setup [:create_user, :log_in, :create_new_site]
+
+ test "returns a plot of total_revenue for a monetary goal filter", %{
+ conn: conn,
+ site: site
+ } do
+ populate_stats(site, [
+ build(:event,
+ name: "Purchase",
+ revenue_reporting_amount: 30.0,
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:event,
+ name: "Purchase",
+ revenue_reporting_amount: 10.0,
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:event,
+ name: "Purchase",
+ revenue_reporting_amount: 10.0,
+ timestamp: ~N[2021-01-02 00:00:00]
+ )
+ ])
+
+ insert(:goal, %{site: site, event_name: "Purchase", currency: :EUR})
+ filters = Jason.encode!(%{goal: "Purchase"})
+
+ conn =
+ get(
+ conn,
+ "/api/stats/#{site.domain}/main-graph?period=custom&from=2021-01-01&to=2021-01-02&interval=date&metric=total_revenue&filters=#{filters}"
+ )
+
+ assert %{"plot" => plot} = json_response(conn, 200)
+
+ assert Enum.count(plot) == 2
+ assert List.first(plot) == 40.0
+ assert List.last(plot) == 10.0
+ end
+
+ test "returns a plot of average_revenue for a monetary goal filter", %{
+ conn: conn,
+ site: site
+ } do
+ populate_stats(site, [
+ build(:event,
+ name: "Purchase",
+ revenue_reporting_amount: 30.0,
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:event,
+ name: "Purchase",
+ revenue_reporting_amount: 10.0,
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:event,
+ name: "Purchase",
+ revenue_reporting_amount: 10.0,
+ timestamp: ~N[2021-01-02 00:00:00]
+ )
+ ])
+
+ insert(:goal, %{site: site, event_name: "Purchase", currency: :EUR})
+ filters = Jason.encode!(%{goal: "Purchase"})
+
+ conn =
+ get(
+ conn,
+ "/api/stats/#{site.domain}/main-graph?period=custom&from=2021-01-01&to=2021-01-02&interval=date&metric=average_revenue&filters=#{filters}"
+ )
+
+ assert %{"plot" => plot} = json_response(conn, 200)
+
+ assert Enum.count(plot) == 2
+ assert List.first(plot) == 20.0
+ assert List.last(plot) == 10.0
+ end
+ end
end
diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs
index e05221cde..6c1e1338f 100644
--- a/test/plausible_web/controllers/site_controller_test.exs
+++ b/test/plausible_web/controllers/site_controller_test.exs
@@ -658,6 +658,62 @@ defmodule PlausibleWeb.SiteControllerTest do
assert goal.page_path == nil
assert redirected_to(conn, 302) == "/#{site.domain}/settings/goals"
end
+
+ test "creates a custom event goal with a revenue value", %{conn: conn, site: site} do
+ conn =
+ post(conn, "/#{site.domain}/goals", %{
+ goal: %{
+ page_path: "",
+ event_name: "Purchase",
+ currency: "EUR"
+ }
+ })
+
+ goal = Repo.get_by(Plausible.Goal, site_id: site.id)
+
+ assert goal.event_name == "Purchase"
+ assert goal.page_path == nil
+ assert goal.currency == :EUR
+
+ assert redirected_to(conn, 302) == "/#{site.domain}/settings/goals"
+ end
+
+ test "fails to create a custom event goal with a non-existant currency", %{
+ conn: conn,
+ site: site
+ } do
+ conn =
+ post(conn, "/#{site.domain}/goals", %{
+ goal: %{
+ page_path: "",
+ event_name: "Purchase",
+ currency: "EEEE"
+ }
+ })
+
+ refute Repo.get_by(Plausible.Goal, site_id: site.id)
+
+ assert html_response(conn, 200) =~ "is invalid"
+ end
+
+ test "Cleans currency for pageview goal creation", %{conn: conn, site: site} do
+ conn =
+ post(conn, "/#{site.domain}/goals", %{
+ goal: %{
+ page_path: "/purchase",
+ event_name: "",
+ currency: "EUR"
+ }
+ })
+
+ goal = Repo.get_by(Plausible.Goal, site_id: site.id)
+
+ assert goal.event_name == nil
+ assert goal.page_path == "/purchase"
+ assert goal.currency == nil
+
+ assert redirected_to(conn, 302) == "/#{site.domain}/settings/goals"
+ end
end
describe "DELETE /:website/goals/:id" do
diff --git a/test/support/exchange_rate_mock.ex b/test/support/exchange_rate_mock.ex
new file mode 100644
index 000000000..6be2bdc70
--- /dev/null
+++ b/test/support/exchange_rate_mock.ex
@@ -0,0 +1,20 @@
+defmodule Plausible.ExchangeRateMock do
+ @moduledoc false
+ @behaviour Money.ExchangeRates
+
+ def init(config) do
+ config
+ end
+
+ def decode_rates(rates) do
+ Money.ExchangeRates.OpenExchangeRates.decode_rates(rates)
+ end
+
+ def get_latest_rates(_config) do
+ {:ok, %{BRL: Decimal.new("0.7"), EUR: Decimal.new("1.2"), USD: Decimal.new(1)}}
+ end
+
+ def get_historic_rates(_date, _config) do
+ {:ok, %{BRL: Decimal.new("0.8"), EUR: Decimal.new("1.3"), USD: Decimal.new(2)}}
+ end
+end
diff --git a/tracker/src/plausible.js b/tracker/src/plausible.js
index 4a0645a67..95b134718 100644
--- a/tracker/src/plausible.js
+++ b/tracker/src/plausible.js
@@ -76,6 +76,9 @@
if (options && options.props) {
payload.p = options.props
}
+ if (options && options.revenue) {
+ payload.$ = { currency: options.revenue.currency, amount: options.revenue.amount }
+ }
{{#if pageview_props}}
var propAttributes = scriptEl.getAttributeNames().filter(function (name) {