2019-11-19 07:30:42 +03:00
|
|
|
defmodule PlausibleWeb.Api.StatsController.CountriesTest do
|
|
|
|
use PlausibleWeb.ConnCase
|
|
|
|
import Plausible.TestUtils
|
|
|
|
|
|
|
|
describe "GET /api/stats/:domain/countries" do
|
2021-07-23 13:44:05 +03:00
|
|
|
setup [:create_user, :log_in, :create_new_site]
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
test "returns top countries by new visitors", %{conn: conn, site: site} do
|
2021-07-23 13:44:05 +03:00
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
country_code: "EE"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
country_code: "EE"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
country_code: "GB"
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/countries?period=day")
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
2020-06-08 10:35:13 +03:00
|
|
|
%{
|
|
|
|
"name" => "EST",
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 2,
|
2021-07-23 13:44:05 +03:00
|
|
|
"percentage" => 67
|
2020-06-08 10:35:13 +03:00
|
|
|
},
|
|
|
|
%{
|
|
|
|
"name" => "GBR",
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1,
|
2021-07-23 13:44:05 +03:00
|
|
|
"percentage" => 33
|
2020-06-08 10:35:13 +03:00
|
|
|
}
|
|
|
|
]
|
2019-11-19 07:30:42 +03:00
|
|
|
end
|
2021-09-20 17:17:11 +03:00
|
|
|
|
|
|
|
test "calculates conversion_rate when filtering for goal", %{conn: conn, site: site} do
|
|
|
|
populate_stats(site, [
|
|
|
|
build(:pageview,
|
|
|
|
user_id: 1,
|
|
|
|
country_code: "EE"
|
|
|
|
),
|
|
|
|
build(:event, user_id: 1, name: "Signup"),
|
|
|
|
build(:pageview,
|
|
|
|
user_id: 2,
|
|
|
|
country_code: "EE"
|
|
|
|
),
|
|
|
|
build(:pageview,
|
|
|
|
user_id: 3,
|
|
|
|
country_code: "GB"
|
|
|
|
),
|
|
|
|
build(:event, user_id: 3, name: "Signup")
|
|
|
|
])
|
|
|
|
|
|
|
|
filters = Jason.encode!(%{"goal" => "Signup"})
|
|
|
|
|
|
|
|
conn = get(conn, "/api/stats/#{site.domain}/countries?period=day&filters=#{filters}")
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == [
|
|
|
|
%{
|
|
|
|
"name" => "GBR",
|
2021-09-29 14:28:29 +03:00
|
|
|
"total_visitors" => 1,
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1,
|
2021-09-20 17:17:11 +03:00
|
|
|
"conversion_rate" => 100.0
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"name" => "EST",
|
2021-09-29 14:28:29 +03:00
|
|
|
"total_visitors" => 2,
|
2021-11-04 15:20:39 +03:00
|
|
|
"visitors" => 1,
|
2021-09-20 17:17:11 +03:00
|
|
|
"conversion_rate" => 50.0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
2019-11-19 07:30:42 +03:00
|
|
|
end
|
|
|
|
end
|