2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.StatsView do
|
|
|
|
use PlausibleWeb, :view
|
2023-11-29 13:04:54 +03:00
|
|
|
use Plausible
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2020-05-26 16:09:34 +03:00
|
|
|
def plausible_url do
|
2020-10-05 15:01:54 +03:00
|
|
|
PlausibleWeb.Endpoint.url()
|
2020-05-26 16:09:34 +03:00
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
def large_number_format(n) do
|
|
|
|
cond do
|
|
|
|
n >= 1_000 && n < 1_000_000 ->
|
|
|
|
thousands = trunc(n / 100) / 10
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
if thousands == trunc(thousands) || n >= 100_000 do
|
|
|
|
"#{trunc(thousands)}k"
|
|
|
|
else
|
|
|
|
"#{thousands}k"
|
|
|
|
end
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2021-03-16 13:03:50 +03:00
|
|
|
n >= 1_000_000 && n < 1_000_000_000 ->
|
2019-09-02 14:29:19 +03:00
|
|
|
millions = trunc(n / 100_000) / 10
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2021-03-24 12:50:15 +03:00
|
|
|
if millions == trunc(millions) || n > 100_000_000 do
|
2021-03-24 13:08:05 +03:00
|
|
|
"#{trunc(millions)}M"
|
2019-09-02 14:29:19 +03:00
|
|
|
else
|
2021-03-24 13:08:05 +03:00
|
|
|
"#{millions}M"
|
|
|
|
end
|
|
|
|
|
|
|
|
n >= 1_000_000_000 && n < 1_000_000_000_000 ->
|
|
|
|
billions = trunc(n / 100_000_000) / 10
|
|
|
|
|
|
|
|
if billions == trunc(billions) || n > 100_000_000_000 do
|
|
|
|
"#{trunc(billions)}B"
|
|
|
|
else
|
|
|
|
"#{billions}B"
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
true ->
|
|
|
|
Integer.to_string(n)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-01 11:22:04 +03:00
|
|
|
def stats_container_class(conn) do
|
|
|
|
cond do
|
2024-03-18 11:37:42 +03:00
|
|
|
conn.assigns[:embedded] && conn.params["width"] == "manual" -> "px-6"
|
2023-03-14 13:47:07 +03:00
|
|
|
conn.assigns[:embedded] -> "max-w-screen-xl mx-auto px-6"
|
2024-02-07 10:57:39 +03:00
|
|
|
!conn.assigns[:embedded] -> "container print:max-w-full"
|
2022-09-01 11:22:04 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-28 10:42:15 +03:00
|
|
|
@doc """
|
|
|
|
Returns a readable stats URL.
|
|
|
|
|
|
|
|
Native Phoenix router functions percent-encode all diacritics, resulting in
|
|
|
|
ugly URLs, e.g. `https://plausible.io/café.com` transforms into
|
|
|
|
`https://plausible.io/caf%C3%A9.com`.
|
|
|
|
|
|
|
|
This function encodes only the slash (`/`) character from the site's domain.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> PlausibleWeb.StatsView.pretty_stats_url(%Plausible.Site{domain: "user.gittea.io/repo"})
|
|
|
|
"http://localhost:8000/user.gittea.io%2Frepo"
|
|
|
|
|
|
|
|
iex> PlausibleWeb.StatsView.pretty_stats_url(%Plausible.Site{domain: "anakin.test"})
|
|
|
|
"http://localhost:8000/anakin.test"
|
|
|
|
|
|
|
|
iex> PlausibleWeb.StatsView.pretty_stats_url(%Plausible.Site{domain: "café.test"})
|
|
|
|
"http://localhost:8000/café.test"
|
|
|
|
|
|
|
|
"""
|
|
|
|
def pretty_stats_url(%Plausible.Site{domain: domain}) when is_binary(domain) do
|
|
|
|
pretty_domain = String.replace(domain, "/", "%2F")
|
|
|
|
"#{plausible_url()}/#{pretty_domain}"
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|