Skip to content

Commit

Permalink
Add contract_id field
Browse files Browse the repository at this point in the history
  • Loading branch information
rdk08 committed Nov 8, 2023
1 parent cf3db2e commit 85ddff2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 23 deletions.
17 changes: 15 additions & 2 deletions lib/plausible/event/clickhouse_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Plausible.ClickhouseEvent do
field :utm_content, :string, default: ""
field :utm_term, :string, default: ""
field :campaign_id, :string, default: ""
field :contract_id, :string, default: ""
field :product_id, :string, default: ""

field :country_code, :string, default: ""
Expand Down Expand Up @@ -67,6 +68,7 @@ defmodule Plausible.ClickhouseEvent do
:page_id,
:job_id,
:campaign_id,
:contract_id,
:product_id,
:timestamp,
:operating_system,
Expand All @@ -93,10 +95,11 @@ defmodule Plausible.ClickhouseEvent do
)
|> validate_required([:name, :domain, :hostname, :pathname, :event_id, :user_id, :timestamp])
|> validate_campaign_id()
|> validate_contract_id()
|> validate_product_id()
end

defp validate_campaign_id(changeset) do
def validate_campaign_id(changeset) do
campaign_id = get_field(changeset, :campaign_id)

if ValueHelpers.validate(campaign_id, type: :prefixed_id) do
Expand All @@ -106,7 +109,17 @@ defmodule Plausible.ClickhouseEvent do
end
end

defp validate_product_id(changeset) do
def validate_contract_id(changeset) do
contract_id = get_field(changeset, :contract_id)

if ValueHelpers.validate(contract_id, type: :prefixed_id) do
changeset
else
delete_change(changeset, :contract_id)
end
end

def validate_product_id(changeset) do
product_id = get_field(changeset, :product_id)

if ValueHelpers.validate(product_id, type: :prefixed_id) do
Expand Down
26 changes: 5 additions & 21 deletions lib/plausible/session/clickhouse_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ defmodule Plausible.ClickhouseSession do

import Ecto.Changeset

alias Plausible.ValueHelpers

@primary_key false
schema "sessions" do
field :hostname, :string
Expand All @@ -31,6 +29,7 @@ defmodule Plausible.ClickhouseSession do
field :referrer, :string
field :referrer_source, :string
field :campaign_id, :string
field :contract_id, :string
field :product_id, :string

field :country_code, :string, default: ""
Expand Down Expand Up @@ -84,26 +83,11 @@ defmodule Plausible.ClickhouseSession do
])
|> validate_required([:hostname, :domain, :fingerprint, :is_bounce, :start])
|> validate_campaign_id()
|> validate_contract_id()
|> validate_product_id()
end

defp validate_campaign_id(changeset) do
campaign_id = get_field(changeset, :campaign_id)

if ValueHelpers.validate(campaign_id, type: :prefixed_id) do
changeset
else
delete_change(changeset, :campaign_id)
end
end

defp validate_product_id(changeset) do
product_id = get_field(changeset, :product_id)

if ValueHelpers.validate(product_id, type: :prefixed_id) do
changeset
else
delete_change(changeset, :product_id)
end
end
defdelegate validate_campaign_id(changeset), to: Plausible.ClickhouseEvent
defdelegate validate_contract_id(changeset), to: Plausible.ClickhouseEvent
defdelegate validate_product_id(changeset), to: Plausible.ClickhouseEvent
end
1 change: 1 addition & 0 deletions lib/plausible/session/store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ defmodule Plausible.Session.Store do
site_id: event.site_id,
session_id: Plausible.ClickhouseSession.random_uint64(),
campaign_id: event.campaign_id,
contract_id: event.contract_id,
product_id: event.product_id,
hostname: event.hostname,
domain: event.domain,
Expand Down
2 changes: 2 additions & 0 deletions lib/plausible_web/controllers/api/external_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ defmodule PlausibleWeb.Api.ExternalController do
page_id: params["page_id"],
site_id: params["site_id"],
campaign_id: params["campaign_id"],
contract_id: params["contract_id"],
product_id: params["product_id"],
country_code: location_details[:country_code],
country_geoname_id: location_details[:country_geoname_id],
Expand Down Expand Up @@ -196,6 +197,7 @@ defmodule PlausibleWeb.Api.ExternalController do
"campaign_id",
"careers_application_form_uuid",
"company_id",
"contract_id",
"job_id",
"page_id",
"product_id",
Expand Down
13 changes: 13 additions & 0 deletions priv/clickhouse_repo/migrations/20231108173105_add_contract_id.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Plausible.ClickhouseRepo.Migrations.AddContractId do
use Ecto.Migration

def change do
alter table(:events) do
add(:contract_id, :string)
end

alter table(:sessions) do
add(:contract_id, :string)
end
end
end
2 changes: 2 additions & 0 deletions test/plausible/session/store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule Plausible.Session.StoreTest do
operating_system: "Mac",
operating_system_version: "11",
campaign_id: 1,
contract_id: 1,
product_id: 1
)

Expand Down Expand Up @@ -58,6 +59,7 @@ defmodule Plausible.Session.StoreTest do
assert session.operating_system == event.operating_system
assert session.operating_system_version == event.operating_system_version
assert session.campaign_id == event.campaign_id
assert session.contract_id == event.contract_id
assert session.product_id == event.product_id
assert session.browser == event.browser
assert session.browser_version == event.browser_version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
domain: "special-props-test.com",
props: %{
campaign_id: "vendor-8",
contract_id: "vendor-10",
company_id: 10,
job_id: 12,
page_id: 15,
Expand All @@ -514,6 +515,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
event = get_event("special-props-test.com")

assert Map.get(event, :campaign_id) == "vendor-8"
assert Map.get(event, :contract_id) == "vendor-10"
assert Map.get(event, :product_id) == "vendor-6a01cfb6-717a-11ee-b962-0242ac120002"
assert Map.get(event, :company_id) == 10
assert Map.get(event, :job_id) == 12
Expand All @@ -534,6 +536,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
domain: "special-props-validation-test.com",
props: %{
campaign_id: "12345",
contract_id: "vendor",
company_id: 10,
page_id: 15,
product_id: "incorrect-product-id",
Expand All @@ -547,6 +550,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
event = get_event("special-props-validation-test.com")

assert Map.get(event, :campaign_id) == ""
assert Map.get(event, :contract_id) == ""
assert Map.get(event, :product_id) == ""
assert Map.get(event, :company_id) == 10
assert Map.get(event, :page_id) == 15
Expand Down

0 comments on commit 85ddff2

Please sign in to comment.