Add `name` to events

pull/15/head
Uku Taht 7 years ago
parent 0c453be08c
commit 3ea64de42b

@ -3,6 +3,7 @@ defmodule Plausible.Event do
import Ecto.Changeset
schema "events" do
field :name, :string
field :hostname, :string
field :pathname, :string
field :new_visitor, :boolean

@ -200,6 +200,7 @@ defmodule Plausible.Stats do
last_datetime = Timex.to_datetime(last, site.timezone)
from(e in Plausible.Event,
where: e.name == "pageview",
where: e.hostname == ^site.domain,
where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime
)

@ -55,7 +55,7 @@ defmodule PlausibleWeb.Api.ExternalController do
screen_size: calculate_screen_size(params["screen_width"])
}
Plausible.Event.changeset(%Plausible.Event{}, pageview_attrs)
Plausible.Event.changeset(%Plausible.Event{name: "pageview"}, pageview_attrs)
|> Plausible.Repo.insert
end
end

@ -0,0 +1,20 @@
defmodule Plausible.Repo.Migrations.AddNameToEvents do
use Ecto.Migration
use Plausible.Repo
def change do
alter table(:events) do
add :name, :string
end
flush()
Repo.update_all(Plausible.Event, set: [name: "pageview"])
flush()
alter table(:events) do
modify :name, :string, null: false
end
end
end

@ -1,6 +1,31 @@
defmodule Plausible.StatsTest do
use Plausible.DataCase
alias Plausible.Stats
@user_id UUID.uuid4()
describe "pageviews_and_visitors" do
test "counts unique visitors" do
site = insert(:site)
insert(:pageview, hostname: site.domain, user_id: @user_id, timestamp: ~N[2019-01-01 00:00:00])
insert(:pageview, hostname: site.domain, user_id: @user_id, timestamp: ~N[2019-01-01 23:59:00])
query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"})
{pageviews, visitors} = Stats.pageviews_and_visitors(site, query)
assert pageviews == 2
assert visitors == 1
end
test "does not count custom events" do
site = insert(:site)
insert(:pageview, hostname: site.domain, timestamp: ~N[2019-01-01 00:00:00])
insert(:event, name: "Custom", hostname: site.domain, timestamp: ~N[2019-01-01 23:59:00])
query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"})
{pageviews, visitors} = Stats.pageviews_and_visitors(site, query)
assert pageviews == 1
assert visitors == 1
end
end
describe "calculate_plot" do
test "displays pageviews for a day" do

@ -23,6 +23,15 @@ defmodule Plausible.Factory do
end
def pageview_factory do
struct!(
event_factory(),
%{
name: "pageview"
}
)
end
def event_factory do
hostname = sequence(:domain, &"example-#{&1}.com")
%Plausible.Event{

Loading…
Cancel
Save