Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Disable sampling in the API

Uku Taht63b7abad58a5
diff --git a/lib/plausible/stats/base.ex b/lib/plausible/stats/base.ex
index 1a7aadd9eb..c293aa6aa9 100644
--- a/lib/plausible/stats/base.ex
+++ b/lib/plausible/stats/base.ex
@@ -30,11 +30,19 @@ defmodule Plausible.Stats.Base do
q =
from(
e in "events",
- hints: [sample: query.sample_threshold],
where: e.domain == ^site.domain,
where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime
)
+ q =
+ case query.sample_threshold do
+ "infinite" ->
+ q
+
+ threshold ->
+ from(e in q, hints: [sample: threshold])
+ end
+
q =
case query.filters["event:page"] do
{:is, page} ->
diff --git a/lib/plausible/stats/query.ex b/lib/plausible/stats/query.ex
index b1c2e1d2a4..c933b3a2f5 100644
--- a/lib/plausible/stats/query.ex
+++ b/lib/plausible/stats/query.ex
@@ -5,6 +5,8 @@ defmodule Plausible.Stats.Query do
filters: %{},
sample_threshold: 10_000_000
+ @default_sample_threshold 10_000_000
+
def shift_back(%__MODULE__{period: "month"} = query, site) do
# Querying current month to date
{new_first, new_last} =
@@ -42,7 +44,8 @@ defmodule Plausible.Stats.Query do
period: "30m",
interval: "minute",
date_range: Date.range(date, date),
- filters: parse_filters(params)
+ filters: parse_filters(params),
+ sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@@ -53,7 +56,8 @@ defmodule Plausible.Stats.Query do
period: "day",
date_range: Date.range(date, date),
interval: "hour",
- filters: parse_filters(params)
+ filters: parse_filters(params),
+ sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@@ -65,7 +69,8 @@ defmodule Plausible.Stats.Query do
period: "7d",
date_range: Date.range(start_date, end_date),
interval: "date",
- filters: parse_filters(params)
+ filters: parse_filters(params),
+ sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@@ -77,7 +82,8 @@ defmodule Plausible.Stats.Query do
period: "30d",
date_range: Date.range(start_date, end_date),
interval: "date",
- filters: parse_filters(params)
+ filters: parse_filters(params),
+ sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@@ -91,7 +97,8 @@ defmodule Plausible.Stats.Query do
period: "month",
date_range: Date.range(start_date, end_date),
interval: "date",
- filters: parse_filters(params)
+ filters: parse_filters(params),
+ sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@@ -108,7 +115,8 @@ defmodule Plausible.Stats.Query do
period: "6mo",
date_range: Date.range(start_date, end_date),
interval: Map.get(params, "interval", "month"),
- filters: parse_filters(params)
+ filters: parse_filters(params),
+ sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@@ -125,7 +133,8 @@ defmodule Plausible.Stats.Query do
period: "12mo",
date_range: Date.range(start_date, end_date),
interval: Map.get(params, "interval", "month"),
- filters: parse_filters(params)
+ filters: parse_filters(params),
+ sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@@ -148,7 +157,8 @@ defmodule Plausible.Stats.Query do
period: "custom",
date_range: Date.range(from_date, to_date),
interval: Map.get(params, "interval", "date"),
- filters: parse_filters(params)
+ filters: parse_filters(params),
+ sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
diff --git a/lib/plausible_web/controllers/api/external_stats_controller.ex b/lib/plausible_web/controllers/api/external_stats_controller.ex
index e647442c92..2c3b7aa8c9 100644
--- a/lib/plausible_web/controllers/api/external_stats_controller.ex
+++ b/lib/plausible_web/controllers/api/external_stats_controller.ex
@@ -12,6 +12,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController do
def aggregate(conn, params) do
site = conn.assigns[:site]
+ params = Map.put(params, "sample_threshold", "infinite")
with :ok <- validate_period(params),
:ok <- validate_date(params),
@@ -52,6 +53,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController do
def breakdown(conn, params) do
site = conn.assigns[:site]
+ params = Map.put(params, "sample_threshold", "infinite")
with :ok <- validate_period(params),
:ok <- validate_date(params),
@@ -124,6 +126,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController do
def timeseries(conn, params) do
site = conn.assigns[:site]
+ params = Map.put(params, "sample_threshold", "infinite")
with :ok <- validate_period(params),
:ok <- validate_date(params),