analytics/test/plausible_web/controllers/api/stats_controller/browsers_test.exs
RobertJoonas 40275b64d4
Pageview custom dimensions (#1816)
* added custom dimension filtering tests for pages

* first filter UI in place

* pages, entry pages and exit pages can be filtered by pageview props

* added tests for expected filtering behaviour

* fix dimension filter for sources + tests

* added is_not filtering functionality

* fixed formatting

* fixed admin_test

* added (none) as filter value + is_not filter type in UI

* added prefilling applied filter values and some UI tweaks

* added fetch options

* Make prop suggestions work with `props` filter

* Fix test

* Track login state internally

* Add CHANGELOG entry

Co-authored-by: Uku Taht <uku.taht@gmail.com>
2022-04-21 11:47:15 +03:00

161 lines
5.1 KiB
Elixir

defmodule PlausibleWeb.Api.StatsController.BrowsersTest do
use PlausibleWeb.ConnCase
import Plausible.TestUtils
describe "GET /api/stats/:domain/browsers" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data]
test "returns top browsers by unique visitors", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, browser: "Chrome"),
build(:pageview, browser: "Chrome"),
build(:pageview, browser: "Firefox")
])
conn = get(conn, "/api/stats/#{site.domain}/browsers?period=day")
assert json_response(conn, 200) == [
%{"name" => "Chrome", "visitors" => 2, "percentage" => 67},
%{"name" => "Firefox", "visitors" => 1, "percentage" => 33}
]
end
test "returns top browsers with :is filter on custom pageview props", %{
conn: conn,
site: site
} do
populate_stats(site, [
build(:pageview,
user_id: 123,
browser: "Chrome"
),
build(:pageview,
user_id: 123,
browser: "Chrome",
"meta.key": ["author"],
"meta.value": ["John Doe"]
),
build(:pageview,
browser: "Firefox",
"meta.key": ["author"],
"meta.value": ["other"]
),
build(:pageview,
browser: "Safari"
)
])
filters = Jason.encode!(%{props: %{"author" => "John Doe"}})
conn = get(conn, "/api/stats/#{site.domain}/browsers?period=day&filters=#{filters}")
assert json_response(conn, 200) == [
%{"name" => "Chrome", "visitors" => 1, "percentage" => 100}
]
end
test "returns top browsers with :is_not filter on custom pageview props", %{
conn: conn,
site: site
} do
populate_stats(site, [
build(:pageview,
user_id: 123,
browser: "Chrome",
"meta.key": ["author"],
"meta.value": ["John Doe"]
),
build(:pageview,
user_id: 123,
browser: "Chrome",
"meta.key": ["author"],
"meta.value": ["John Doe"]
),
build(:pageview,
browser: "Firefox",
"meta.key": ["author"],
"meta.value": ["other"]
),
build(:pageview,
browser: "Safari"
)
])
filters = Jason.encode!(%{props: %{"author" => "!John Doe"}})
conn = get(conn, "/api/stats/#{site.domain}/browsers?period=day&filters=#{filters}")
assert json_response(conn, 200) == [
%{"name" => "Firefox", "visitors" => 1, "percentage" => 50},
%{"name" => "Safari", "visitors" => 1, "percentage" => 50}
]
end
test "calculates conversion_rate when filtering for goal", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, user_id: 1, browser: "Chrome"),
build(:pageview, user_id: 2, browser: "Chrome"),
build(:event, user_id: 1, name: "Signup")
])
filters = Jason.encode!(%{"goal" => "Signup"})
conn = get(conn, "/api/stats/#{site.domain}/browsers?period=day&filters=#{filters}")
assert json_response(conn, 200) == [
%{
"name" => "Chrome",
"total_visitors" => 2,
"visitors" => 1,
"conversion_rate" => 50.0
}
]
end
test "returns top browsers including imported data", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, browser: "Chrome"),
build(:imported_browsers, browser: "Chrome"),
build(:imported_browsers, browser: "Firefox")
])
conn = get(conn, "/api/stats/#{site.domain}/browsers?period=day")
assert json_response(conn, 200) == [
%{"name" => "Chrome", "visitors" => 1, "percentage" => 100}
]
conn = get(conn, "/api/stats/#{site.domain}/browsers?period=day&with_imported=true")
assert json_response(conn, 200) == [
%{"name" => "Chrome", "visitors" => 2, "percentage" => 67},
%{"name" => "Firefox", "visitors" => 1, "percentage" => 33}
]
end
end
describe "GET /api/stats/:domain/browser-versions" do
setup [:create_user, :log_in, :create_new_site]
test "returns top browser versions by unique visitors", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, browser: "Chrome", browser_version: "78.0"),
build(:pageview, browser: "Chrome", browser_version: "78.0"),
build(:pageview, browser: "Chrome", browser_version: "77.0"),
build(:pageview, browser: "Firefox", browser_version: "88.0")
])
filters = Jason.encode!(%{browser: "Chrome"})
conn =
get(
conn,
"/api/stats/#{site.domain}/browser-versions?period=day&filters=#{filters}"
)
assert json_response(conn, 200) == [
%{"name" => "78.0", "visitors" => 2, "percentage" => 67},
%{"name" => "77.0", "visitors" => 1, "percentage" => 33}
]
end
end
end