Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Add `name` to events
Uku Taht3ea64de42bd7
diff --git a/lib/plausible/event/schema.ex b/lib/plausible/event/schema.exindex 8084e95a5a..2e04883150 100644--- a/lib/plausible/event/schema.ex+++ b/lib/plausible/event/schema.ex@@ -3,6 +3,7 @@ defmodule Plausible.Event doimport Ecto.Changesetschema "events" do+ field :name, :stringfield :hostname, :stringfield :pathname, :stringfield :new_visitor, :booleandiff --git a/lib/plausible/stats/stats.ex b/lib/plausible/stats/stats.exindex 9837265860..4c3e0997f5 100644--- a/lib/plausible/stats/stats.ex+++ b/lib/plausible/stats/stats.ex@@ -200,6 +200,7 @@ defmodule Plausible.Stats dolast_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.exindex 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 doscreen_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.insertendenddiff --git a/priv/repo/migrations/20191025055334_add_name_to_events.exs b/priv/repo/migrations/20191025055334_add_name_to_events.exsnew file mode 100644index 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+enddiff --git a/test/plausible/stats/stats_test.exs b/test/plausible/stats/stats_test.exsindex 2499cce2ae..5d5bb1d040 100644--- a/test/plausible/stats/stats_test.exs+++ b/test/plausible/stats/stats_test.exs@@ -1,6 +1,31 @@defmodule Plausible.StatsTest douse Plausible.DataCasealias 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+ enddescribe "calculate_plot" dotest "displays pageviews for a day" dodiff --git a/test/support/factory.ex b/test/support/factory.exindex ef28ff4df7..f212bbe0b9 100644--- a/test/support/factory.ex+++ b/test/support/factory.ex@@ -23,6 +23,15 @@ defmodule Plausible.Factory doenddef pageview_factory do+ struct!(+ event_factory(),+ %{+ name: "pageview"+ }+ )+ end++ def event_factory dohostname = sequence(:domain, &"example-#{&1}.com")%Plausible.Event{