Skip to content

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

Set root domain `(none)` for ingests without hostname (#4678)

hq1023e2041fb9f
diff --git a/lib/plausible/ingestion/event.ex b/lib/plausible/ingestion/event.ex
index 94e13d04c2..f177ee9408 100644
--- a/lib/plausible/ingestion/event.ex
+++ b/lib/plausible/ingestion/event.ex
@@ -541,7 +541,7 @@ defmodule Plausible.Ingestion.Event do
end
end
- defp get_root_domain(nil), do: "(none)"
+ defp get_root_domain("(none)"), do: "(none)"
defp get_root_domain(hostname) do
case :inet.parse_ipv4_address(String.to_charlist(hostname)) do
diff --git a/lib/plausible/ingestion/request.ex b/lib/plausible/ingestion/request.ex
index fa2218611b..407b4c0325 100644
--- a/lib/plausible/ingestion/request.ex
+++ b/lib/plausible/ingestion/request.ex
@@ -194,11 +194,11 @@ defmodule Plausible.Ingestion.Request do
defp put_hostname(changeset) do
host =
case Changeset.get_field(changeset, :uri) do
- %{host: host} when is_binary(host) and host != "" -> host
+ %{host: host} when is_binary(host) and host != "" -> sanitize_hostname(host)
_ -> "(none)"
end
- Changeset.put_change(changeset, :hostname, sanitize_hostname(host))
+ Changeset.put_change(changeset, :hostname, host)
end
@max_props 30
diff --git a/test/plausible/ingestion/event_test.exs b/test/plausible/ingestion/event_test.exs
index bc2a63696a..88fd25aab4 100644
--- a/test/plausible/ingestion/event_test.exs
+++ b/test/plausible/ingestion/event_test.exs
@@ -397,4 +397,20 @@ defmodule Plausible.Ingestion.EventTest do
assert {:ok, %{buffered: [event]}} = Event.build_and_buffer(request)
assert event.clickhouse_event.hostname == "foo.netlify.app"
end
+
+ test "hostname is (none) when no hostname can be derived from the url" do
+ site = insert(:site, domain: "foo.example.com")
+
+ payload = %{
+ domain: site.domain,
+ name: "pageview",
+ url: "/no/hostname"
+ }
+
+ conn = build_conn(:post, "/api/events", payload)
+ assert {:ok, request} = Request.build(conn)
+
+ assert {:ok, %{buffered: [event]}} = Event.build_and_buffer(request)
+ assert event.clickhouse_event.hostname == "(none)"
+ end
end