Small bugfix + refactor email reports (#3642)
* use more convenient testing functions * do not display + sign with 0% change in emails * Rename module/file/function names before, `weekly_report` was also used for monthly reports and that was a bit confusing to read in code. * Refactor send_email_report.ex This commit improves readability by refactoring the code into smaller functions and reducing the number of arguments given to functions. But more importantly, it stops making duplicate stats queries for every email recipient by moving the queries out of the for loop. * Refactor: move querying logic out of the worker module and merge all stats information under a single `stats` assign.pull/3651/head r2023.12.21-295
parent
2bbc063b6c
commit
d2270f3c35
@ -0,0 +1,53 @@
|
||||
defmodule Plausible.Stats.EmailReport do
|
||||
@moduledoc """
|
||||
This module exposes a `get/2` function that returns a map
|
||||
of stats needed for email reports. These stats include:
|
||||
|
||||
* Total pageviews
|
||||
* Unique visitors
|
||||
* Bounce rate
|
||||
* A list of Top 5 sources (excluding "Direct / None")
|
||||
* A list of Top 5 pages
|
||||
|
||||
where total pageviews, unique visitors, and bounce rate
|
||||
also include the change compared to previous period.
|
||||
"""
|
||||
|
||||
alias Plausible.Stats
|
||||
alias Plausible.Stats.{Query, Compare, Comparisons}
|
||||
|
||||
def get(site, query) do
|
||||
metrics = [:pageviews, :visitors, :bounce_rate]
|
||||
|
||||
Stats.aggregate(site, query, metrics)
|
||||
|> with_comparisons(site, query, metrics)
|
||||
|> put_top_5_pages(site, query)
|
||||
|> put_top_5_sources(site, query)
|
||||
end
|
||||
|
||||
defp with_comparisons(stats, site, query, metrics) do
|
||||
{:ok, prev_query} = Comparisons.compare(site, query, "previous_period")
|
||||
prev_period_stats = Stats.aggregate(site, prev_query, metrics)
|
||||
|
||||
stats
|
||||
|> Enum.map(fn {metric, %{value: value}} ->
|
||||
%{value: prev_value} = Map.fetch!(prev_period_stats, metric)
|
||||
change = Compare.calculate_change(metric, prev_value, value)
|
||||
|
||||
{metric, %{value: value, change: change}}
|
||||
end)
|
||||
|> Enum.into(%{})
|
||||
end
|
||||
|
||||
defp put_top_5_pages(stats, site, query) do
|
||||
pages = Stats.breakdown(site, query, "event:page", [:visitors], {5, 1})
|
||||
Map.put(stats, :pages, pages)
|
||||
end
|
||||
|
||||
defp put_top_5_sources(stats, site, query) do
|
||||
query = Query.put_filter(query, "visit:source", {:is_not, "Direct / None"})
|
||||
sources = Stats.breakdown(site, query, "visit:source", [:visitors], {5, 1})
|
||||
|
||||
Map.put(stats, :sources, sources)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,7 @@
|
||||
defmodule PlausibleWeb.MJML.StatsReport do
|
||||
@moduledoc """
|
||||
MJML rendered for the weekly or monthly report e-mail
|
||||
"""
|
||||
|
||||
use MjmlEEx, mjml_template: "templates/stats_report.mjml.eex"
|
||||
end
|
||||
@ -1,7 +0,0 @@
|
||||
defmodule PlausibleWeb.MJML.WeeklyReport do
|
||||
@moduledoc """
|
||||
MJML rendered for the weekly report e-mail
|
||||
"""
|
||||
|
||||
use MjmlEEx, mjml_template: "templates/weekly_report.mjml.eex"
|
||||
end
|
||||
Loading…
Reference in New Issue