diff --git a/assets/js/p.js b/assets/js/p.js index 83cbcae300..a634e133fe 100644 --- a/assets/js/p.js +++ b/assets/js/p.js @@ -58,7 +58,7 @@ uid: uid, user_agent: window.navigator.userAgent, referrer: window.document.referrer, - screenWidth: window.innerWidth + screen_width: window.innerWidth })); request.onreadystatechange = function() { diff --git a/assets/js/plausible.js b/assets/js/plausible.js index 94e222bbaa..804ae1ea7f 100644 --- a/assets/js/plausible.js +++ b/assets/js/plausible.js @@ -58,7 +58,7 @@ uid: uid, user_agent: window.navigator.userAgent, referrer: window.document.referrer, - screenWidth: window.innerWidth + screen_width: window.innerWidth })); request.onreadystatechange = function() { diff --git a/lib/plausible_web/controllers/api/external_controller.ex b/lib/plausible_web/controllers/api/external_controller.ex index ce9004762c..11fe92b1ac 100644 --- a/lib/plausible_web/controllers/api/external_controller.ex +++ b/lib/plausible_web/controllers/api/external_controller.ex @@ -63,11 +63,11 @@ defmodule PlausibleWeb.Api.ExternalController do end end + defp calculate_screen_size(nil) , do: nil defp calculate_screen_size(width) when width < 576, do: "Mobile" defp calculate_screen_size(width) when width < 992, do: "Tablet" defp calculate_screen_size(width) when width < 1440, do: "Laptop" defp calculate_screen_size(width) when width >= 1440, do: "Desktop" - defp calculate_screen_size(_) , do: nil defp clean_referrer(referrer) do uri = if referrer do diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exs index 6cb5457eab..28094aba3b 100644 --- a/test/plausible_web/controllers/api/external_controller_test.exs +++ b/test/plausible_web/controllers/api/external_controller_test.exs @@ -285,4 +285,22 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert response(conn, 202) == "" assert pageview.screen_size == "Mobile" end + + test "screen size is nil if screen_width is missing", %{conn: conn} do + params = %{ + url: "http://gigride.live/", + new_visitor: true, + uid: UUID.uuid4() + } + + conn = conn + |> put_req_header("content-type", "text/plain") + |> put_req_header("user-agent", @user_agent) + |> post("/api/page", Jason.encode!(params)) + + pageview = Repo.one(Plausible.Pageview) + + assert response(conn, 202) == "" + assert pageview.screen_size == nil + end end