analytics/test/plausible_web/controllers/api/stats_controller/browsers_test.exs
Matt Colligan f576fa2a2c
Improvements to CSV export (#1427)
* Add details=True to export API parameters

This makes the ZIP export add `%{"details" => "True"}` to the query's
`params` when fetching data internally for packaging in the ZIP.

This adds bounce_rate and time_on_page to the data in pages.csv, and
bounce_rate and visit_duration to sources.csv.

* Make API return data with consistent names

Some of the data types returned via the JSON or CSV API use inconsistent
naming, and some have redundant name changes (i.e. count -> visitors ->
count). This makes these all consistent and removes the redundancy.

This addresses #1426, fixes some of the CSV headers, and unifies the
JSON and CSV return data labels.

* Update changelog

* Test should use Timex.shift, not relative time

* Return full country names in CSV export

This also replaces the " character with ' in two country names, as those
are the characters used in the names, yielding a more predictable and
'correct' output.

* Fetch CSV exported data concurrently

* Use spinner to indicate when export has started

* Use 300 as default number of brekadown entries for export

Higher numbers (e.g. 1000) seem to cause clickhouse errors when there
many pages to request. It is unclear what is causing the error, as
clickhouse returns an "unknown" error code and an empty error message.
2021-11-04 14:20:39 +02:00

72 lines
2.4 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]
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 "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,
"percentage" => 100,
"conversion_rate" => 50.0
}
]
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