diff --git a/assets/js/app.js b/assets/js/app.js index 4d53e4903..43f0918c2 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -31,3 +31,22 @@ if (flash) { flash.style.display = 'none' }, 2500) } + +const registerForm = document.getElementById('register-form') + +if (registerForm) { + registerForm.addEventListener('submit', function(e) { + e.preventDefault(); + setTimeout(submitForm, 1000); + var formSubmitted = false; + + function submitForm() { + if (!formSubmitted) { + formSubmitted = true; + registerForm.submit(); + } + } + + plausible('trigger', 'Signup', {callback: submitForm}); + }) +} diff --git a/assets/js/p.js b/assets/js/p.js index 40e7fc212..15d14553a 100644 --- a/assets/js/p.js +++ b/assets/js/p.js @@ -33,7 +33,7 @@ } function ignore(reason) { - console.warn('[Plausible] Ignoring pageview because ' + reason); + console.warn('[Plausible] Ignoring event because ' + reason); } function getUrl() { @@ -77,7 +77,7 @@ })) } - function trigger(eventName) { + function trigger(eventName, options) { if (/localhost$/.test(window.location.hostname)) return ignore('website is running locally'); if (window.location.protocol === 'file:') return ignore('website is running locally'); if (window.document.visibilityState === 'prerender') return ignore('document is prerendering'); @@ -95,13 +95,14 @@ request.onreadystatechange = function() { if (request.readyState == XMLHttpRequest.DONE) { setUserData(payload) + options && options.callback && options.callback() } } } - function page() { - trigger('pageview') + function page(options) { + trigger('pageview', options) } function trackPushState() { diff --git a/assets/js/plausible.js b/assets/js/plausible.js index 95049c0da..b23a3e3d6 100644 --- a/assets/js/plausible.js +++ b/assets/js/plausible.js @@ -33,7 +33,7 @@ } function ignore(reason) { - console.warn('[Plausible] Ignoring pageview because ' + reason); + console.warn('[Plausible] Ignoring event because ' + reason); } function getUrl() { diff --git a/assets/js/stats/index.js b/assets/js/stats/index.js index b2cf07b55..743a14c23 100644 --- a/assets/js/stats/index.js +++ b/assets/js/stats/index.js @@ -124,6 +124,15 @@ if (domainEl) { document.getElementById('browsers-stats').innerHTML = res router.updateLinkHandlers() }) + + if (document.getElementById('conversion-stats')) { + fetch(`/stats/${domain}/conversions${location.search}`) + .then(res => res.text()) + .then((res) => { + document.getElementById('conversion-stats').innerHTML = res + router.updateLinkHandlers() + }) + } }) setInterval(function() { diff --git a/lib/plausible/goal/schema.ex b/lib/plausible/goal/schema.ex new file mode 100644 index 000000000..912ac84e8 --- /dev/null +++ b/lib/plausible/goal/schema.ex @@ -0,0 +1,34 @@ +defmodule Plausible.Goal do + use Ecto.Schema + import Ecto.Changeset + + schema "goals" do + field :domain, :string + field :event_name, :string + field :page_path, :string + + timestamps() + end + + def changeset(goal, attrs \\ %{}) do + goal + |> cast(attrs, [:domain, :event_name, :page_path]) + |> validate_required([:domain]) + |> validate_event_name_and_page_path() + end + + defp validate_event_name_and_page_path(changeset) do + if present?(changeset, :event_name) || present?(changeset, :page_path) do + changeset + else + changeset + |> add_error(:event_name, "this field is required") + |> add_error(:page_path, "this field is required") + end + end + + defp present?(changeset, field) do + value = get_field(changeset, field) + value && value != "" + end +end diff --git a/lib/plausible/goals.ex b/lib/plausible/goals.ex new file mode 100644 index 000000000..3f44ce2e8 --- /dev/null +++ b/lib/plausible/goals.ex @@ -0,0 +1,21 @@ +defmodule Plausible.Goals do + use Plausible.Repo + alias Plausible.Goal + + def create(site, params) do + params = Map.merge(params, %{"domain" => site.domain}) + + Goal.changeset(%Goal{}, params) |> Repo.insert + end + + def for_site(domain) do + Repo.all( + from g in Goal, + where: g.domain == ^domain + ) + end + + def delete(id) do + Repo.one(from g in Goal, where: g.id == ^id) |> Repo.delete! + end +end diff --git a/lib/plausible/sites.ex b/lib/plausible/sites.ex index abbc53031..6ecf74818 100644 --- a/lib/plausible/sites.ex +++ b/lib/plausible/sites.ex @@ -18,6 +18,13 @@ defmodule Plausible.Sites do ) end + def has_goals?(site) do + Repo.exists?( + from g in Plausible.Goal, + where: g.domain == ^site.domain + ) + end + def is_owner?(user_id, site) do Repo.exists?( from sm in Plausible.Site.Membership, diff --git a/lib/plausible/stats/stats.ex b/lib/plausible/stats/stats.ex index 1a08bf84c..f3a8e1efa 100644 --- a/lib/plausible/stats/stats.ex +++ b/lib/plausible/stats/stats.ex @@ -181,6 +181,66 @@ defmodule Plausible.Stats do ) end + def goal_conversions(site, query, _limit \\ 5) do + goals = Repo.all(from g in Plausible.Goal, where: g.domain == ^site.domain) + fetch_pageview_goals(goals, site, query) + ++ fetch_event_goals(goals, site, query) + |> sort_conversions() + end + + defp fetch_event_goals(goals, site, query) do + {:ok, first} = NaiveDateTime.new(query.date_range.first, ~T[00:00:00]) + first_datetime = Timex.to_datetime(first, site.timezone) + + {:ok, last} = NaiveDateTime.new(query.date_range.last |> Timex.shift(days: 1), ~T[00:00:00]) + last_datetime = Timex.to_datetime(last, site.timezone) + + events = Enum.map(goals, fn goal -> goal.event_name end) + |> Enum.filter(&(&1)) + + if Enum.count(events) > 0 do + Repo.all( + from e in Plausible.Event, + where: e.hostname == ^site.domain, + where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime, + where: e.name in ^events, + group_by: e.name, + select: {e.name, count(e.user_id, :distinct)} + ) + else + [] + end + end + + defp fetch_pageview_goals(goals, site, query) do + {:ok, first} = NaiveDateTime.new(query.date_range.first, ~T[00:00:00]) + first_datetime = Timex.to_datetime(first, site.timezone) + + {:ok, last} = NaiveDateTime.new(query.date_range.last |> Timex.shift(days: 1), ~T[00:00:00]) + last_datetime = Timex.to_datetime(last, site.timezone) + + pages = Enum.map(goals, fn goal -> goal.page_path end) + |> Enum.filter(&(&1)) + + if Enum.count(pages) > 0 do + Repo.all( + from e in Plausible.Event, + where: e.hostname == ^site.domain, + where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime, + where: e.name == "pageview", + where: e.pathname in ^pages, + group_by: e.pathname, + select: {fragment("concat('Visit ', ?)", e.pathname), count(e.user_id, :distinct)} + ) + else + [] + end + end + + defp sort_conversions(conversions) do + Enum.sort_by(conversions, fn {_, count} -> -count end) + end + defp base_query(site, query) do {:ok, first} = NaiveDateTime.new(query.date_range.first, ~T[00:00:00]) first_datetime = Timex.to_datetime(first, site.timezone) diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex index abf9886b6..53aef9011 100644 --- a/lib/plausible_web/controllers/site_controller.ex +++ b/lib/plausible_web/controllers/site_controller.ex @@ -1,7 +1,7 @@ defmodule PlausibleWeb.SiteController do use PlausibleWeb, :controller use Plausible.Repo - alias Plausible.Sites + alias Plausible.{Sites, Goals} plug PlausibleWeb.RequireAccountPlug @@ -33,6 +33,59 @@ defmodule PlausibleWeb.SiteController do |> render("snippet.html", site: site, layout: {PlausibleWeb.LayoutView, "focus.html"}) end + def goals(conn, %{"website" => website}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + goals = Goals.for_site(site.domain) + + conn + |> assign(:skip_plausible_tracking, true) + |> render("goal_settings.html", + site: site, + goals: goals, + layout: {PlausibleWeb.LayoutView, "focus.html"} + ) + end + + def new_goal(conn, %{"website" => website}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + changeset = Plausible.Goal.changeset(%Plausible.Goal{}) + + conn + |> assign(:skip_plausible_tracking, true) + |> render("new_goal.html", + site: site, + changeset: changeset, + layout: {PlausibleWeb.LayoutView, "focus.html"} + ) + end + + def create_goal(conn, %{"website" => website, "goal" => goal}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + + case Plausible.Goals.create(site, goal) do + {:ok, _} -> + conn + |> put_flash(:success, "Goal created succesfully") + |> redirect(to: "/#{website}/goals") + {:error, :goal, changeset, _} -> + conn + |> assign(:skip_plausible_tracking, true) + |> render("new_goal.html", + site: site, + changeset: changeset, + layout: {PlausibleWeb.LayoutView, "focus.html"} + ) + end + end + + def delete_goal(conn, %{"website" => website, "id" => goal_id}) do + Plausible.Goals.delete(goal_id) + + conn + |> put_flash(:success, "Goal deleted succesfully") + |> redirect(to: "/#{website}/goals") + end + def settings(conn, %{"website" => website}) do site = Sites.get_for_user!(conn.assigns[:current_user].id, website) |> Repo.preload(:google_auth) diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex index 139e398cc..fabda2289 100644 --- a/lib/plausible_web/controllers/stats_controller.ex +++ b/lib/plausible_web/controllers/stats_controller.ex @@ -25,12 +25,14 @@ defmodule PlausibleWeb.StatsController do {conn, params} = fetch_period(conn, site) query = Stats.Query.from(site.timezone, params) current_visitors = Stats.current_visitors(site) + has_goals = user && Plausible.Sites.has_goals?(site) conn |> assign(:skip_plausible_tracking, !demo) |> put_session(site.domain <> "_offer_email_report", nil) |> render("stats.html", site: site, + has_goals: has_goals, query: query, current_visitors: current_visitors, title: "Plausible · " <> site.domain, @@ -310,6 +312,20 @@ defmodule PlausibleWeb.StatsController do end end + def conversions_preview(conn, %{"domain" => domain}) do + site = Repo.get_by(Plausible.Site, domain: domain) + + if site && current_user_can_access?(conn, site) do + {conn, params} = fetch_period(conn, site) + query = Stats.Query.from(site.timezone, params) + goals = Stats.goal_conversions(site, query) + + render(conn, "conversions_preview.html", layout: false, query: query, site: site, goals: goals) + else + render_error(conn, 404) + end + end + defp current_user_can_access?(_conn, %Plausible.Site{public: true}) do true end diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index 9109b2231..e687bac0e 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -93,6 +93,10 @@ defmodule PlausibleWeb.Router do put "/sites/:website/monthly-report", SiteController, :update_monthly_settings get "/:website/snippet", SiteController, :add_snippet get "/:website/settings", SiteController, :settings + get "/:website/goals", SiteController, :goals + get "/:website/goals/new", SiteController, :new_goal + post "/:website/goals", SiteController, :create_goal + delete "/:website/goals/:id", SiteController, :delete_goal put "/:website/settings", SiteController, :update_settings put "/:website/settings/google", SiteController, :update_google_auth delete "/:website", SiteController, :delete_site @@ -103,6 +107,7 @@ defmodule PlausibleWeb.Router do get "/stats/:domain/screen-sizes", StatsController, :screen_sizes_preview get "/stats/:domain/operating-systems", StatsController, :operating_systems_preview get "/stats/:domain/browsers", StatsController, :browsers_preview + get "/stats/:domain/conversions", StatsController, :conversions_preview get "/stats/:domain/main-graph", StatsController, :main_graph get "/:website/*path", StatsController, :stats end diff --git a/lib/plausible_web/templates/auth/register_form.html.eex b/lib/plausible_web/templates/auth/register_form.html.eex index b3473835e..73b1ecc69 100644 --- a/lib/plausible_web/templates/auth/register_form.html.eex +++ b/lib/plausible_web/templates/auth/register_form.html.eex @@ -1,4 +1,4 @@ -<%= form_for @changeset, "/register", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 py-6 mb-4 mt-8"], fn f -> %> +<%= form_for @changeset, "/register", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 py-6 mb-4 mt-8", id: "register-form"], fn f -> %>
©2019 Plausible Insights. All rights reserved.
+ +