From 3ea64de42bd764ced334a1944eb7949108ea410b Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Fri, 25 Oct 2019 14:06:11 +0800 Subject: [PATCH] Add `name` to events --- lib/plausible/event/schema.ex | 1 + lib/plausible/stats/stats.ex | 1 + .../controllers/api/external_controller.ex | 2 +- .../20191025055334_add_name_to_events.exs | 20 +++++++++++++++ test/plausible/stats/stats_test.exs | 25 +++++++++++++++++++ test/support/factory.ex | 9 +++++++ 6 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20191025055334_add_name_to_events.exs diff --git a/lib/plausible/event/schema.ex b/lib/plausible/event/schema.ex index 8084e95a5..2e0488315 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 983726586..4c3e0997f 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 6d717ea54..5718fc482 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 000000000..13ea39061 --- /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 2499cce2a..5d5bb1d04 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 ef28ff4df..f212bbe0b 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{