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
Rename pageviews to events
Uku Taht0c453be08c72
diff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.exindex dbbc563d65..5c67f89df6 100644--- a/lib/plausible/auth/auth.ex+++ b/lib/plausible/auth/auth.ex@@ -15,9 +15,9 @@ defmodule Plausible.Auth dodef user_completed_setup?(user) doquery =from(- p in Plausible.Pageview,+ e in Plausible.Event,join: s in Plausible.Site,- on: s.domain == p.hostname,+ on: s.domain == e.hostname,join: sm in Plausible.Site.Membership,on: sm.site_id == s.id,join: u in Plausible.Auth.User,diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.exindex f752dbef1c..d08793031b 100644--- a/lib/plausible/billing/billing.ex+++ b/lib/plausible/billing/billing.ex@@ -118,11 +118,10 @@ defmodule Plausible.Billing dodefp site_usage(site) doRepo.aggregate(from(- p in Plausible.Pageview,- where: p.hostname == ^site.domain,- where: p.timestamp >= fragment("now() - '30 days'::interval")- ), :count, :id- )+ e in Plausible.Event,+ where: e.hostname == ^site.domain,+ where: e.timestamp >= fragment("now() - '30 days'::interval")+ ), :count, :id)enddefp format_subscription(params) dodiff --git a/lib/plausible/pageview/schema.ex b/lib/plausible/event/schema.exsimilarity index 92%rename from lib/plausible/pageview/schema.exrename to lib/plausible/event/schema.exindex 7132b70755..8084e95a5a 100644--- a/lib/plausible/pageview/schema.ex+++ b/lib/plausible/event/schema.ex@@ -1,19 +1,19 @@-defmodule Plausible.Pageview do+defmodule Plausible.Event douse Ecto.Schemaimport Ecto.Changeset- schema "pageviews" do+ schema "events" dofield :hostname, :stringfield :pathname, :stringfield :new_visitor, :booleanfield :user_id, :binary_idfield :referrer, :string- field :screen_size, :string+ field :referrer_source, :stringfield :country_code, :string+ field :screen_size, :stringfield :operating_system, :stringfield :browser, :string- field :referrer_source, :stringtimestamps(inserted_at: :timestamp, updated_at: false)enddiff --git a/lib/plausible/sites.ex b/lib/plausible/sites.exindex 95884c8822..abbc53031b 100644--- a/lib/plausible/sites.ex+++ b/lib/plausible/sites.ex@@ -13,8 +13,8 @@ defmodule Plausible.Sites dodef has_pageviews?(site) doRepo.exists?(- from p in Plausible.Pageview,- where: p.hostname == ^site.domain+ from e in Plausible.Event,+ where: e.hostname == ^site.domain)enddiff --git a/lib/plausible/stats/stats.ex b/lib/plausible/stats/stats.exindex 8d72f5bd3d..9837265860 100644--- a/lib/plausible/stats/stats.ex+++ b/lib/plausible/stats/stats.ex@@ -24,10 +24,10 @@ defmodule Plausible.Stats doend)groups = Repo.all(- from p in base_query(site, query),+ from e in base_query(site, query),group_by: 1,order_by: 1,- select: {fragment("date_trunc('month', ? at time zone 'utc' at time zone ?)", p.timestamp, ^site.timezone), count(p.user_id, :distinct)}+ select: {fragment("date_trunc('month', ? at time zone 'utc' at time zone ?)", e.timestamp, ^site.timezone), count(e.user_id, :distinct)}) |> Enum.into(%{})|> transform_keys(fn dt -> NaiveDateTime.to_date(dt) end)@@ -42,10 +42,10 @@ defmodule Plausible.Stats dosteps = Enum.into(query.date_range, [])groups = Repo.all(- from p in base_query(site, query),+ from e in base_query(site, query),group_by: 1,order_by: 1,- select: {fragment("date_trunc('day', ? at time zone 'utc' at time zone ?)", p.timestamp, ^site.timezone), count(p.user_id, :distinct)}+ select: {fragment("date_trunc('day', ? at time zone 'utc' at time zone ?)", e.timestamp, ^site.timezone), count(e.user_id, :distinct)}) |> Enum.into(%{})|> transform_keys(fn dt -> NaiveDateTime.to_date(dt) end)@@ -68,10 +68,10 @@ defmodule Plausible.Stats doend)groups = Repo.all(- from p in base_query(site, query),+ from e in base_query(site, query),group_by: 1,order_by: 1,- select: {fragment("date_trunc('hour', ? at time zone 'utc' at time zone ?)", p.timestamp, ^site.timezone), count(p.user_id, :distinct)}+ select: {fragment("date_trunc('hour', ? at time zone 'utc' at time zone ?)", e.timestamp, ^site.timezone), count(e.user_id, :distinct)})|> Enum.into(%{})|> transform_keys(fn dt -> NaiveDateTime.truncate(dt, :second) end)@@ -85,8 +85,8 @@ defmodule Plausible.Stats dodef pageviews_and_visitors(site, query) doRepo.one(from(- p in base_query(site, query),- select: {count(p.id), count(p.user_id, :distinct)}+ e in base_query(site, query),+ select: {count(e.id), count(e.user_id, :distinct)}))end@@ -96,43 +96,43 @@ defmodule Plausible.Stats dodef unique_visitors(site, query) doRepo.one(from(- p in base_query(site, query),- select: count(p.user_id, :distinct)+ e in base_query(site, query),+ select: count(e.user_id, :distinct)))enddef top_referrers(site, query, limit \\ 5) do- Repo.all(from p in base_query(site, query),- select: {p.referrer_source, count(p.referrer_source)},- group_by: p.referrer_source,- where: p.new_visitor == true and not is_nil(p.referrer_source),+ Repo.all(from e in base_query(site, query),+ select: {e.referrer_source, count(e.referrer_source)},+ group_by: e.referrer_source,+ where: e.new_visitor == true and not is_nil(e.referrer_source),order_by: [desc: 2],limit: ^limit)enddef visitors_from_referrer(site, query, referrer) do- Repo.one(from p in base_query(site, query),- select: count(p),- where: p.new_visitor == true and p.referrer_source == ^referrer+ Repo.one(from e in base_query(site, query),+ select: count(e),+ where: e.new_visitor == true and e.referrer_source == ^referrer)enddef referrer_drilldown(site, query, referrer) do- Repo.all(from p in base_query(site, query),- select: {p.referrer, count(p)},- group_by: p.referrer,- where: p.new_visitor == true and p.referrer_source == ^referrer,+ Repo.all(from e in base_query(site, query),+ select: {e.referrer, count(e)},+ group_by: e.referrer,+ where: e.new_visitor == true and e.referrer_source == ^referrer,order_by: [desc: 2],limit: 100)enddef top_pages(site, query, limit \\ 5) do- Repo.all(from p in base_query(site, query),- select: {p.pathname, count(p.pathname)},- group_by: p.pathname,- order_by: [desc: count(p.pathname)],+ Repo.all(from e in base_query(site, query),+ select: {e.pathname, count(e.pathname)},+ group_by: e.pathname,+ order_by: [desc: count(e.pathname)],limit: ^limit)end@@ -140,10 +140,10 @@ defmodule Plausible.Stats do@available_screen_sizes ["Desktop", "Laptop", "Tablet", "Mobile"]def top_screen_sizes(site, query) do- Repo.all(from p in base_query(site, query),- select: {p.screen_size, count(p.screen_size)},- group_by: p.screen_size,- where: p.new_visitor == true and not is_nil(p.screen_size)+ Repo.all(from e in base_query(site, query),+ select: {e.screen_size, count(e.screen_size)},+ group_by: e.screen_size,+ where: e.new_visitor == true and not is_nil(e.screen_size)) |> Enum.sort(fn {screen_size1, _}, {screen_size2, _} ->index1 = Enum.find_index(@available_screen_sizes, fn s -> s == screen_size1 end)index2 = Enum.find_index(@available_screen_sizes, fn s -> s == screen_size2 end)@@ -152,11 +152,11 @@ defmodule Plausible.Stats doenddef countries(site, query, limit \\ 5) do- Repo.all(from p in base_query(site, query),- select: {p.country_code, count(p.country_code)},- group_by: p.country_code,- where: p.new_visitor == true and not is_nil(p.country_code),- order_by: [desc: count(p.country_code)],+ Repo.all(from e in base_query(site, query),+ select: {e.country_code, count(e.country_code)},+ group_by: e.country_code,+ where: e.new_visitor == true and not is_nil(e.country_code),+ order_by: [desc: count(e.country_code)],limit: ^limit) |> Enum.map(fn {country_code, count} ->{Plausible.Stats.CountryName.from_iso3166(country_code), count}@@ -164,31 +164,31 @@ defmodule Plausible.Stats doenddef browsers(site, query, limit \\ 5) do- Repo.all(from p in base_query(site, query),- select: {p.browser, count(p.browser)},- group_by: p.browser,- where: p.new_visitor == true and not is_nil(p.browser),- order_by: [desc: count(p.browser)],+ Repo.all(from e in base_query(site, query),+ select: {e.browser, count(e.browser)},+ group_by: e.browser,+ where: e.new_visitor == true and not is_nil(e.browser),+ order_by: [desc: count(e.browser)],limit: ^limit)enddef operating_systems(site, query, limit \\ 5) do- Repo.all(from p in base_query(site, query),- select: {p.operating_system, count(p.operating_system)},- group_by: p.operating_system,- where: p.new_visitor == true and not is_nil(p.operating_system),- order_by: [desc: count(p.operating_system)],+ Repo.all(from e in base_query(site, query),+ select: {e.operating_system, count(e.operating_system)},+ group_by: e.operating_system,+ where: e.new_visitor == true and not is_nil(e.operating_system),+ order_by: [desc: count(e.operating_system)],limit: ^limit)enddef current_visitors(site) doRepo.one(- from p in Plausible.Pageview,- where: p.timestamp >= fragment("(now() at time zone 'utc') - '5 minutes'::interval"),- where: p.hostname == ^site.domain,- select: count(p.user_id, :distinct)+ from e in Plausible.Event,+ where: e.timestamp >= fragment("(now() at time zone 'utc') - '5 minutes'::interval"),+ where: e.hostname == ^site.domain,+ select: count(e.user_id, :distinct))end@@ -199,9 +199,9 @@ defmodule Plausible.Stats do{:ok, last} = NaiveDateTime.new(query.date_range.last |> Timex.shift(days: 1), ~T[00:00:00])last_datetime = Timex.to_datetime(last, site.timezone)- from(p in Plausible.Pageview,- where: p.hostname == ^site.domain,- where: p.timestamp >= ^first_datetime and p.timestamp < ^last_datetime+ from(e in Plausible.Event,+ where: e.hostname == ^site.domain,+ where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime)enddiff --git a/lib/plausible_web/controllers/api/external_controller.ex b/lib/plausible_web/controllers/api/external_controller.exindex dc75cb1122..6d717ea543 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.Pageview.changeset(%Plausible.Pageview{}, pageview_attrs)+ Plausible.Event.changeset(%Plausible.Event{}, pageview_attrs)|> Plausible.Repo.insertendenddiff --git a/lib/plausible_web/controllers/api/internal_controller.ex b/lib/plausible_web/controllers/api/internal_controller.exindex 8f082a7ca3..14c43cd7d3 100644--- a/lib/plausible_web/controllers/api/internal_controller.ex+++ b/lib/plausible_web/controllers/api/internal_controller.ex@@ -4,8 +4,8 @@ defmodule PlausibleWeb.Api.InternalController dodef domain_status(conn, %{"domain" => domain}) dohas_pageviews = Repo.exists?(- from p in Plausible.Pageview,- where: p.hostname == ^domain+ from e in Plausible.Event,+ where: e.hostname == ^domain)if has_pageviews dodiff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.exindex 2828022346..abf9886b6d 100644--- a/lib/plausible_web/controllers/site_controller.ex+++ b/lib/plausible_web/controllers/site_controller.ex@@ -90,7 +90,7 @@ defmodule PlausibleWeb.SiteController do|> Repo.preload(:google_auth)Repo.delete_all(from sm in "site_memberships", where: sm.site_id == ^site.id)- Repo.delete_all(from p in "pageviews", where: p.hostname == ^site.domain)+ Repo.delete_all(from e in "events", where: e.hostname == ^site.domain)if site.google_auth doRepo.delete!(site.google_auth)diff --git a/priv/repo/migrations/20191024062200_rename_pageviews_to_events.exs b/priv/repo/migrations/20191024062200_rename_pageviews_to_events.exsnew file mode 100644index 0000000000..5ed57a6d74--- /dev/null+++ b/priv/repo/migrations/20191024062200_rename_pageviews_to_events.exs@@ -0,0 +1,7 @@+defmodule Plausible.Repo.Migrations.RenamePageviewsToEvents do+ use Ecto.Migration++ def change do+ rename table("pageviews"), to: table("events")+ end+enddiff --git a/test/plausible/auth/auth_test.exs b/test/plausible/auth/auth_test.exsnew file mode 100644index 0000000000..34aa37b4cf--- /dev/null+++ b/test/plausible/auth/auth_test.exs@@ -0,0 +1,21 @@+defmodule Plausible.AuthTest do+ use Plausible.DataCase+ alias Plausible.Auth++ describe "user_completed_setup?" do+ test "is false if user does not have any events" do+ user = insert(:user)+ insert(:site, members: [user])++ refute Auth.user_completed_setup?(user)+ end++ test "is true if user does have events" do+ user = insert(:user)+ site = insert(:site, members: [user])+ insert(:pageview, hostname: site.domain)++ assert Auth.user_completed_setup?(user)+ end+ end+enddiff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exsindex 4452c9dcfa..d9741ba79b 100644--- a/test/plausible/billing/billing_test.exs+++ b/test/plausible/billing/billing_test.exs@@ -2,6 +2,23 @@ defmodule Plausible.BillingTest douse Plausible.DataCasealias Plausible.Billing+ describe "usage" do+ test "is 0 with no events" do+ user = insert(:user)++ assert Billing.usage(user) == 0+ end++ test "counts the total number of events" do+ user = insert(:user)+ site = insert(:site, members: [user])+ insert(:pageview, hostname: site.domain)+ insert(:pageview, hostname: site.domain)++ assert Billing.usage(user) == 2+ end+ end+describe "trial_days_left" dotest "is 30 days for new signup" douser = insert(:user)diff --git a/test/plausible/sites_test.exs b/test/plausible/sites_test.exsnew file mode 100644index 0000000000..88b6be153d--- /dev/null+++ b/test/plausible/sites_test.exs@@ -0,0 +1,35 @@+defmodule Plausible.SitesTest do+ use Plausible.DataCase+ alias Plausible.Sites++ describe "has_pageviews?" do+ test "is true if site has pageviews" do+ site = insert(:site)+ insert(:pageview, hostname: site.domain)++ assert Sites.has_pageviews?(site)+ end++ test "is false if site does not have pageviews" do+ site = insert(:site)++ refute Sites.has_pageviews?(site)+ end+ end++ describe "is_owner?" do+ test "is true if user is the owner of the site" do+ user = insert(:user)+ site = insert(:site, members: [user])++ assert Sites.is_owner?(user.id, site)+ end++ test "is false if user is not the owner" do+ user = insert(:user)+ site = insert(:site)++ refute Sites.is_owner?(user.id, site)+ end+ end+enddiff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exsindex 64d116e33f..db9604c691 100644--- a/test/plausible_web/controllers/api/external_controller_test.exs+++ b/test/plausible_web/controllers/api/external_controller_test.exs@@ -21,7 +21,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("cf-ipcountry", @country_code)|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert response(conn, 202) == ""assert pageview.hostname == "gigride.live"@@ -41,7 +41,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("content-type", "text/plain")|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert pageview.hostname == "example.com"end@@ -57,7 +57,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("user-agent", "generic crawler")|> post("/api/page", Jason.encode!(params))- pageviews = Repo.all(Plausible.Pageview)+ pageviews = Repo.all(Plausible.Event)assert Enum.count(pageviews) == 0end@@ -74,7 +74,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("user-agent", @user_agent)|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert response(conn, 202) == ""assert pageview.operating_system == "Mac"@@ -94,7 +94,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("user-agent", @user_agent)|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert response(conn, 202) == ""assert pageview.referrer_source == "Facebook"@@ -113,7 +113,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("user-agent", @user_agent)|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert response(conn, 202) == ""assert pageview.referrer_source == nil@@ -132,7 +132,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("user-agent", @user_agent)|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert response(conn, 202) == ""assert pageview.referrer_source == nil@@ -151,7 +151,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("user-agent", @user_agent)|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert response(conn, 202) == ""assert pageview.referrer_source == "blog.gigride.live"@@ -169,7 +169,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("content-type", "text/plain")|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert pageview.referrer == "indiehackers.com/page"end@@ -186,7 +186,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("content-type", "text/plain")|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert pageview.referrer_source == "traffic-source"end@@ -203,7 +203,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("content-type", "text/plain")|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert pageview.referrer_source == "traffic-source"end@@ -221,7 +221,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("user-agent", @user_agent)|> post("/api/page", Jason.encode!(params))- assert Repo.aggregate(Plausible.Pageview, :count, :id) == 0+ assert Repo.aggregate(Plausible.Event, :count, :id) == 0endtest "if it's an :unknown referrer, just the domain is used", %{conn: conn} do@@ -237,7 +237,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("user-agent", @user_agent)|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert response(conn, 202) == ""assert pageview.referrer_source == "indiehackers.com"@@ -256,7 +256,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("user-agent", @user_agent)|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert response(conn, 202) == ""assert is_nil(pageview.referrer_source)@@ -277,7 +277,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("user-agent", @user_agent)|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert response(conn, 202) == ""assert pageview.screen_size == "Mobile"@@ -295,7 +295,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do|> put_req_header("user-agent", @user_agent)|> post("/api/page", Jason.encode!(params))- pageview = Repo.one(Plausible.Pageview)+ pageview = Repo.one(Plausible.Event)assert response(conn, 202) == ""assert pageview.screen_size == nildiff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exsindex 8762ab82d3..a4ebe48e66 100644--- a/test/plausible_web/controllers/site_controller_test.exs+++ b/test/plausible_web/controllers/site_controller_test.exs@@ -120,7 +120,7 @@ defmodule PlausibleWeb.SiteControllerTest dodelete(conn, "/#{site.domain}")refute Repo.exists?(from s in Plausible.Site, where: s.id == ^site.id)- refute Repo.exists?(from p in Plausible.Pageview, where: p.id == ^pageview.id)+ refute Repo.exists?(from e in Plausible.Event, where: e.id == ^pageview.id)endendenddiff --git a/test/support/factory.ex b/test/support/factory.exindex a51ef8e5ff..ef28ff4df7 100644--- a/test/support/factory.ex+++ b/test/support/factory.ex@@ -25,7 +25,7 @@ defmodule Plausible.Factory dodef pageview_factory dohostname = sequence(:domain, &"example-#{&1}.com")- %Plausible.Pageview{+ %Plausible.Event{hostname: hostname,pathname: "/",new_visitor: true,