Skip to content

Commit

Permalink
Read and write channels from acquisition_channel column
Browse files Browse the repository at this point in the history
  • Loading branch information
macobo committed Nov 12, 2024
1 parent 4aa7dec commit 05fa72e
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 133 deletions.
4 changes: 2 additions & 2 deletions lib/plausible/clickhouse_event_v2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ defmodule Plausible.ClickhouseEventV2 do
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
field :utm_campaign, :string
Expand All @@ -44,6 +43,8 @@ defmodule Plausible.ClickhouseEventV2 do
field :operating_system_version, Ch, type: "LowCardinality(String)"
field :browser, Ch, type: "LowCardinality(String)"
field :browser_version, Ch, type: "LowCardinality(String)"

field :acquisition_channel, Ch, type: "LowCardinality(String)", writable: :never
end

def new(attrs) do
Expand Down Expand Up @@ -72,7 +73,6 @@ defmodule Plausible.ClickhouseEventV2 do
:session_id,
:referrer,
:referrer_source,
:channel,
:click_id_param,
:utm_medium,
:utm_source,
Expand Down
3 changes: 2 additions & 1 deletion lib/plausible/clickhouse_session_v2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ defmodule Plausible.ClickhouseSessionV2 do
field :utm_term, :string
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))"
Expand All @@ -74,6 +73,8 @@ defmodule Plausible.ClickhouseSessionV2 do
field :timestamp, :naive_datetime

field :transferred_from, :string

field :acquisition_channel, Ch, type: "LowCardinality(String)", writable: :never
end

def random_uint64() do
Expand Down
16 changes: 0 additions & 16 deletions lib/plausible/ingestion/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ 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,
put_channel: &put_channel/2,
put_props: &put_props/2,
put_revenue: &put_revenue/2,
put_salts: &put_salts/2,
Expand Down Expand Up @@ -270,21 +269,6 @@ defmodule Plausible.Ingestion.Event do
})
end

defp put_channel(%__MODULE__{} = event, _context) do
session = event.clickhouse_session_attrs

channel =
Plausible.Ingestion.Acquisition.get_channel(
session[:referrer_source],
session[:utm_medium],
session[:utm_campaign],
session[:utm_source],
session[:click_id_param]
)

update_session_attrs(event, %{channel: channel})
end

defp put_geolocation(%__MODULE__{} = event, _context) do
case event.request.ip_classification do
"anonymous_vpn_ip" ->
Expand Down
6 changes: 5 additions & 1 deletion lib/plausible/ingestion/write_buffer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ defmodule Plausible.Ingestion.WriteBuffer do

@doc false
def compile_time_prepare(schema) do
fields = schema.__schema__(:fields)
fields =
schema.__schema__(:fields)
|> Enum.reject(&(&1 in fields_to_ignore()))

types =
Enum.map(fields, fn field ->
Expand Down Expand Up @@ -147,4 +149,6 @@ defmodule Plausible.Ingestion.WriteBuffer do
]
}
end

defp fields_to_ignore(), do: [:acquisition_channel]
end
1 change: 0 additions & 1 deletion lib/plausible/session/cache_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ defmodule Plausible.Session.CacheStore do
pageviews: if(event.name == "pageview", do: 1, else: 0),
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),
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/stats/filter_suggestions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ defmodule Plausible.Stats.FilterSuggestions do
"page" -> :pathname
"entry_page" -> :entry_page
"source" -> :referrer_source
"channel" -> :channel
"channel" -> :acquisition_channel
"os" -> :operating_system
"os_version" -> :operating_system_version
"screen" -> :screen_size
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/stats/sql/expression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ defmodule Plausible.Stats.SQL.Expression do
do: field_or_blank_value(q, key, t.source, @no_ref)

def select_dimension(q, key, "visit:channel", _table, _query),
do: field_or_blank_value(q, key, t.channel, @no_channel)
do: field_or_blank_value(q, key, t.acquisition_channel, @no_channel)

def select_dimension(q, key, "visit:referrer", _table, _query),
do: field_or_blank_value(q, key, t.referrer, @no_ref)
Expand Down
7 changes: 5 additions & 2 deletions lib/plausible/stats/sql/where_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ defmodule Plausible.Stats.SQL.WhereBuilder do
[_, "visit:" <> key | _rest] = filter
) do
# Filter events query with visit dimension if possible
field_name = String.to_existing_atom(key)
field_name = db_field_name(key)

if Enum.member?(@sessions_only_visit_fields, field_name) do
true
Expand All @@ -137,7 +137,7 @@ defmodule Plausible.Stats.SQL.WhereBuilder do
end

defp add_filter(:sessions, _query, [_, "visit:" <> key | _rest] = filter) do
filter_field(String.to_existing_atom(key), filter)
filter_field(db_field_name(key), filter)
end

defp add_filter(:sessions, _query, [_, "event:" <> _ | _rest]) do
Expand Down Expand Up @@ -282,6 +282,9 @@ defmodule Plausible.Stats.SQL.WhereBuilder do
@no_ref "Direct / None"
@not_set "(not set)"

defp db_field_name("channel"), do: :acquisition_channel
defp db_field_name(name), do: String.to_existing_atom(name)

defp db_field_val(:source, @no_ref), do: ""
defp db_field_val(:referrer, @no_ref), do: ""
defp db_field_val(:utm_medium, @no_ref), do: ""
Expand Down
Loading

0 comments on commit 05fa72e

Please sign in to comment.