mirror of
https://github.com/plausible/analytics.git
synced 2024-12-25 02:24:55 +03:00
Do not error on empty string custom prop value
This commit is contained in:
parent
0de1dbcf9d
commit
c92c548ca8
@ -200,23 +200,21 @@ defmodule PlausibleWeb.Api.ExternalController do
|
|||||||
defp parse_meta(params) do
|
defp parse_meta(params) do
|
||||||
raw_meta = params["m"] || params["meta"] || params["p"] || params["props"]
|
raw_meta = params["m"] || params["meta"] || params["p"] || params["props"]
|
||||||
|
|
||||||
with {:ok, parsed_json} <- decode_raw_props(raw_meta),
|
case decode_raw_props(raw_meta) do
|
||||||
:ok <- validate_custom_props(parsed_json) do
|
{:ok, parsed_json} ->
|
||||||
parsed_json
|
Enum.filter(parsed_json, fn
|
||||||
else
|
{_, ""} -> false
|
||||||
_ -> %{}
|
{_, val} when is_list(val) -> false
|
||||||
|
{_, val} when is_map(val) -> false
|
||||||
|
_ -> true
|
||||||
|
end)
|
||||||
|
|> Map.new()
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
%{}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp validate_custom_props(props) do
|
|
||||||
is_valid =
|
|
||||||
Enum.all?(props, fn {_key, val} ->
|
|
||||||
!is_list(val) && !is_map(val)
|
|
||||||
end)
|
|
||||||
|
|
||||||
if is_valid, do: :ok, else: :invalid_props
|
|
||||||
end
|
|
||||||
|
|
||||||
defp decode_raw_props(props) when is_map(props), do: {:ok, props}
|
defp decode_raw_props(props) when is_map(props), do: {:ok, props}
|
||||||
|
|
||||||
defp decode_raw_props(raw_json) when is_binary(raw_json) do
|
defp decode_raw_props(raw_json) when is_binary(raw_json) do
|
||||||
|
@ -509,7 +509,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
|
|||||||
name: "Signup",
|
name: "Signup",
|
||||||
url: "http://gigride.live/",
|
url: "http://gigride.live/",
|
||||||
domain: "custom-prop-test-4.com",
|
domain: "custom-prop-test-4.com",
|
||||||
props: Jason.encode!(%{wat: ["some-thing"]})
|
props: Jason.encode!(%{wat: ["some-thing"], other: "key"})
|
||||||
}
|
}
|
||||||
|
|
||||||
conn = post(conn, "/api/event", params)
|
conn = post(conn, "/api/event", params)
|
||||||
@ -518,8 +518,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
|
|||||||
|
|
||||||
event = get_event("custom-prop-test-4.com")
|
event = get_event("custom-prop-test-4.com")
|
||||||
|
|
||||||
assert Map.get(event, :"meta.key") == []
|
assert Map.get(event, :"meta.key") == ["other"]
|
||||||
assert Map.get(event, :"meta.value") == []
|
assert Map.get(event, :"meta.value") == ["key"]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "ignores custom prop with map value", %{conn: conn} do
|
test "ignores custom prop with map value", %{conn: conn} do
|
||||||
@ -527,7 +527,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
|
|||||||
name: "Signup",
|
name: "Signup",
|
||||||
url: "http://gigride.live/",
|
url: "http://gigride.live/",
|
||||||
domain: "custom-prop-test-5.com",
|
domain: "custom-prop-test-5.com",
|
||||||
props: Jason.encode!(%{foo: %{bar: "baz"}})
|
props: Jason.encode!(%{foo: %{bar: "baz"}, other_key: 1})
|
||||||
}
|
}
|
||||||
|
|
||||||
conn = post(conn, "/api/event", params)
|
conn = post(conn, "/api/event", params)
|
||||||
@ -536,8 +536,26 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
|
|||||||
|
|
||||||
event = get_event("custom-prop-test-5.com")
|
event = get_event("custom-prop-test-5.com")
|
||||||
|
|
||||||
assert Map.get(event, :"meta.key") == []
|
assert Map.get(event, :"meta.key") == ["other_key"]
|
||||||
assert Map.get(event, :"meta.value") == []
|
assert Map.get(event, :"meta.value") == ["1"]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "ignores custom prop with empty string value", %{conn: conn} do
|
||||||
|
params = %{
|
||||||
|
name: "Signup",
|
||||||
|
url: "http://gigride.live/",
|
||||||
|
domain: "custom-prop-test-empty-string-val.com",
|
||||||
|
props: Jason.encode!(%{foo: "", other_key: true})
|
||||||
|
}
|
||||||
|
|
||||||
|
conn = post(conn, "/api/event", params)
|
||||||
|
|
||||||
|
assert conn.status == 202
|
||||||
|
|
||||||
|
event = get_event("custom-prop-test-empty-string-val.com")
|
||||||
|
|
||||||
|
assert Map.get(event, :"meta.key") == ["other_key"]
|
||||||
|
assert Map.get(event, :"meta.value") == ["true"]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "ignores a malformed referrer URL", %{conn: conn} do
|
test "ignores a malformed referrer URL", %{conn: conn} do
|
||||||
|
Loading…
Reference in New Issue
Block a user