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
Show current visitors
Uku Taht00d02faad0b8
diff --git a/assets/js/stats/index.js b/assets/js/stats/index.jsindex 87a9dbc5a4..b2cf07b55e 100644--- a/assets/js/stats/index.js+++ b/assets/js/stats/index.js@@ -125,4 +125,13 @@ if (domainEl) {router.updateLinkHandlers()})})++ setInterval(function() {+ fetch(`/api/${domain}/current-visitors`)+ .then(res => res.json())+ .then((res) => {+ console.log(res)+ document.getElementById('current-visitors').innerHTML = res+ })+ }, 10000)}diff --git a/lib/plausible/stats/stats.ex b/lib/plausible/stats/stats.exindex 775398e177..9e5a0aebff 100644--- a/lib/plausible/stats/stats.ex+++ b/lib/plausible/stats/stats.ex@@ -181,6 +181,15 @@ defmodule Plausible.Stats do)end+ def current_visitors(site) do+ Repo.one(+ from p in Plausible.Pageview,+ where: p.inserted_at >= fragment("(now() at time zone 'utc') - '5 minutes'::interval"),+ where: p.hostname == ^site.domain,+ select: count(p.user_id, :distinct)+ )+ end+defp base_query(site, query) do{:ok, first} = NaiveDateTime.new(query.date_range.first, ~T[00:00:00])first_datetime = Timex.to_datetime(first, site.timezone)diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.exindex aeb90ea3aa..278700fa9b 100644--- a/lib/plausible_web/controllers/stats_controller.ex+++ b/lib/plausible_web/controllers/stats_controller.ex@@ -23,12 +23,14 @@ defmodule PlausibleWeb.StatsController do{conn, params} = fetch_period(conn, site)query = Stats.Query.from(site.timezone, params)+ current_visitors = Stats.current_visitors(site)conn|> assign(:skip_plausible_tracking, !demo)|> render("stats.html",site: site,query: query,+ current_visitors: current_visitors,title: "Plausible · " <> site.domain)else@@ -295,6 +297,16 @@ defmodule PlausibleWeb.StatsController doendend+ def current_visitors(conn, %{"domain" => domain}) do+ site = Repo.get_by(Plausible.Site, domain: domain)++ if site && current_user_can_access?(conn, site) do+ json(conn, Stats.current_visitors(site))+ else+ render_error(conn, 404)+ end+ end+defp current_user_can_access?(_conn, %Plausible.Site{public: true}) dotrueenddiff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.exindex 2a18895ba9..61dc2edfb2 100644--- a/lib/plausible_web/router.ex+++ b/lib/plausible_web/router.ex@@ -42,6 +42,7 @@ defmodule PlausibleWeb.Router doget "/:domain/operating-systems", StatsController, :operating_systemsget "/:domain/browsers", StatsController, :browsersget "/:domain/compare", StatsController, :compare+ get "/:domain/current-visitors", StatsController, :current_visitorsendscope "/", PlausibleWeb dodiff --git a/lib/plausible_web/templates/stats/stats.html.eex b/lib/plausible_web/templates/stats/stats.html.eexindex 9c519d6d0d..e4a6da213b 100644--- a/lib/plausible_web/templates/stats/stats.html.eex+++ b/lib/plausible_web/templates/stats/stats.html.eex@@ -1,7 +1,15 @@<div class="pt-12"></div><div class="container pb-32" data-site-domain="<%= @site.domain %>"><div class="w-full sm:flex justify-between items-center">- <h2 class="text-center sm:text-left sm:mr-8">Analytics for <%= link(@site.domain, to: "//" <> @site.domain, target: "_blank") %></h2>+ <div class="w-full sm:flex items-center">+ <h2 class="text-center sm:text-left sm:mr-8">Analytics for <%= link(@site.domain, to: "//" <> @site.domain, target: "_blank") %></h2>+ <div class="text-sm font-bold text-grey-darker">+ <svg class="w-2 mr-1 fill-current text-green" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">+ <circle cx="8" cy="8" r="8"/>+ </svg>+ <span id="current-visitors"><%= @current_visitors %></span> current visitors+ </div>+ </div><div class="mt-4 sm:mt-0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css"><script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>diff --git a/test/plausible/stats/stats_test.exs b/test/plausible/stats/stats_test.exsindex 68f2e1a523..57e4f21ad5 100644--- a/test/plausible/stats/stats_test.exs+++ b/test/plausible/stats/stats_test.exs@@ -143,6 +143,48 @@ defmodule Plausible.StatsTest doendend+ describe "current_visitors" do+ test "counts user ids seen in the last 5 minutes" do+ site = insert(:site)++ insert(:pageview, %{+ hostname: site.domain,+ user_id: UUID.uuid4(),+ })++ insert(:pageview, %{+ hostname: site.domain,+ user_id: UUID.uuid4(),+ inserted_at: Timex.now() |> Timex.shift(minutes: -4)+ })++ insert(:pageview, %{+ hostname: site.domain,+ user_id: UUID.uuid4(),+ inserted_at: Timex.now() |> Timex.shift(minutes: -6)+ })++ assert Stats.current_visitors(site) == 2+ end++ test "counts unique user ids" do+ site = insert(:site)+ id = UUID.uuid4()++ insert(:pageview, %{+ hostname: site.domain,+ user_id: id,+ })++ insert(:pageview, %{+ hostname: site.domain,+ user_id: id,+ })++ assert Stats.current_visitors(site) == 1+ end+ end+defp months_ago(months) doTimex.now() |> Timex.shift(months: -months)end