2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.StatsController do
|
|
|
|
use PlausibleWeb, :controller
|
|
|
|
use Plausible.Repo
|
2020-05-18 12:44:52 +03:00
|
|
|
alias Plausible.Stats.Query
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
plug PlausibleWeb.AuthorizeSiteAccess when action in [:stats, :csv_export]
|
2020-05-26 16:09:34 +03:00
|
|
|
|
2020-10-02 12:02:32 +03:00
|
|
|
def stats(%{assigns: %{site: site}} = conn, _params) do
|
2021-09-06 13:54:51 +03:00
|
|
|
has_stats = Plausible.Sites.has_stats?(site)
|
2021-06-16 15:00:07 +03:00
|
|
|
|
|
|
|
cond do
|
2021-09-06 13:54:51 +03:00
|
|
|
!site.locked && has_stats ->
|
2021-06-16 15:00:07 +03:00
|
|
|
demo = site.domain == PlausibleWeb.Endpoint.host()
|
|
|
|
offer_email_report = get_session(conn, site.domain <> "_offer_email_report")
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, !demo)
|
|
|
|
|> remove_email_report_banner(site)
|
|
|
|
|> put_resp_header("x-robots-tag", "noindex")
|
|
|
|
|> render("stats.html",
|
|
|
|
site: site,
|
|
|
|
has_goals: Plausible.Sites.has_goals?(site),
|
|
|
|
title: "Plausible · " <> site.domain,
|
|
|
|
offer_email_report: offer_email_report,
|
|
|
|
demo: demo
|
|
|
|
)
|
|
|
|
|
2021-09-06 13:54:51 +03:00
|
|
|
!site.locked && !has_stats ->
|
2021-06-16 15:00:07 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("waiting_first_pageview.html", site: site)
|
|
|
|
|
|
|
|
site.locked ->
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("site_locked.html", site: site)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-14 14:04:35 +03:00
|
|
|
def csv_export(conn, %{"domain" => domain}) do
|
|
|
|
site = conn.assigns[:site]
|
2020-05-18 12:44:52 +03:00
|
|
|
query = Query.from(site.timezone, conn.params)
|
2021-04-23 15:27:50 +03:00
|
|
|
|
|
|
|
metrics =
|
|
|
|
if query.filters["event:name"] do
|
|
|
|
["visitors", "pageviews"]
|
|
|
|
else
|
|
|
|
["visitors", "pageviews", "bounce_rate", "visit_duration"]
|
|
|
|
end
|
|
|
|
|
|
|
|
graph = Plausible.Stats.timeseries(site, query, metrics)
|
|
|
|
|
|
|
|
headers = ["date" | metrics]
|
2020-01-13 16:16:35 +03:00
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
csv_content =
|
2021-04-23 15:27:50 +03:00
|
|
|
Enum.map(graph, fn row -> Enum.map(headers, &row[&1]) end)
|
|
|
|
|> (fn data -> [headers | data] end).()
|
2020-06-08 10:35:13 +03:00
|
|
|
|> CSV.encode()
|
|
|
|
|> Enum.into([])
|
|
|
|
|> Enum.join()
|
|
|
|
|
|
|
|
filename =
|
2021-04-23 15:27:50 +03:00
|
|
|
"Plausible export #{domain} #{Timex.format!(query.date_range.first, "{ISOdate} ")} to #{
|
2020-06-08 10:35:13 +03:00
|
|
|
Timex.format!(query.date_range.last, "{ISOdate} ")
|
|
|
|
}.csv"
|
2020-01-13 16:16:35 +03:00
|
|
|
|
2020-04-14 14:04:35 +03:00
|
|
|
conn
|
|
|
|
|> put_resp_content_type("text/csv")
|
|
|
|
|> put_resp_header("content-disposition", "attachment; filename=\"#{filename}\"")
|
2020-06-08 10:35:13 +03:00
|
|
|
|> send_resp(200, csv_content)
|
2020-01-13 16:16:35 +03:00
|
|
|
end
|
|
|
|
|
2021-03-02 12:15:43 +03:00
|
|
|
def shared_link(conn, %{"slug" => domain, "auth" => auth}) do
|
2020-06-08 10:35:13 +03:00
|
|
|
shared_link =
|
2021-03-02 12:15:43 +03:00
|
|
|
Repo.get_by(Plausible.Site.SharedLink, slug: auth)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.preload(:site)
|
2020-01-29 12:29:11 +03:00
|
|
|
|
2021-03-02 12:15:43 +03:00
|
|
|
if shared_link && shared_link.site.domain == domain do
|
2020-01-29 12:29:11 +03:00
|
|
|
if shared_link.password_hash do
|
2021-03-02 12:15:43 +03:00
|
|
|
with conn <- Plug.Conn.fetch_cookies(conn),
|
|
|
|
{:ok, token} <- Map.fetch(conn.req_cookies, "shared-link-token"),
|
|
|
|
{:ok, _} <- Plausible.Auth.Token.verify_shared_link(token) do
|
|
|
|
render_shared_link(conn, shared_link)
|
|
|
|
else
|
|
|
|
_e ->
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("shared_link_password.html",
|
|
|
|
link: shared_link,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
2020-01-29 12:29:11 +03:00
|
|
|
else
|
2021-03-02 12:15:43 +03:00
|
|
|
render_shared_link(conn, shared_link)
|
2020-01-29 12:29:11 +03:00
|
|
|
end
|
2021-03-02 12:15:43 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def shared_link(conn, %{"slug" => slug}) do
|
|
|
|
shared_link =
|
|
|
|
Repo.get_by(Plausible.Site.SharedLink, slug: slug)
|
|
|
|
|> Repo.preload(:site)
|
|
|
|
|
|
|
|
if shared_link do
|
|
|
|
redirect(conn, to: "/share/#{URI.encode_www_form(shared_link.site.domain)}?auth=#{slug}")
|
2020-01-29 12:29:11 +03:00
|
|
|
else
|
|
|
|
render_error(conn, 404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_shared_link(conn, %{"slug" => slug, "password" => password}) do
|
2020-06-08 10:35:13 +03:00
|
|
|
shared_link =
|
|
|
|
Repo.get_by(Plausible.Site.SharedLink, slug: slug)
|
|
|
|
|> Repo.preload(:site)
|
2020-01-29 12:29:11 +03:00
|
|
|
|
|
|
|
if shared_link do
|
|
|
|
if Plausible.Auth.Password.match?(password, shared_link.password_hash) do
|
2021-03-02 12:15:43 +03:00
|
|
|
token = Plausible.Auth.Token.sign_shared_link(slug)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_resp_cookie("shared-link-token", token)
|
|
|
|
|> redirect(to: "/share/#{URI.encode_www_form(shared_link.site.domain)}?auth=#{slug}")
|
2020-01-29 12:29:11 +03:00
|
|
|
else
|
2020-05-19 16:20:21 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> render("shared_link_password.html",
|
|
|
|
link: shared_link,
|
|
|
|
error: "Incorrect password. Please try again.",
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-01-29 12:29:11 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
render_error(conn, 404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-02 12:15:43 +03:00
|
|
|
defp render_shared_link(conn, shared_link) do
|
2020-01-29 12:29:11 +03:00
|
|
|
conn
|
2021-03-02 12:15:43 +03:00
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> put_resp_header("x-robots-tag", "noindex")
|
2021-03-10 15:41:17 +03:00
|
|
|
|> delete_resp_header("x-frame-options")
|
2021-03-02 12:15:43 +03:00
|
|
|
|> render("stats.html",
|
|
|
|
site: shared_link.site,
|
|
|
|
has_goals: Plausible.Sites.has_goals?(shared_link.site),
|
|
|
|
title: "Plausible · " <> shared_link.site.domain,
|
|
|
|
offer_email_report: false,
|
|
|
|
demo: false,
|
2021-03-15 16:56:12 +03:00
|
|
|
skip_plausible_tracking: true,
|
2021-03-10 15:41:17 +03:00
|
|
|
shared_link_auth: shared_link.slug,
|
|
|
|
embedded: conn.params["embed"] == "true",
|
2021-03-16 11:40:25 +03:00
|
|
|
background: conn.params["background"],
|
2021-03-15 16:56:12 +03:00
|
|
|
theme: conn.params["theme"]
|
2021-03-02 12:15:43 +03:00
|
|
|
)
|
2020-01-29 12:29:11 +03:00
|
|
|
end
|
|
|
|
|
2020-04-13 15:14:16 +03:00
|
|
|
defp remove_email_report_banner(conn, site) do
|
|
|
|
if conn.assigns[:current_user] do
|
2020-11-06 14:34:31 +03:00
|
|
|
delete_session(conn, site.domain <> "_offer_email_report")
|
2020-04-13 15:14:16 +03:00
|
|
|
else
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|