Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Add 7 days as an option

Uku Tahtf3985903b323
diff --git a/lib/plausible/stats/query.ex b/lib/plausible/stats/query.ex
index d9d8b80acd..5d596b8d83 100644
--- a/lib/plausible/stats/query.ex
+++ b/lib/plausible/stats/query.ex
@@ -51,6 +51,17 @@ defmodule Plausible.Stats.Query do
}
end
+ def from(tz, %{"period" => "7d"}) do
+ end_date = today(tz)
+ start_date = end_date |> Timex.shift(days: -7)
+
+ %__MODULE__{
+ period: "7d",
+ date_range: Date.range(start_date, end_date),
+ step_type: "date"
+ }
+ end
+
def from(_tz, %{"period" => "month", "date" => month_start}) do
start_date = Date.from_iso8601!(month_start) |> Timex.beginning_of_month
end_date = Timex.end_of_month(start_date)
diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex
index 907a0f8027..aeb90ea3aa 100644
--- a/lib/plausible_web/controllers/stats_controller.ex
+++ b/lib/plausible_web/controllers/stats_controller.ex
@@ -308,7 +308,7 @@ defmodule PlausibleWeb.StatsController do
defp fetch_period(conn, site) do
case conn.params["period"] do
- p when p in ["day", "month", "3mo", "6mo"] ->
+ p when p in ["day", "month", "7d", "3mo", "6mo"] ->
saved_periods = get_session(conn, :saved_periods) || %{}
{put_session(conn, :saved_periods, Map.merge(saved_periods, %{site.domain => p})), conn.params}
_ ->
diff --git a/lib/plausible_web/templates/stats/stats.html.eex b/lib/plausible_web/templates/stats/stats.html.eex
index 653b7d8b81..9c519d6d0d 100644
--- a/lib/plausible_web/templates/stats/stats.html.eex
+++ b/lib/plausible_web/templates/stats/stats.html.eex
@@ -46,9 +46,10 @@
</div>
<div data-dropdown style="top: 42px; right: 0px; width: 225px;" class="dropdown-content hidden absolute pin-r bg-white border border-grey-light rounded shadow z-10 font-bold text-sm text-grey-darker">
- <%= link("Today", to: "/#{@site.domain}?period=day&date=#{Timex.now(@site.timezone) |> Timex.format!("{ISOdate}")}", class: "block px-2 py-3 hover:bg-grey-lighter") %>
- <%= link("This month (#{this_month(@site) |> Timex.format!("{Mfull}")})", to: "/#{@site.domain}?period=month&date=#{this_month(@site) |> Timex.format!("{ISOdate}")}", class: "block p-2 hover:bg-grey-lighter") %>
- <%= link("Last month (#{last_month(@site) |> Timex.format!("{Mfull}")})", to: "/#{@site.domain}?period=month&date=#{last_month(@site) |> Timex.format!("{ISOdate}")}", class: "block p-2 hover:bg-grey-lighter") %>
+ <%= link("Today", to: "/#{@site.domain}?period=day", class: "block p-2 hover:bg-grey-lighter") %>
+ <%= link("Last 7 days", to: "/#{@site.domain}?period=7d", class: "block px-2 py-3 hover:bg-grey-lighter") %>
+ <%= link("This month", to: "/#{@site.domain}?period=month", class: "block p-2 hover:bg-grey-lighter") %>
+ <%= link("Last month", to: "/#{@site.domain}?period=month&date=#{last_month(@site) |> Timex.format!("{ISOdate}")}", class: "block p-2 hover:bg-grey-lighter") %>
<%= link("Last 3 months", to: "/#{@site.domain}?period=3mo", class: "block p-2 hover:bg-grey-lighter") %>
<%= link("Last 6 months", to: "/#{@site.domain}?period=6mo", class: "block p-2 hover:bg-grey-lighter") %>
</div>
diff --git a/lib/plausible_web/views/stats_view.ex b/lib/plausible_web/views/stats_view.ex
index d9544ba5dd..36e89294d3 100644
--- a/lib/plausible_web/views/stats_view.ex
+++ b/lib/plausible_web/views/stats_view.ex
@@ -36,6 +36,8 @@ defmodule PlausibleWeb.StatsView do
"on #{Timex.format!(query.date_range.first, "{Mfull} {D}")}"
"month" ->
"in #{Timex.format!(query.date_range.first, "{Mfull} {YYYY}")}"
+ "7d" ->
+ "in the last 7 days"
"3mo" ->
"in the last 3 months"
"6mo" ->
@@ -51,6 +53,8 @@ defmodule PlausibleWeb.StatsView do
"month" ->
date = Date.to_iso8601(query.date_range.first)
"?period=month&date=#{date}"
+ "7d" ->
+ "?period=7d"
"3mo" ->
"?period=3mo"
"6mo" ->
@@ -86,6 +90,8 @@ defmodule PlausibleWeb.StatsView do
else
Timex.format!(query.date_range.first, "{Mfull} {YYYY}")
end
+ "7d" ->
+ "Last 7 days"
"day" ->
if query.date_range.first == today(site) do
"Today"