Skip to content

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

Store referrers from android apps (#3715)

Uku Tahtfd879eeb1656
diff --git a/lib/plausible/ingestion/event.ex b/lib/plausible/ingestion/event.ex
index 3211055c50..8c66c01d71 100644
--- a/lib/plausible/ingestion/event.ex
+++ b/lib/plausible/ingestion/event.ex
@@ -381,9 +381,7 @@ defmodule Plausible.Ingestion.Event do
uri = URI.parse(ref.referer)
if PlausibleWeb.RefInspector.right_uri?(uri) do
- host = String.replace_prefix(uri.host, "www.", "")
- path = uri.path || ""
- host <> String.trim_trailing(path, "/")
+ PlausibleWeb.RefInspector.format_referrer(uri)
end
end
diff --git a/lib/plausible_web/refinspector.ex b/lib/plausible_web/refinspector.ex
index 154ab9476b..0bfbcb456b 100644
--- a/lib/plausible_web/refinspector.ex
+++ b/lib/plausible_web/refinspector.ex
@@ -7,7 +7,7 @@ defmodule PlausibleWeb.RefInspector do
uri = URI.parse(String.trim(ref.referer))
if right_uri?(uri) do
- String.replace_leading(uri.host, "www.", "")
+ format_referrer_host(uri)
end
source ->
@@ -15,11 +15,23 @@ defmodule PlausibleWeb.RefInspector do
end
end
+ def format_referrer(uri) do
+ path = String.trim_trailing(uri.path || "", "/")
+ format_referrer_host(uri) <> path
+ end
+
def right_uri?(%URI{host: nil}), do: false
def right_uri?(%URI{host: host, scheme: scheme})
- when scheme in ["http", "https"] and byte_size(host) > 0,
+ when scheme in ["http", "https", "android-app"] and byte_size(host) > 0,
do: true
def right_uri?(_), do: false
+
+ defp format_referrer_host(uri) do
+ protocol = if uri.scheme == "android-app", do: "android-app://", else: ""
+ host = String.replace_prefix(uri.host, "www.", "")
+
+ protocol <> host
+ end
end
diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exs
index e37ec9e94d..868c73c4bb 100644
--- a/test/plausible_web/controllers/api/external_controller_test.exs
+++ b/test/plausible_web/controllers/api/external_controller_test.exs
@@ -444,11 +444,31 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
assert session.referrer_source == "indiehackers.com"
end
- test "if the referrer is not http or https, it is ignored", %{conn: conn, site: site} do
+ test "if the referrer is not http, https, or android it is ignored", %{conn: conn, site: site} do
params = %{
name: "pageview",
url: "http://example.com/",
- referrer: "android-app://random",
+ referrer: "ftp://wat",
+ domain: site.domain
+ }
+
+ conn =
+ conn
+ |> put_req_header("user-agent", @user_agent)
+ |> post("/api/event", params)
+
+ pageview = get_event(site)
+
+ assert response(conn, 202) == "ok"
+ assert pageview.referrer_source == ""
+ assert pageview.referrer == ""
+ end
+
+ test "stores referrer from android app", %{conn: conn, site: site} do
+ params = %{
+ name: "pageview",
+ url: "http://example.com/",
+ referrer: "android-app://some.android.app",
domain: site.domain
}
@@ -460,7 +480,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
session = get_created_session(site)
assert response(conn, 202) == "ok"
- assert session.referrer_source == ""
+ assert session.referrer == "android-app://some.android.app"
+ assert session.referrer_source == "android-app://some.android.app"
end
test "screen size is calculated from user agent", %{conn: conn, site: site} do