diff --git a/config/config.exs b/config/config.exs index 62c1064ff..b4689c9c8 100644 --- a/config/config.exs +++ b/config/config.exs @@ -54,4 +54,8 @@ config :plausible, config :plausible, Plausible.Ingestion.Counters, enabled: true +config :ex_cldr, + default_locale: "en", + default_backend: Plausible.Cldr + import_config "#{config_env()}.exs" diff --git a/config/runtime.exs b/config/runtime.exs index 0a7594372..155a8d1e1 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -320,6 +320,9 @@ config :plausible, Plausible.ImportDeletionRepo, transport_opts: ch_transport_opts, pool_size: 1 +config :ex_money, + open_exchange_rates_app_id: get_var_from_path_or_env(config_dir, "OPEN_EXCHANGE_RATES_APP_ID") + case mailer_adapter do "Bamboo.PostmarkAdapter" -> config :plausible, Plausible.Mailer, diff --git a/config/test.exs b/config/test.exs index 02fdfef5e..5717ca71f 100644 --- a/config/test.exs +++ b/config/test.exs @@ -29,4 +29,6 @@ config :plausible, http_impl: Plausible.HTTPClient.Mock, sites_by_domain_cache_enabled: false +config :ex_money, api_module: Plausible.ExchangeRateMock + config :plausible, Plausible.Ingestion.Counters, enabled: false diff --git a/lib/plausible/cldr.ex b/lib/plausible/cldr.ex new file mode 100644 index 000000000..0a04142a5 --- /dev/null +++ b/lib/plausible/cldr.ex @@ -0,0 +1,5 @@ +defmodule Plausible.Cldr do + @moduledoc false + + use Cldr, locales: ["en"], providers: [Cldr.Number] +end diff --git a/lib/plausible/goal/schema.ex b/lib/plausible/goal/schema.ex index 7530807d5..02a58ad1d 100644 --- a/lib/plausible/goal/schema.ex +++ b/lib/plausible/goal/schema.ex @@ -24,20 +24,40 @@ defmodule Plausible.Goal do field :event_name, :string field :page_path, :string + field :currency, Ecto.Enum, values: Money.Currency.known_current_currencies() + belongs_to :site, Plausible.Site timestamps() end + def revenue?(%__MODULE__{currency: currency}) do + !!currency + end + + def valid_currencies do + Ecto.Enum.dump_values(__MODULE__, :currency) + end + + def currency_options do + options = + for code <- valid_currencies() do + {"#{code} - #{Cldr.Currency.display_name!(code)}", code} + end + + [{"", nil}] ++ options + end + def changeset(goal, attrs \\ %{}) do goal - |> cast(attrs, [:site_id, :event_name, :page_path]) + |> cast(attrs, [:site_id, :event_name, :page_path, :currency]) |> validate_required([:site_id]) |> cast_assoc(:site) |> validate_event_name_and_page_path() |> update_change(:event_name, &String.trim/1) |> update_change(:page_path, &String.trim/1) |> validate_length(:event_name, max: 120) + |> maybe_drop_currency() end defp validate_event_name_and_page_path(changeset) do @@ -59,4 +79,12 @@ defmodule Plausible.Goal do value = get_field(changeset, :event_name) value && String.match?(value, ~r/^.+/) end + + defp maybe_drop_currency(changeset) do + if get_field(changeset, :page_path) do + delete_change(changeset, :currency) + else + changeset + end + end end diff --git a/lib/plausible/site.ex b/lib/plausible/site.ex index 661e83312..513a20fd8 100644 --- a/lib/plausible/site.ex +++ b/lib/plausible/site.ex @@ -31,6 +31,7 @@ defmodule Plausible.Site do many_to_many :members, User, join_through: Plausible.Site.Membership has_many :memberships, Plausible.Site.Membership has_many :invitations, Plausible.Auth.Invitation + has_many :revenue_goals, Plausible.Goal, where: [currency: {:not, nil}] has_one :google_auth, GoogleAuth has_one :weekly_report, Plausible.Site.WeeklyReport has_one :monthly_report, Plausible.Site.MonthlyReport diff --git a/lib/plausible/site/cache.ex b/lib/plausible/site/cache.ex index 0be5e894f..d4108eccf 100644 --- a/lib/plausible/site/cache.ex +++ b/lib/plausible/site/cache.ex @@ -51,11 +51,11 @@ defmodule Plausible.Site.Cache do @modes [:all, :updated_recently] @cached_schema_fields ~w( - id - domain - domain_changed_from - ingest_rate_limit_scale_seconds - ingest_rate_limit_threshold + id + domain + domain_changed_from + ingest_rate_limit_scale_seconds + ingest_rate_limit_threshold )a @type t() :: Site.t() @@ -92,17 +92,9 @@ defmodule Plausible.Site.Cache do @spec refresh_all(Keyword.t()) :: :ok def refresh_all(opts \\ []) do - sites_by_domain_query = - from s in Site, - select: { - s.domain, - s.domain_changed_from, - %{struct(s, ^@cached_schema_fields) | from_cache?: true} - } - refresh( :all, - sites_by_domain_query, + sites_by_domain_query(), Keyword.put(opts, :delete_stale_items?, true) ) end @@ -110,14 +102,9 @@ defmodule Plausible.Site.Cache do @spec refresh_updated_recently(Keyword.t()) :: :ok def refresh_updated_recently(opts \\ []) do recently_updated_sites_query = - from s in Site, + from [s, mg] in sites_by_domain_query(), order_by: [asc: s.updated_at], - where: s.updated_at > ago(^15, "minute"), - select: { - s.domain, - s.domain_changed_from, - %{struct(s, ^@cached_schema_fields) | from_cache?: true} - } + where: s.updated_at > ago(^15, "minute") or mg.updated_at > ago(^15, "minute") refresh( :updated_recently, @@ -126,6 +113,17 @@ defmodule Plausible.Site.Cache do ) end + defp sites_by_domain_query do + from s in Site, + left_join: mg in assoc(s, :revenue_goals), + select: { + s.domain, + s.domain_changed_from, + %{struct(s, ^@cached_schema_fields) | from_cache?: true} + }, + preload: [revenue_goals: mg] + end + @spec merge(new_items :: [Site.t()], opts :: Keyword.t()) :: :ok def merge(new_items, opts \\ []) def merge([], _), do: :ok @@ -182,7 +180,16 @@ defmodule Plausible.Site.Cache do nil end else - Plausible.Sites.get_by_domain(domain) + get_from_source(domain) + end + end + + defp get_from_source(domain) do + query = from s in sites_by_domain_query(), where: s.domain == ^domain + + case Plausible.Repo.one(query) do + {_, _, site} -> %Site{site | from_cache?: false} + _any -> nil end end diff --git a/lib/plausible_web/templates/site/new_goal.html.eex b/lib/plausible_web/templates/site/new_goal.html.eex index 70d35fba8..0417c5a30 100644 --- a/lib/plausible_web/templates/site/new_goal.html.eex +++ b/lib/plausible_web/templates/site/new_goal.html.eex @@ -7,9 +7,19 @@
- <%= 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 %> +
+ + <%= if FunWithFlags.enabled?(:revenue_goals, for: @current_user) do %> +
+ <%= 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 %> +
+ <% end %>