Store referrers from android apps (#3715)

* Store referrers from android apps

* Add test for unknown referrer protocol

* Store android referrer protocol
dependabot/npm_and_yarn/tracker/eslint-plugin-playwright-1.5.4 r2024.03.21-406
Uku Taht 3 years ago committed by GitHub
parent 8992c8ee07
commit fd879eeb16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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

@ -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

@ -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

Loading…
Cancel
Save