Show percentages for most stats

pull/18/head
Uku Taht 7 years ago
parent 9f2cdea503
commit 0ae5448a2b

@ -129,27 +129,36 @@ defmodule Plausible.Stats do
@available_screen_sizes ["Desktop", "Laptop", "Tablet", "Mobile"]
def top_screen_sizes(site, query) do
Repo.all(from e in base_query(site, query),
sizes = Repo.all(from e in base_query(site, query),
select: {e.screen_size, count(e.screen_size)},
group_by: e.screen_size,
where: e.new_visitor == true and not is_nil(e.screen_size)
) |> Enum.sort(fn {screen_size1, _}, {screen_size2, _} ->
)
|> Enum.sort(fn {screen_size1, _}, {screen_size2, _} ->
index1 = Enum.find_index(@available_screen_sizes, fn s -> s == screen_size1 end)
index2 = Enum.find_index(@available_screen_sizes, fn s -> s == screen_size2 end)
index2 > index1
end)
|> add_percentages
end
defp add_percentages(stat_list) do
total = Enum.reduce(stat_list, 0, fn {_, count}, total -> total + count end)
Enum.map(stat_list, fn {stat, count} -> {stat, count, round(count / total * 100)} end)
end
def countries(site, query, limit \\ 5) do
Repo.all(from e in base_query(site, query),
countries = Repo.all(from e in base_query(site, query),
select: {e.country_code, count(e.country_code)},
group_by: e.country_code,
where: e.new_visitor == true and not is_nil(e.country_code),
order_by: [desc: count(e.country_code)],
limit: ^limit
) |> Enum.map(fn {country_code, count} ->
order_by: [desc: count(e.country_code)]
)
|> Enum.map(fn {country_code, count} ->
{Plausible.Stats.CountryName.from_iso3166(country_code), count}
end)
|> add_percentages
|> Enum.take(limit)
end
def browsers(site, query, limit \\ 5) do
@ -157,9 +166,10 @@ defmodule Plausible.Stats do
select: {e.browser, count(e.browser)},
group_by: e.browser,
where: e.new_visitor == true and not is_nil(e.browser),
order_by: [desc: count(e.browser)],
limit: ^limit
order_by: [desc: count(e.browser)]
)
|> add_percentages
|> Enum.take(limit)
end
def operating_systems(site, query, limit \\ 5) do
@ -167,9 +177,10 @@ defmodule Plausible.Stats do
select: {e.operating_system, count(e.operating_system)},
group_by: e.operating_system,
where: e.new_visitor == true and not is_nil(e.operating_system),
order_by: [desc: count(e.operating_system)],
limit: ^limit
order_by: [desc: count(e.operating_system)]
)
|> add_percentages
|> Enum.take(limit)
end
def current_visitors(site) do

@ -6,10 +6,10 @@
<div class="my-4 border-b border-grey-light"></div>
<main class="modal__content" id="modal-1-content">
<div class="mt-8">
<%= for {browser, count} <- @browsers do %>
<%= for {browser, count, percentage} <- @browsers do %>
<div class="flex items-center justify-between my-2">
<span><%= browser %></span>
<span title="<%= count %>"><%= large_number_format(count) %></span>
<span tooltip="<%= count %> visitors"><%= percentage %>%</span>
</div>
<%= bar(count, @browsers, :red) %>
<% end %>

@ -4,10 +4,10 @@
</div>
<div class="mt-8">
<%= for {key, count} <- @browsers do %>
<%= for {key, count, percentage} <- @browsers do %>
<div class="flex items-center justify-between my-2">
<span class="truncate" style="max-width: 80%;"><%= key %></span>
<span title="<%= count %>"><%= large_number_format(count) %></span>
<span tooltip="<%= count %> visitors"><%= percentage %>%</span>
</div>
<%= bar(count, @browsers, :red) %>
<% end %>

@ -6,10 +6,10 @@
<div class="my-4 border-b border-grey-light"></div>
<main class="modal__content">
<div class="mt-8">
<%= for {country, count} <- @countries do %>
<%= for {country, count, percentage} <- @countries do %>
<div class="flex items-center justify-between my-2">
<span><%= Plausible.Stats.CountryName.from_iso3166(country) %></span>
<span title="<%= count %>"><%= large_number_format(count) %></span>
<span><%= country %></span>
<span tooltip="<%= count %> visitors"><%= percentage %>%</span>
</div>
<%= bar(count, @countries, :indigo) %>
<% end %>

@ -4,10 +4,10 @@
</div>
<div class="mt-8">
<%= for {key, count} <- @top_countries do %>
<%= for {key, count, percentage} <- @top_countries do %>
<div class="flex items-center justify-between my-2">
<span class="truncate" style="max-width: 80%;"><%= key %></span>
<span title="<%= count %>"><%= large_number_format(count) %></span>
<span tooltip="<%= count %> visitors"><%= percentage %>%</span>
</div>
<%= bar(count, @top_countries, :indigo) %>
<% end %>

@ -6,10 +6,10 @@
<div class="my-4 border-b border-grey-light"></div>
<main class="modal__content">
<div class="mt-8">
<%= for {os, count} <- @operating_systems do %>
<%= for {os, count, percentage} <- @operating_systems do %>
<div class="flex items-center justify-between my-2">
<span><%= os %></span>
<span><%= count %></span>
<span tooltip="<%= count %> visitors"><%= percentage %>%</span>
</div>
<%= bar(count, @operating_systems, :blue) %>
<% end %>

@ -4,10 +4,10 @@
</div>
<div class="mt-8">
<%= for {key, count} <- @operating_systems do %>
<%= for {key, count, percentage} <- @operating_systems do %>
<div class="flex items-center justify-between my-2">
<span class="truncate" style="max-width: 80%;"><%= key %></span>
<span title="<%= count %>"><%= large_number_format(count) %></span>
<span tooltip="<%= count %> visitors"><%= percentage %>%</span>
</div>
<%= bar(count, @operating_systems, :blue) %>
<% end %>

@ -3,13 +3,13 @@
<div class="text-grey-darker mt-1">by visitors</div>
</div>
<div class="mt-8">
<%= for {screen_size, count} <- @top_screen_sizes do %>
<%= for {screen_size, count, percentage} <- @top_screen_sizes do %>
<div class="flex items-center justify-between my-2">
<span tooltip="<%= explanation_for(screen_size) %>">
<%= icon_for(screen_size) %>
<span class="ml-1 cursor-default"><%= screen_size %></span>
</span>
<span title="<%= count %>"><%= large_number_format(count) %></span>
<span tooltip="<%= count %> visitors"><%= percentage %>%</span>
</div>
<%= bar(count, @top_screen_sizes, :teal) %>
<% end %>

@ -104,7 +104,11 @@ defmodule PlausibleWeb.StatsView do
end
defp bar_width(count, all) do
max = Enum.max_by(all, fn {_, count} -> count end) |> elem(1)
max = Enum.max_by(all, fn
{_, count} -> count
{_, count, _} -> count
end) |> elem(1)
count / max * 100
end

@ -53,7 +53,7 @@ defmodule Plausible.StatsTest do
end
describe "top_screen_sizes" do
test "shows top screen sizes by new visitors" do
test "shows top screen sizes by new visitors and percentages" do
site = insert(:site)
insert(:pageview, hostname: site.domain, screen_size: "desktop", new_visitor: true, timestamp: ~N[2019-01-01 01:00:00])
insert(:pageview, hostname: site.domain, screen_size: "desktop", new_visitor: true, timestamp: ~N[2019-01-01 02:00:00])
@ -61,14 +61,14 @@ defmodule Plausible.StatsTest do
query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"})
assert Stats.top_screen_sizes(site, query) == [
{"mobile", 1},
{"desktop", 2}
{"mobile", 1, 33},
{"desktop", 2, 67}
]
end
end
describe "countries" do
test "shows top countries by new visitors" do
test "shows top countries by new visitors and percentages" do
site = insert(:site)
insert(:pageview, hostname: site.domain, country_code: "EE", new_visitor: true, timestamp: ~N[2019-01-01 01:00:00])
insert(:pageview, hostname: site.domain, country_code: "EE", new_visitor: true, timestamp: ~N[2019-01-01 02:00:00])
@ -76,8 +76,8 @@ defmodule Plausible.StatsTest do
query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"})
assert Stats.countries(site, query) == [
{"Estonia", 2},
{"United Kingdom", 1}
{"Estonia", 2, 67},
{"United Kingdom", 1, 33}
]
end
end
@ -91,14 +91,14 @@ defmodule Plausible.StatsTest do
query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"})
assert Stats.browsers(site, query) == [
{"Chrome", 2},
{"Safari", 1}
{"Chrome", 2, 67},
{"Safari", 1, 33}
]
end
end
describe "operating_systems" do
test "shows top browsers by new visitors" do
test "shows top operating systems by new visitors" do
site = insert(:site)
insert(:pageview, hostname: site.domain, operating_system: "Mac", new_visitor: true, timestamp: ~N[2019-01-01 01:00:00])
insert(:pageview, hostname: site.domain, operating_system: "Windows", new_visitor: true, timestamp: ~N[2019-01-01 02:00:00])
@ -106,8 +106,8 @@ defmodule Plausible.StatsTest do
query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"})
assert Stats.operating_systems(site, query) == [
{"Windows", 2},
{"Mac", 1}
{"Windows", 2, 67},
{"Mac", 1, 33}
]
end
end

Loading…
Cancel
Save