Remember timeframe period for next time

This commit is contained in:
Uku Taht 2019-09-02 20:12:30 +01:00
parent d065f33bbd
commit dc201f0b57
4 changed files with 78 additions and 22 deletions

View File

@ -41,6 +41,16 @@ defmodule Plausible.Stats.Query do
} }
end end
def from(tz, %{"period" => "day"}) do
date = today(tz)
%__MODULE__{
period: "day",
date_range: Date.range(date, date),
step_type: "hour"
}
end
def from(_tz, %{"period" => "month", "date" => month_start}) do def from(_tz, %{"period" => "month", "date" => month_start}) do
start_date = Date.from_iso8601!(month_start) |> Timex.beginning_of_month start_date = Date.from_iso8601!(month_start) |> Timex.beginning_of_month
end_date = Timex.end_of_month(start_date) end_date = Timex.end_of_month(start_date)

View File

@ -21,7 +21,8 @@ defmodule PlausibleWeb.StatsController do
Plausible.Tracking.event(conn, "Site Analytics: Open", %{demo: demo}) Plausible.Tracking.event(conn, "Site Analytics: Open", %{demo: demo})
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
conn conn
|> assign(:skip_plausible_tracking, !demo) |> assign(:skip_plausible_tracking, !demo)
@ -43,7 +44,8 @@ defmodule PlausibleWeb.StatsController do
def browsers_preview(conn, %{"domain" => domain}) do def browsers_preview(conn, %{"domain" => domain}) do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
render(conn, render(conn,
@ -60,7 +62,8 @@ defmodule PlausibleWeb.StatsController do
def operating_systems_preview(conn, %{"domain" => domain}) do def operating_systems_preview(conn, %{"domain" => domain}) do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
render(conn, render(conn,
@ -77,7 +80,8 @@ defmodule PlausibleWeb.StatsController do
def screen_sizes_preview(conn, %{"domain" => domain}) do def screen_sizes_preview(conn, %{"domain" => domain}) do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
render(conn, render(conn,
@ -94,7 +98,8 @@ defmodule PlausibleWeb.StatsController do
def referrers_preview(conn, %{"domain" => domain}) do def referrers_preview(conn, %{"domain" => domain}) do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
render(conn, render(conn,
@ -111,7 +116,8 @@ defmodule PlausibleWeb.StatsController do
def pages_preview(conn, %{"domain" => domain}) do def pages_preview(conn, %{"domain" => domain}) do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
render(conn, render(conn,
@ -128,7 +134,8 @@ defmodule PlausibleWeb.StatsController do
def countries_preview(conn, %{"domain" => domain}) do def countries_preview(conn, %{"domain" => domain}) do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
render(conn, render(conn,
@ -145,7 +152,8 @@ defmodule PlausibleWeb.StatsController do
def main_graph(conn, %{"domain" => domain}) do def main_graph(conn, %{"domain" => domain}) do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
plot_task = Task.async(fn -> Stats.calculate_plot(site, query) end) plot_task = Task.async(fn -> Stats.calculate_plot(site, query) end)
{pageviews, visitors} = Stats.pageviews_and_visitors(site, query) {pageviews, visitors} = Stats.pageviews_and_visitors(site, query)
@ -163,7 +171,8 @@ defmodule PlausibleWeb.StatsController do
def compare(conn, %{"domain" => domain}) do def compare(conn, %{"domain" => domain}) do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
{pageviews, ""} = Integer.parse(conn.params["pageviews"]) {pageviews, ""} = Integer.parse(conn.params["pageviews"])
{unique_visitors, ""} = Integer.parse(conn.params["unique_visitors"]) {unique_visitors, ""} = Integer.parse(conn.params["unique_visitors"])
@ -181,7 +190,8 @@ defmodule PlausibleWeb.StatsController do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
referrers = Stats.top_referrers(site, query, 100) referrers = Stats.top_referrers(site, query, 100)
render(conn, "referrers.html", layout: false, site: site, top_referrers: referrers, query: query) render(conn, "referrers.html", layout: false, site: site, top_referrers: referrers, query: query)
@ -195,7 +205,8 @@ defmodule PlausibleWeb.StatsController do
|> Repo.preload(:google_auth) |> Repo.preload(:google_auth)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
total_visitors = Stats.visitors_from_referrer(site, query, "Google") total_visitors = Stats.visitors_from_referrer(site, query, "Google")
search_terms = if site.google_auth do search_terms = if site.google_auth do
Plausible.Google.Api.fetch_stats(site, site.google_auth, query) Plausible.Google.Api.fetch_stats(site, site.google_auth, query)
@ -217,7 +228,8 @@ defmodule PlausibleWeb.StatsController do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
referrers = Stats.referrer_drilldown(site, query, referrer) referrers = Stats.referrer_drilldown(site, query, referrer)
total_visitors = Stats.visitors_from_referrer(site, query, referrer) total_visitors = Stats.visitors_from_referrer(site, query, referrer)
@ -231,7 +243,8 @@ defmodule PlausibleWeb.StatsController do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
pages = Stats.top_pages(site, query, 100) pages = Stats.top_pages(site, query, 100)
render(conn, "pages.html", layout: false, site: site, top_pages: pages) render(conn, "pages.html", layout: false, site: site, top_pages: pages)
@ -244,7 +257,8 @@ defmodule PlausibleWeb.StatsController do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
countries = Stats.countries(site, query, 100) countries = Stats.countries(site, query, 100)
render(conn, "countries.html", layout: false, site: site, countries: countries) render(conn, "countries.html", layout: false, site: site, countries: countries)
@ -257,7 +271,8 @@ defmodule PlausibleWeb.StatsController do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
operating_systems = Stats.operating_systems(site, query, 100) operating_systems = Stats.operating_systems(site, query, 100)
render(conn, "operating_systems.html", layout: false, site: site, operating_systems: operating_systems) render(conn, "operating_systems.html", layout: false, site: site, operating_systems: operating_systems)
@ -270,7 +285,8 @@ defmodule PlausibleWeb.StatsController do
site = Repo.get_by(Plausible.Site, domain: domain) site = Repo.get_by(Plausible.Site, domain: domain)
if site && current_user_can_access?(conn, site) do if site && current_user_can_access?(conn, site) do
query = Stats.Query.from(site.timezone, conn.params) {conn, params} = fetch_period(conn, site)
query = Stats.Query.from(site.timezone, params)
browsers = Stats.browsers(site, query, 100) browsers = Stats.browsers(site, query, 100)
render(conn, "browsers.html", layout: false, site: site, browsers: browsers) render(conn, "browsers.html", layout: false, site: site, browsers: browsers)
@ -289,4 +305,21 @@ defmodule PlausibleWeb.StatsController do
user -> Plausible.Sites.is_owner?(user.id, site) user -> Plausible.Sites.is_owner?(user.id, site)
end end
end end
defp fetch_period(conn, site) do
case conn.params["period"] do
p when p in ["day", "month", "3mo", "6mo"] ->
saved_periods = get_session(conn, :saved_periods) || %{}
{put_session(conn, :saved_periods, Map.merge(saved_periods, %{site.domain => p})), conn.params}
_ ->
saved_period = (get_session(conn, :saved_periods) || %{})[site.domain]
if saved_period do
{conn, %{"period" => saved_period}}
else
{conn, conn.params}
end
end
end
end end

View File

@ -39,14 +39,14 @@
<div class="relative" style="height: 35.5px; width: 180px;"> <div class="relative" style="height: 35.5px; width: 180px;">
<div data-dropdown-trigger class="flex items-center justify-between hover:bg-grey-lighter rounded bg-white shadow px-4 pr-3 py-2 leading-tight cursor-pointer text-sm font-bold text-grey-darker h-full"> <div data-dropdown-trigger class="flex items-center justify-between hover:bg-grey-lighter rounded bg-white shadow px-4 pr-3 py-2 leading-tight cursor-pointer text-sm font-bold text-grey-darker h-full">
<span class="mr-2"><%= timeframe_text(@query) %></span> <span class="mr-2"><%= timeframe_text(@site, @query) %></span>
<svg class="text-pink fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"> <svg class="text-pink fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<use xlink:href="#feather-chevron-down" /> <use xlink:href="#feather-chevron-down" />
</svg> </svg>
</div> </div>
<div data-dropdown style="top: 42px; right: 0px; width: 185px;" class="dropdown-content hidden absolute pin-r bg-white border border-grey-light rounded shadow-md z-10 font-bold text-sm text-grey-darker"> <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 p-2 hover:bg-grey-lighter") %> <%= 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("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("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("Last 3 months", to: "/#{@site.domain}?period=3mo", 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") %>

View File

@ -58,6 +58,11 @@ defmodule PlausibleWeb.StatsView do
end end
end end
def today(site) do
Timex.now(site.timezone)
|> DateTime.to_date
end
def this_month(site) do def this_month(site) do
Timex.now(site.timezone) Timex.now(site.timezone)
|> DateTime.to_date |> DateTime.to_date
@ -69,16 +74,24 @@ defmodule PlausibleWeb.StatsView do
|> Timex.shift(months: -1) |> Timex.shift(months: -1)
end end
def timeframe_text(query) do def timeframe_text(site, query) do
case query.period do case query.period do
"6mo" -> "6mo" ->
"Last 6 months" "Last 6 months"
"3mo" -> "3mo" ->
"Last 3 months" "Last 3 months"
"month" -> "month" ->
Timex.format!(query.date_range.first, "{Mfull} {YYYY}") if query.date_range.first == this_month(site) do
"This month"
else
Timex.format!(query.date_range.first, "{Mfull} {YYYY}")
end
"day" -> "day" ->
Timex.format!(query.date_range.first, "{D} {Mfull} {YYYY}") if query.date_range.first == today(site) do
"Today"
else
Timex.format!(query.date_range.first, "{D} {Mfull} {YYYY}")
end
_ -> _ ->
"wat" "wat"
end end