2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.StatsController do
|
|
|
|
use PlausibleWeb, :controller
|
|
|
|
use Plausible.Repo
|
|
|
|
|
|
|
|
def stats(conn, %{"website" => website}) do
|
|
|
|
site = Repo.get_by(Plausible.Site, domain: website)
|
|
|
|
|
|
|
|
if site && current_user_can_access?(conn, site) do
|
|
|
|
user = conn.assigns[:current_user]
|
|
|
|
if user && Plausible.Billing.needs_to_upgrade?(conn.assigns[:current_user]) do
|
|
|
|
redirect(conn, to: "/billing/upgrade")
|
|
|
|
else
|
|
|
|
if Plausible.Sites.has_pageviews?(site) do
|
2019-11-26 07:08:33 +03:00
|
|
|
demo = site.domain == "plausible.io"
|
2019-09-10 18:51:34 +03:00
|
|
|
offer_email_report = get_session(conn, site.domain <> "_offer_email_report")
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2019-11-26 07:08:33 +03:00
|
|
|
Plausible.Tracking.event(conn, "Site Analytics: Open", %{demo: demo})
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
conn
|
2019-11-26 07:08:33 +03:00
|
|
|
|> assign(:skip_plausible_tracking, !demo)
|
2019-09-10 18:51:34 +03:00
|
|
|
|> put_session(site.domain <> "_offer_email_report", nil)
|
2019-09-02 14:29:19 +03:00
|
|
|
|> render("stats.html",
|
|
|
|
site: site,
|
2019-11-28 07:44:31 +03:00
|
|
|
has_goals: Plausible.Sites.has_goals?(site),
|
2019-09-10 18:51:34 +03:00
|
|
|
title: "Plausible · " <> site.domain,
|
|
|
|
offer_email_report: offer_email_report
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
else
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("waiting_first_pageview.html", site: site)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render_error(conn, 404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp current_user_can_access?(_conn, %Plausible.Site{public: true}) do
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
defp current_user_can_access?(conn, site) do
|
|
|
|
case conn.assigns[:current_user] do
|
|
|
|
nil -> false
|
|
|
|
user -> Plausible.Sites.is_owner?(user.id, site)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-11-19 07:30:42 +03:00
|
|
|
|