Skip to content

Commit

Permalink
Map lowercase tagged sources to capitalized form during ingestion
Browse files Browse the repository at this point in the history
  • Loading branch information
ukutaht committed Aug 1, 2024
1 parent b868042 commit 2b778f0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
7 changes: 5 additions & 2 deletions lib/mix/tasks/send_pageview.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Mix.Tasks.SendPageview do
@default_referrer "https://google.com"
@default_event "pageview"
@default_props "{}"
@default_queryparams ""
@options [
ip: :string,
user_agent: :string,
Expand All @@ -28,7 +29,8 @@ defmodule Mix.Tasks.SendPageview do
event: :string,
props: :string,
revenue_currency: :string,
revenue_amount: :string
revenue_amount: :string,
queryparams: :string
]

def run(opts) do
Expand Down Expand Up @@ -88,6 +90,7 @@ defmodule Mix.Tasks.SendPageview do
event = Keyword.get(opts, :event, @default_event)
props = Keyword.get(opts, :props, @default_props)
hostname = Keyword.get(opts, :hostname, domain)
queryparams = Keyword.get(opts, :queryparams, @default_queryparams)

revenue =
if Keyword.get(opts, :revenue_currency) do
Expand All @@ -99,7 +102,7 @@ defmodule Mix.Tasks.SendPageview do

%{
name: event,
url: "http://#{hostname}#{page}",
url: "http://#{hostname}#{page}?#{queryparams}",
domain: domain,
referrer: referrer,
props: props,
Expand Down
1 change: 1 addition & 0 deletions lib/plausible/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ defmodule Plausible.Application do

setup_geolocation()
Location.load_all()
Plausible.Ingestion.Acquisition.init()
Plausible.Geo.await_loader()

Supervisor.start_link(List.flatten(children), opts)
Expand Down
25 changes: 25 additions & 0 deletions lib/plausible/ingestion/acquisition.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Plausible.Ingestion.Acquisition do
def init() do
:ets.new(__MODULE__, [
:named_table,
:set,
:public,
{:read_concurrency, true}
])

[{"referers.yml", map}] = RefInspector.Database.list(:default)

Enum.flat_map(map, fn {_, entries} ->
Enum.map(entries, fn {_, _, _, _, _, _, name} ->
:ets.insert(__MODULE__, {String.downcase(name), name})
end)
end)
end

def find_mapping(source) do
case :ets.lookup(__MODULE__, source) do
[{_, name}] -> name
_ -> source
end
end
end
8 changes: 6 additions & 2 deletions lib/plausible/ingestion/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,16 @@ defmodule Plausible.Ingestion.Event do
end

defp get_referrer_source(request, ref) do
source =
tagged_source =
request.query_params["utm_source"] ||
request.query_params["source"] ||
request.query_params["ref"]

source || PlausibleWeb.RefInspector.parse(ref)
if tagged_source do
Plausible.Ingestion.Acquisition.find_mapping(tagged_source)
else
PlausibleWeb.RefInspector.parse(ref)
end
end

defp clean_referrer(nil), do: nil
Expand Down
18 changes: 17 additions & 1 deletion test/plausible_web/controllers/api/external_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
assert event.browser_version == "70.0"
end

test "parses referrer", %{conn: conn, site: site} do
test "parses referrer source", %{conn: conn, site: site} do
params = %{
name: "pageview",
url: "http://example.com/",
Expand Down Expand Up @@ -407,6 +407,22 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
assert session.referrer_source == "betalist"
end

test "if utm_source matches a capitalized form from ref_inspector, the capitalized form is recorded",
%{conn: conn, site: site} do
params = %{
name: "pageview",
url: "http://www.example.com/?utm_source=facebook",
domain: site.domain
}

conn
|> post("/api/event", params)

session = get_created_session(site)

assert session.referrer_source == "Facebook"
end

test "utm tags are stored", %{conn: conn, site: site} do
params = %{
name: "pageview",
Expand Down

0 comments on commit 2b778f0

Please sign in to comment.