Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Add `name` to events

Uku Taht3ea64de42bd7
diff --git a/lib/plausible/event/schema.ex b/lib/plausible/event/schema.ex
index 8084e95a5a..2e04883150 100644
--- a/lib/plausible/event/schema.ex
+++ b/lib/plausible/event/schema.ex
@@ -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
diff --git a/lib/plausible/stats/stats.ex b/lib/plausible/stats/stats.ex
index 9837265860..4c3e0997f5 100644
--- a/lib/plausible/stats/stats.ex
+++ b/lib/plausible/stats/stats.ex
@@ -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
)
diff --git a/lib/plausible_web/controllers/api/external_controller.ex b/lib/plausible_web/controllers/api/external_controller.ex
index 6d717ea543..5718fc482c 100644
--- a/lib/plausible_web/controllers/api/external_controller.ex
+++ b/lib/plausible_web/controllers/api/external_controller.ex
@@ -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
diff --git a/priv/repo/migrations/20191025055334_add_name_to_events.exs b/priv/repo/migrations/20191025055334_add_name_to_events.exs
new file mode 100644
index 0000000000..13ea39061e
--- /dev/null
+++ b/priv/repo/migrations/20191025055334_add_name_to_events.exs
@@ -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
diff --git a/test/plausible/stats/stats_test.exs b/test/plausible/stats/stats_test.exs
index 2499cce2ae..5d5bb1d040 100644
--- a/test/plausible/stats/stats_test.exs
+++ b/test/plausible/stats/stats_test.exs
@@ -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
diff --git a/test/support/factory.ex b/test/support/factory.ex
index ef28ff4df7..f212bbe0b9 100644
--- a/test/support/factory.ex
+++ b/test/support/factory.ex
@@ -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{