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
defmodule Plausible.Stats.CurrentVisitors do
@moduledoc """
API for querying current number of visitors for real-time stats.
"""
use Plausible.ClickhouseRepo
use Plausible.Stats.SQL.Fragments
@spec current_visitors(Plausible.Site.t(), Duration.duration()) :: non_neg_integer
def current_visitors(site, duration \\ Duration.new!(minute: -5)) do
first_datetime =
NaiveDateTime.utc_now()
|> NaiveDateTime.shift(duration)
|> NaiveDateTime.truncate(:second)
ClickhouseRepo.one(
from e in "events_v2",
where: ^Plausible.Sites.site_id_query_filter(site),
where: e.timestamp >= ^first_datetime,
where: e.name != "engagement",
select: uniq(e.user_id)
)
end
end