commit
f36b7afbb8
@ -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
|
||||
@ -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
|
||||
@ -0,0 +1,23 @@
|
||||
<%= if get_flash(@conn, :success) do %>
|
||||
<div id="flash" class="max-w-sm w-full rounded mx-auto text-center bg-green-dark text-green-lightest text-sm font-bold px-4 w-full transition overflow-hidden" role="alert">
|
||||
<p class="py-3"><%= get_flash(@conn, :success) %></p>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8">
|
||||
<h2>Goals for <%= @site.domain %></h2>
|
||||
<div class="my-6">
|
||||
<%= if Enum.count(@goals) > 0 do %>
|
||||
<%= for goal <- @goals do %>
|
||||
<div class="border-b border-grey-light py-3 flex justify-between">
|
||||
<small class="font-bold"><%= goal_name(goal) %></small>
|
||||
<%= button("❌", to: "/#{@site.domain}/goals/#{goal.id}", method: :delete, class: "text-sm", data: [confirm: "Are you sure you want to remove goal #{goal_name(goal)}? This will just affect the UI, all of your analytics data will stay intact."]) %>
|
||||
</div>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<div>No goals configured for this site yet</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= link("+ Add goal", to: "/#{@site.domain}/goals/new", class: "button mt-4 w-full") %>
|
||||
</div>
|
||||
@ -0,0 +1,37 @@
|
||||
<%= form_for @changeset, "/#{@site.domain}/goals", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>
|
||||
<h2>Add goal for <%= @site.domain %></h2>
|
||||
<div class="mt-6 text-sm font-bold">Goal trigger</div>
|
||||
<div class="my-3 w-full flex rounded border border-grey-light">
|
||||
<div class="w-1/2 text-center py-2 border-r border-grey shadow-inner font-bold bg-grey-lighter cursor-pointer" id="pageview-tab">Pageview</div>
|
||||
<div class="w-1/2 text-center py-2 bg-grey-lightest cursor-pointer" id="event-tab">Custom event</div>
|
||||
</div>
|
||||
<div class="my-6">
|
||||
<div id="pageview-fields">
|
||||
<%= label f, :page_path, class: "block text-sm font-bold" %>
|
||||
<%= text_input f, :page_path, class: "transition mt-3 bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:bg-white focus:border-grey-light", placeholder: "/success" %>
|
||||
<%= error_tag f, :page_path %>
|
||||
</div>
|
||||
<div id="event-fields" class="hidden">
|
||||
<%= label f, :event_name, class: "block text-sm font-bold" %>
|
||||
<%= text_input f, :event_name, class: "transition mt-3 bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:bg-white focus:border-grey-light", placeholder: "Signup" %>
|
||||
<%= error_tag f, :event_name %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<%= submit "Add goal →", class: "button mt-4 w-full" %>
|
||||
<% end %>
|
||||
|
||||
<script>
|
||||
document.getElementById('pageview-tab').onclick = function() {
|
||||
document.getElementById('pageview-fields').classList.remove('hidden')
|
||||
document.getElementById('pageview-tab').classList.add('shadow-inner', 'font-bold')
|
||||
document.getElementById('event-fields').classList.add('hidden')
|
||||
document.getElementById('event-tab').classList.remove('shadow-inner', 'font-bold')
|
||||
}
|
||||
document.getElementById('event-tab').onclick = function() {
|
||||
document.getElementById('event-fields').classList.remove('hidden')
|
||||
document.getElementById('event-tab').classList.add('shadow-inner', 'font-bold')
|
||||
document.getElementById('pageview-fields').classList.add('hidden')
|
||||
document.getElementById('pageview-tab').classList.remove('shadow-inner', 'font-bold')
|
||||
}
|
||||
</script>
|
||||
@ -0,0 +1,27 @@
|
||||
<div class="text-center">
|
||||
<h2>Conversions</h2>
|
||||
<div class="text-grey-darker mt-1">by unique visitors</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-8">
|
||||
<%= if Enum.count(@goals) > 0 do %>
|
||||
<%= for {key, count} <- @goals do %>
|
||||
<div class="flex items-center justify-between my-2">
|
||||
<span class="truncate" style="max-width: 80%;"><%= key %></span>
|
||||
<span title="<%= count %>"><%= large_number_format(count) %></span>
|
||||
</div>
|
||||
<%= bar(count, @goals, :indigo) %>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<div class="text-center">No conversions for this period</div>
|
||||
<div class="text-center mt-2"><%= link("Manage goals", to: "/#{@site.domain}/goals", class: "text-indigo") %></div>
|
||||
<% end %>
|
||||
</div>
|
||||
<%= if Enum.count(@goals) >= 5 do %>
|
||||
<div class="text-center">
|
||||
<a href="/<%= @site.domain %>/conversions<%= query_params(@query) %>" data-pushstate class="font-bold text-sm text-grey-dark hover:text-red transition tracking-wide">
|
||||
<svg style="fill: #8795a1;" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-maximize"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"/></svg>
|
||||
MORE
|
||||
</a>
|
||||
</div>
|
||||
<% end %>
|
||||
@ -0,0 +1,16 @@
|
||||
defmodule Plausible.Repo.Migrations.AddGoals do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:goals) do
|
||||
add :domain, :text, null: false
|
||||
add :name, :text, null: false
|
||||
add :event_name, :text
|
||||
add :page_path, :text
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
create unique_index(:goals, [:domain, :name])
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,9 @@
|
||||
defmodule Plausible.Repo.Migrations.RemoveGoalName do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
alter table(:goals) do
|
||||
remove :name
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue