Infer medium from click id if not present (#4817)

This commit is contained in:
Uku Taht 2024-11-18 13:19:11 +02:00 committed by GitHub
parent 916c2bb4c5
commit 73166774e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 68 additions and 3 deletions

View File

@ -119,6 +119,7 @@ defmodule Plausible.Ingestion.Event do
put_user_agent: &put_user_agent/2,
put_basic_info: &put_basic_info/2,
put_source_info: &put_source_info/2,
maybe_infer_medium: &maybe_infer_medium/2,
put_props: &put_props/2,
put_revenue: &put_revenue/2,
put_salts: &put_salts/2,
@ -269,6 +270,18 @@ defmodule Plausible.Ingestion.Event do
})
end
defp maybe_infer_medium(%__MODULE__{} = event, _context) do
inferred_medium =
case event.clickhouse_session_attrs do
%{utm_medium: medium} when is_binary(medium) -> medium
%{utm_medium: nil, referrer_source: "Google", click_id_param: "gclid"} -> "(gclid)"
%{utm_medium: nil, referrer_source: "Bing", click_id_param: "msclkid"} -> "(msclkid)"
_ -> nil
end
update_session_attrs(event, %{utm_medium: inferred_medium})
end
defp put_geolocation(%__MODULE__{} = event, _context) do
case event.request.ip_classification do
"anonymous_vpn_ip" ->

View File

@ -1374,6 +1374,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
assert response(conn, 202) == "ok"
assert session.acquisition_channel == "Paid Search"
assert session.utm_medium == "(gclid)"
assert session.click_id_param == "gclid"
end
@ -1397,6 +1398,31 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
assert response(conn, 202) == "ok"
assert session.acquisition_channel == "Organic Search"
assert session.utm_medium == ""
assert session.click_id_param == "gclid"
end
test "does not override utm_medium with (gclid) if link is already tagged", %{
conn: conn,
site: site
} do
params = %{
name: "pageview",
url: "http://example.com?gclid=123identifier&utm_medium=paidads",
referrer: "https://google.com",
domain: site.domain
}
conn =
conn
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", params)
session = get_created_session(site)
assert response(conn, 202) == "ok"
assert session.acquisition_channel == "Paid Search"
assert session.utm_medium == "paidads"
assert session.click_id_param == "gclid"
end
@ -1417,6 +1443,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
assert response(conn, 202) == "ok"
assert session.acquisition_channel == "Paid Search"
assert session.utm_medium == "(msclkid)"
assert session.click_id_param == "msclkid"
end
@ -1426,8 +1453,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
} do
params = %{
name: "pageview",
url: "http://example.com?msclkid=123identifier",
referrer: "https://duckduckgo.com",
url: "http://example.com?msclkid=123identifier&utm_medium=cpc",
referrer: "https://bing.com",
domain: site.domain
}
@ -1439,10 +1466,35 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
session = get_created_session(site)
assert response(conn, 202) == "ok"
assert session.acquisition_channel == "Organic Search"
assert session.acquisition_channel == "Paid Search"
assert session.utm_medium == "cpc"
assert session.click_id_param == "msclkid"
end
test "does not override utm_medium with (msclkid) if link is already tagged", %{
conn: conn,
site: site
} do
params = %{
name: "pageview",
url: "http://example.com?gclid=123identifier&utm_medium=paidads",
referrer: "https://google.com",
domain: site.domain
}
conn =
conn
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", params)
session = get_created_session(site)
assert response(conn, 202) == "ok"
assert session.acquisition_channel == "Paid Search"
assert session.utm_medium == "paidads"
assert session.click_id_param == "gclid"
end
test "parses paid search channel based on utm_source and medium", %{conn: conn, site: site} do
params = %{
name: "pageview",