Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Show percentages for most stats
Uku Taht0ae5448a2b3d
diff --git a/lib/plausible/stats/stats.ex b/lib/plausible/stats/stats.exindex 6dc5060773..2186cbb506 100644--- a/lib/plausible/stats/stats.ex+++ b/lib/plausible/stats/stats.ex@@ -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 > index1end)+ |> 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)enddef 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)enddef browsers(site, query, limit \\ 5) do@@ -157,9 +166,10 @@ defmodule Plausible.Stats doselect: {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)enddef operating_systems(site, query, limit \\ 5) do@@ -167,9 +177,10 @@ defmodule Plausible.Stats doselect: {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)enddef current_visitors(site) dodiff --git a/lib/plausible_web/templates/stats/browsers.html.eex b/lib/plausible_web/templates/stats/browsers.html.eexindex c998c3b8da..8051218be4 100644--- a/lib/plausible_web/templates/stats/browsers.html.eex+++ b/lib/plausible_web/templates/stats/browsers.html.eex@@ -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 %>diff --git a/lib/plausible_web/templates/stats/browsers_preview.html.eex b/lib/plausible_web/templates/stats/browsers_preview.html.eexindex afd88614c2..e0f61b633b 100644--- a/lib/plausible_web/templates/stats/browsers_preview.html.eex+++ b/lib/plausible_web/templates/stats/browsers_preview.html.eex@@ -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 %>diff --git a/lib/plausible_web/templates/stats/countries.html.eex b/lib/plausible_web/templates/stats/countries.html.eexindex 03a1356cde..ebaf7f9075 100644--- a/lib/plausible_web/templates/stats/countries.html.eex+++ b/lib/plausible_web/templates/stats/countries.html.eex@@ -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 %>diff --git a/lib/plausible_web/templates/stats/countries_preview.html.eex b/lib/plausible_web/templates/stats/countries_preview.html.eexindex 80b430fc61..730b43bec2 100644--- a/lib/plausible_web/templates/stats/countries_preview.html.eex+++ b/lib/plausible_web/templates/stats/countries_preview.html.eex@@ -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 %>diff --git a/lib/plausible_web/templates/stats/operating_systems.html.eex b/lib/plausible_web/templates/stats/operating_systems.html.eexindex 6e4a64e96c..51b1c0b1e2 100644--- a/lib/plausible_web/templates/stats/operating_systems.html.eex+++ b/lib/plausible_web/templates/stats/operating_systems.html.eex@@ -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 %>diff --git a/lib/plausible_web/templates/stats/operating_systems_preview.html.eex b/lib/plausible_web/templates/stats/operating_systems_preview.html.eexindex 196cb1c3ce..db2e75e9d7 100644--- a/lib/plausible_web/templates/stats/operating_systems_preview.html.eex+++ b/lib/plausible_web/templates/stats/operating_systems_preview.html.eex@@ -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 %>diff --git a/lib/plausible_web/templates/stats/screen_sizes_preview.html.eex b/lib/plausible_web/templates/stats/screen_sizes_preview.html.eexindex 775e9662bd..20f7a715b6 100644--- a/lib/plausible_web/templates/stats/screen_sizes_preview.html.eex+++ b/lib/plausible_web/templates/stats/screen_sizes_preview.html.eex@@ -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 %>diff --git a/lib/plausible_web/views/stats_view.ex b/lib/plausible_web/views/stats_view.exindex 36e89294d3..4385a3bda3 100644--- a/lib/plausible_web/views/stats_view.ex+++ b/lib/plausible_web/views/stats_view.ex@@ -104,7 +104,11 @@ defmodule PlausibleWeb.StatsView doenddefp 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 * 100enddiff --git a/test/plausible/stats/stats_test.exs b/test/plausible/stats/stats_test.exsindex 306375c2c8..2af11c42b3 100644--- a/test/plausible/stats/stats_test.exs+++ b/test/plausible/stats/stats_test.exs@@ -53,7 +53,7 @@ defmodule Plausible.StatsTest doenddescribe "top_screen_sizes" do- test "shows top screen sizes by new visitors" do+ test "shows top screen sizes by new visitors and percentages" dosite = 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 doquery = 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}]endenddescribe "countries" do- test "shows top countries by new visitors" do+ test "shows top countries by new visitors and percentages" dosite = 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 doquery = 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}]endend@@ -91,14 +91,14 @@ defmodule Plausible.StatsTest doquery = 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}]endenddescribe "operating_systems" do- test "shows top browsers by new visitors" do+ test "shows top operating systems by new visitors" dosite = 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 doquery = 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}]endend