diff --git a/lib/plausible/clickhouse_event_v2.ex b/lib/plausible/clickhouse_event_v2.ex index 017d57639..77dfd446b 100644 --- a/lib/plausible/clickhouse_event_v2.ex +++ b/lib/plausible/clickhouse_event_v2.ex @@ -26,6 +26,7 @@ defmodule Plausible.ClickhouseEventV2 do # Session attributes field :referrer, :string field :referrer_source, :string + field :click_id_param, Ch, type: "LowCardinality(String)" field :channel, Ch, type: "LowCardinality(String)" field :utm_medium, :string field :utm_source, :string @@ -72,6 +73,7 @@ defmodule Plausible.ClickhouseEventV2 do :referrer, :referrer_source, :channel, + :click_id_param, :utm_medium, :utm_source, :utm_campaign, diff --git a/lib/plausible/clickhouse_session_v2.ex b/lib/plausible/clickhouse_session_v2.ex index edfbc233c..ef1ead43c 100644 --- a/lib/plausible/clickhouse_session_v2.ex +++ b/lib/plausible/clickhouse_session_v2.ex @@ -59,6 +59,7 @@ defmodule Plausible.ClickhouseSessionV2 do field :referrer, :string field :referrer_source, :string field :channel, Ch, type: "LowCardinality(String)" + field :click_id_param, Ch, type: "LowCardinality(String)" field :country_code, Ch, type: "LowCardinality(FixedString(2))" field :subdivision1_code, Ch, type: "LowCardinality(String)" diff --git a/lib/plausible/ingestion/event.ex b/lib/plausible/ingestion/event.ex index 4c2c88769..313bf25df 100644 --- a/lib/plausible/ingestion/event.ex +++ b/lib/plausible/ingestion/event.ex @@ -257,7 +257,8 @@ defmodule Plausible.Ingestion.Event do update_session_attrs(event, %{ channel: channel, referrer_source: source, - referrer: Plausible.Ingestion.Source.format_referrer(event.request.referrer) + referrer: Plausible.Ingestion.Source.format_referrer(event.request.referrer), + click_id_param: get_click_id_param(event.request.query_params) }) end @@ -391,6 +392,15 @@ defmodule Plausible.Ingestion.Event do event end + @click_id_params ["gclid", "gbraid", "wbraid", "msclkid", "fbclid", "twclid"] + + defp get_click_id_param(nil), do: nil + + defp get_click_id_param(query_params) do + @click_id_params + |> Enum.find(fn param_name -> Map.has_key?(query_params, param_name) end) + end + defp parse_user_agent(%Request{user_agent: user_agent}) when is_binary(user_agent) do Plausible.Cache.Adapter.get(:user_agents, user_agent, fn -> UAInspector.parse(user_agent) diff --git a/lib/plausible/session/cache_store.ex b/lib/plausible/session/cache_store.ex index c306d9730..bd171d8a0 100644 --- a/lib/plausible/session/cache_store.ex +++ b/lib/plausible/session/cache_store.ex @@ -116,6 +116,7 @@ defmodule Plausible.Session.CacheStore do events: 1, referrer: Map.get(session_attributes, :referrer), channel: Map.get(session_attributes, :channel), + click_id_param: Map.get(session_attributes, :click_id_param), referrer_source: Map.get(session_attributes, :referrer_source), utm_medium: Map.get(session_attributes, :utm_medium), utm_source: Map.get(session_attributes, :utm_source), diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exs index fd84e99f3..7578b5681 100644 --- a/test/plausible_web/controllers/api/external_controller_test.exs +++ b/test/plausible_web/controllers/api/external_controller_test.exs @@ -256,6 +256,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert response(conn, 202) == "ok" assert session.referrer_source == "Facebook" + assert session.click_id_param == "" end test "strips trailing slash from referrer", %{conn: conn, site: site} do @@ -1340,6 +1341,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert response(conn, 202) == "ok" assert session.channel == "Paid Search" + assert session.click_id_param == "gclid" end test "is not paid search when gclid is present on non-google referrer", %{ @@ -1362,6 +1364,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert response(conn, 202) == "ok" assert session.channel == "Organic Search" + assert session.click_id_param == "gclid" end test "parses paid search channel based on msclkid", %{conn: conn, site: site} do @@ -1381,6 +1384,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert response(conn, 202) == "ok" assert session.channel == "Paid Search" + assert session.click_id_param == "msclkid" end test "is not paid search when msclkid is present on non-bing referrer", %{ @@ -1403,6 +1407,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert response(conn, 202) == "ok" assert session.channel == "Organic Search" + assert session.click_id_param == "msclkid" end test "parses paid search channel based on utm_source and medium", %{conn: conn, site: site} do @@ -1421,6 +1426,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert response(conn, 202) == "ok" assert session.channel == "Paid Search" + assert session.click_id_param == "" end test "parses paid social channel based on referrer and medium", %{conn: conn, site: site} do