From f185d3736e4189031222226beff7e01b3abd900e Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Tue, 27 Apr 2021 11:10:37 +0300 Subject: [PATCH] Add hostname to user id hash --- lib/plausible/event/clickhouse_schema.ex | 4 ++-- .../controllers/api/external_controller.ex | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/plausible/event/clickhouse_schema.ex b/lib/plausible/event/clickhouse_schema.ex index 44298d27d..20e26773d 100644 --- a/lib/plausible/event/clickhouse_schema.ex +++ b/lib/plausible/event/clickhouse_schema.ex @@ -30,8 +30,8 @@ defmodule Plausible.ClickhouseEvent do timestamps(inserted_at: :timestamp, updated_at: false) end - def changeset(pageview, attrs) do - pageview + def new(attrs) do + %__MODULE__{} |> cast(attrs, [ :name, :domain, diff --git a/lib/plausible_web/controllers/api/external_controller.ex b/lib/plausible_web/controllers/api/external_controller.ex index 4b5d04616..edf2e79e0 100644 --- a/lib/plausible_web/controllers/api/external_controller.ex +++ b/lib/plausible_web/controllers/api/external_controller.ex @@ -84,7 +84,6 @@ defmodule PlausibleWeb.Api.ExternalController do name: params["name"], hostname: strip_www(uri && uri.host), pathname: get_pathname(uri, params["hash_mode"]), - user_id: generate_user_id(conn, params, salts[:current]), referrer_source: get_referrer_source(query, ref) || "", referrer: clean_referrer(ref) || "", utm_medium: query["utm_medium"] || "", @@ -101,12 +100,19 @@ defmodule PlausibleWeb.Api.ExternalController do } Enum.reduce_while(get_domains(params, uri), :error, fn domain, _res -> - attrs = Map.put(event_attrs, :domain, domain) - changeset = Plausible.ClickhouseEvent.changeset(%Plausible.ClickhouseEvent{}, attrs) + user_id = generate_user_id(conn, domain, event_attrs[:hostname], salts[:current]) + + previous_user_id = + salts[:previous] && + generate_user_id(conn, domain, event_attrs[:hostname], salts[:previous]) + + changeset = + event_attrs + |> Map.merge(%{domain: domain, user_id: user_id}) + |> Plausible.ClickhouseEvent.new() if changeset.valid? do - previous_user_id = salts[:previous] && generate_user_id(conn, params, salts[:previous]) - event = struct(Plausible.ClickhouseEvent, attrs) + event = struct(Plausible.ClickhouseEvent, event_attrs) session_id = Plausible.Session.Store.on_event(event, previous_user_id) event @@ -186,12 +192,11 @@ defmodule PlausibleWeb.Api.ExternalController do end end - defp generate_user_id(conn, params, salt) do + defp generate_user_id(conn, domain, hostname, salt) do user_agent = List.first(Plug.Conn.get_req_header(conn, "user-agent")) || "" ip_address = PlausibleWeb.RemoteIp.get(conn) - domain = strip_www(params["domain"]) || "" - SipHash.hash!(salt, user_agent <> ip_address <> domain) + SipHash.hash!(salt, user_agent <> ip_address <> domain <> hostname) end defp calculate_screen_size(nil), do: nil