Channels: Write click_id_param column (#4704)

* Add migration for click_id_source

* click_id_param

* Write click_id_source column

* Add columns to schema

* click_id_param
This commit is contained in:
Karl-Aksel Puulmann 2024-11-05 10:04:04 +02:00 committed by GitHub
parent 0ec8ac6d7c
commit 1048967ac5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 21 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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

View File

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