2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.StatsController do
|
|
|
|
use PlausibleWeb, :controller
|
|
|
|
use Plausible.Repo
|
2021-10-26 16:54:50 +03:00
|
|
|
alias PlausibleWeb.Api
|
2021-11-09 15:51:10 +03:00
|
|
|
alias Plausible.Stats.{Query, Filters}
|
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)
|
2022-02-23 22:48:33 +03:00
|
|
|
can_see_stats = !site.locked || conn.assigns[:current_user_role] == :super_admin
|
2021-06-16 15:00:07 +03:00
|
|
|
|
|
|
|
cond do
|
2022-02-23 22:48:33 +03:00
|
|
|
has_stats && can_see_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
|
|
|
|
)
|
|
|
|
|
2022-02-23 22:48:33 +03:00
|
|
|
!has_stats && can_see_stats ->
|
2021-06-16 15:00:07 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("waiting_first_pageview.html", site: site)
|
|
|
|
|
|
|
|
site.locked ->
|
2021-10-27 12:38:24 +03:00
|
|
|
owner = Plausible.Sites.owner_for(site)
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2021-10-27 12:38:24 +03:00
|
|
|
|> render("site_locked.html", owner: owner, site: site)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-15 18:06:39 +03:00
|
|
|
@doc """
|
|
|
|
The export is limited to 300 entries for other reports and 100 entries for pages because bigger result sets
|
|
|
|
start causing failures. Since we request data like time on page or bounce_rate for pages in a separate query
|
|
|
|
using the IN filter, it causes the requests to balloon in payload size.
|
|
|
|
"""
|
2021-10-26 16:54:50 +03:00
|
|
|
def csv_export(conn, params) do
|
2020-04-14 14:04:35 +03:00
|
|
|
site = conn.assigns[:site]
|
2021-11-09 15:51:10 +03:00
|
|
|
query = Query.from(site.timezone, params) |> Filters.add_prefix()
|
2021-04-23 15:27:50 +03:00
|
|
|
|
2021-11-09 15:51:10 +03:00
|
|
|
metrics = ["visitors", "pageviews", "bounce_rate", "visit_duration"]
|
2021-04-23 15:27:50 +03:00
|
|
|
graph = Plausible.Stats.timeseries(site, query, metrics)
|
|
|
|
headers = ["date" | metrics]
|
2020-01-13 16:16:35 +03:00
|
|
|
|
2021-10-26 16:54:50 +03:00
|
|
|
visitors =
|
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.join()
|
|
|
|
|
|
|
|
filename =
|
2021-10-26 16:54:50 +03:00
|
|
|
"Plausible export #{params["domain"]} #{Timex.format!(query.date_range.first, "{ISOdate} ")} to #{Timex.format!(query.date_range.last, "{ISOdate} ")}.zip"
|
|
|
|
|
2021-11-04 15:20:39 +03:00
|
|
|
params = Map.merge(params, %{"limit" => "300", "csv" => "True", "detailed" => "True"})
|
2021-11-15 18:06:39 +03:00
|
|
|
limited_params = Map.merge(params, %{"limit" => "100"})
|
2021-10-26 16:54:50 +03:00
|
|
|
|
|
|
|
csvs = [
|
2021-11-04 15:20:39 +03:00
|
|
|
{'sources.csv', fn -> Api.StatsController.sources(conn, params) end},
|
|
|
|
{'utm_mediums.csv', fn -> Api.StatsController.utm_mediums(conn, params) end},
|
|
|
|
{'utm_sources.csv', fn -> Api.StatsController.utm_sources(conn, params) end},
|
|
|
|
{'utm_campaigns.csv', fn -> Api.StatsController.utm_campaigns(conn, params) end},
|
2021-12-17 17:00:06 +03:00
|
|
|
{'utm_contents.csv', fn -> Api.StatsController.utm_contents(conn, params) end},
|
|
|
|
{'utm_terms.csv', fn -> Api.StatsController.utm_terms(conn, params) end},
|
2021-11-15 18:06:39 +03:00
|
|
|
{'pages.csv', fn -> Api.StatsController.pages(conn, limited_params) end},
|
2021-11-04 15:20:39 +03:00
|
|
|
{'entry_pages.csv', fn -> Api.StatsController.entry_pages(conn, params) end},
|
2021-11-15 18:06:39 +03:00
|
|
|
{'exit_pages.csv', fn -> Api.StatsController.exit_pages(conn, limited_params) end},
|
2021-11-04 15:20:39 +03:00
|
|
|
{'countries.csv', fn -> Api.StatsController.countries(conn, params) end},
|
2022-01-04 12:08:06 +03:00
|
|
|
{'regions.csv', fn -> Api.StatsController.regions(conn, params) end},
|
|
|
|
{'cities.csv', fn -> Api.StatsController.cities(conn, params) end},
|
2021-11-04 15:20:39 +03:00
|
|
|
{'browsers.csv', fn -> Api.StatsController.browsers(conn, params) end},
|
|
|
|
{'operating_systems.csv', fn -> Api.StatsController.operating_systems(conn, params) end},
|
|
|
|
{'devices.csv', fn -> Api.StatsController.screen_sizes(conn, params) end},
|
2021-11-10 17:53:38 +03:00
|
|
|
{'conversions.csv', fn -> Api.StatsController.conversions(conn, params) end},
|
|
|
|
{'prop_breakdown.csv', fn -> Api.StatsController.all_props_breakdown(conn, params) end}
|
2021-10-26 16:54:50 +03:00
|
|
|
]
|
|
|
|
|
2021-11-04 15:20:39 +03:00
|
|
|
csvs =
|
|
|
|
csvs
|
|
|
|
|> Enum.map(fn {file, task} -> {file, Task.async(task)} end)
|
|
|
|
|> Enum.map(fn {file, task} -> {file, Task.await(task)} end)
|
|
|
|
|
|
|
|
csvs = [{'visitors.csv', visitors} | csvs]
|
|
|
|
|
2021-10-26 16:54:50 +03:00
|
|
|
{:ok, {_, zip_content}} = :zip.create(filename, csvs, [:memory])
|
2020-01-13 16:16:35 +03:00
|
|
|
|
2020-04-14 14:04:35 +03:00
|
|
|
conn
|
2021-10-26 16:54:50 +03:00
|
|
|
|> put_resp_content_type("application/zip")
|
2020-04-14 14:04:35 +03:00
|
|
|
|> put_resp_header("content-disposition", "attachment; filename=\"#{filename}\"")
|
2021-11-09 10:51:38 +03:00
|
|
|
|> delete_resp_cookie("exporting")
|
2021-10-26 16:54:50 +03:00
|
|
|
|> send_resp(200, zip_content)
|
2020-01-13 16:16:35 +03:00
|
|
|
end
|
|
|
|
|
2022-02-22 22:46:57 +03:00
|
|
|
def shared_link(conn, %{"domain" => 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),
|
2022-02-22 22:46:57 +03:00
|
|
|
{:ok, token} <- Map.fetch(conn.req_cookies, shared_link_cookie_name(auth)),
|
|
|
|
{:ok, %{slug: token_slug}} <- Plausible.Auth.Token.verify_shared_link(token),
|
|
|
|
true <- token_slug == shared_link.slug do
|
2021-03-02 12:15:43 +03:00
|
|
|
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
|
2022-02-22 22:46:57 +03:00
|
|
|
|> put_resp_cookie(shared_link_cookie_name(slug), token)
|
2021-03-02 12:15:43 +03:00
|
|
|
|> 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
|
2021-12-14 13:10:34 +03:00
|
|
|
cond do
|
|
|
|
!shared_link.site.locked ->
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> put_resp_header("x-robots-tag", "noindex")
|
|
|
|
|> delete_resp_header("x-frame-options")
|
|
|
|
|> 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,
|
|
|
|
skip_plausible_tracking: true,
|
|
|
|
shared_link_auth: shared_link.slug,
|
|
|
|
embedded: conn.params["embed"] == "true",
|
|
|
|
background: conn.params["background"],
|
|
|
|
theme: conn.params["theme"]
|
|
|
|
)
|
|
|
|
|
|
|
|
shared_link.site.locked ->
|
|
|
|
owner = Plausible.Sites.owner_for(shared_link.site)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("site_locked.html", owner: owner, site: shared_link.site)
|
|
|
|
end
|
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
|
2022-02-22 22:46:57 +03:00
|
|
|
|
|
|
|
defp shared_link_cookie_name(slug), do: "shared-link-" <> slug
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|