From 73166774e0b1729e30643dbbe0dfa0e01a719cb6 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Mon, 18 Nov 2024 13:19:11 +0200 Subject: [PATCH] Infer medium from click id if not present (#4817) --- lib/plausible/ingestion/event.ex | 13 +++++ .../api/external_controller_test.exs | 58 ++++++++++++++++++- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/lib/plausible/ingestion/event.ex b/lib/plausible/ingestion/event.ex index 1027e1075308..f94c81bc4be3 100644 --- a/lib/plausible/ingestion/event.ex +++ b/lib/plausible/ingestion/event.ex @@ -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" -> diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exs index 81180b392865..a5244488ef7b 100644 --- a/test/plausible_web/controllers/api/external_controller_test.exs +++ b/test/plausible_web/controllers/api/external_controller_test.exs @@ -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",