File size: 6,810 Bytes
6778ee0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | defmodule PlausibleWeb.Api.StatsController.CitiesTest do
use PlausibleWeb.ConnCase
defp query_cities(conn, site, opts) do
always_on_filters = ["is_not", "visit:city", [0]]
params = %{
"dimensions" => Keyword.get(opts, :dimensions, ["visit:city", "visit:city_name"]),
"date_range" => Keyword.get(opts, :date_range, "all"),
"filters" => [
always_on_filters
| Keyword.get(opts, :filters, [])
],
"metrics" => Keyword.get(opts, :metrics, ["visitors", "percentage"]),
"include" => Keyword.get(opts, :include, nil),
"pagination" => Keyword.get(opts, :pagination, nil),
"order_by" => Keyword.get(opts, :order_by, nil)
}
conn
|> post("/api/stats/#{site.domain}/query", params)
|> json_response(200)
end
describe "querying for cities with POST /query" do
defp seed(%{site: site}) do
populate_stats(site, [
build(:pageview,
country_code: "EE",
subdivision1_code: "EE-37",
city_geoname_id: 588_409
),
build(:pageview,
country_code: "EE",
subdivision1_code: "EE-37",
city_geoname_id: 588_409
),
build(:pageview,
country_code: "EE",
subdivision1_code: "EE-37",
city_geoname_id: 588_409
),
build(:pageview,
country_code: "EE",
subdivision1_code: "EE-39",
city_geoname_id: 591_632
),
build(:pageview,
country_code: "EE",
subdivision1_code: "EE-39",
city_geoname_id: 591_632
)
])
end
setup [:create_user, :log_in, :create_site, :create_legacy_site_import, :seed]
test "returns top cities by visitors", %{conn: conn, site: site} do
response = query_cities(conn, site, date_range: "day")
assert response["results"] == [
%{"dimensions" => [588_409, "Tallinn"], "metrics" => [3, 60.0]},
%{"dimensions" => [591_632, "Kärdla"], "metrics" => [2, 40.0]}
]
end
test "when list is filtered returns one city only", %{conn: conn, site: site} do
response =
query_cities(conn, site,
date_range: "day",
filters: [["is", "visit:city", [591_632]]]
)
assert response["results"] == [
%{"dimensions" => [591_632, "Kärdla"], "metrics" => [2, 100.0]}
]
end
test "does not return missing cities from imported data", %{conn: conn, site: site} do
populate_stats(site, [
build(:imported_locations, country: "EE", region: "EE-37", city: 588_409),
build(:imported_locations, country: nil, region: nil, city: 0),
build(:imported_locations, country: nil, region: nil, city: nil)
])
response =
query_cities(conn, site,
date_range: "day",
include: %{"imports" => true}
)
assert response["results"] == [
%{"dimensions" => [588_409, "Tallinn"], "metrics" => [4, 66.67]},
%{"dimensions" => [591_632, "Kärdla"], "metrics" => [2, 33.33]}
]
end
@tag :ee_only
test "return revenue metrics for cities breakdown", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview,
user_id: 1,
country_code: "EE",
subdivision1_code: "EE-37",
city_geoname_id: 588_409
),
build(:event,
name: "Payment",
user_id: 1,
revenue_reporting_amount: Decimal.new("1000"),
revenue_reporting_currency: "USD"
),
build(:pageview,
user_id: 2,
country_code: "EE",
subdivision1_code: "EE-37",
city_geoname_id: 588_409
),
build(:event,
name: "Payment",
user_id: 2,
revenue_reporting_amount: Decimal.new("2000"),
revenue_reporting_currency: "USD"
),
build(:pageview,
user_id: 3,
country_code: "EE",
subdivision1_code: "EE-37",
city_geoname_id: 588_409
),
build(:pageview,
user_id: 4,
country_code: "EE",
subdivision1_code: "EE-39",
city_geoname_id: 591_632
),
build(:event,
name: "Payment",
user_id: 4,
revenue_reporting_amount: Decimal.new("500"),
revenue_reporting_currency: "USD"
),
build(:pageview,
user_id: 5,
country_code: "EE",
subdivision1_code: "EE-39",
city_geoname_id: 591_632
),
build(:pageview, user_id: 6),
build(:event,
name: "Payment",
user_id: 6,
revenue_reporting_amount: Decimal.new("600"),
revenue_reporting_currency: "USD"
),
build(:pageview, user_id: 7),
build(:event,
name: "Payment",
user_id: 7,
revenue_reporting_amount: nil
)
])
insert(:goal, %{site: site, event_name: "Payment", currency: :USD})
response =
query_cities(conn, site,
date_range: "day",
filters: [["is", "event:goal", ["Payment"]]],
metrics: [
"visitors",
"total_visitors",
"group_conversion_rate",
"average_revenue",
"total_revenue"
],
order_by: [["visitors", "desc"], ["visit:city", "asc"]]
)
assert response["results"] == [
%{
"dimensions" => [588_409, "Tallinn"],
"metrics" => [
2,
6,
33.33,
%{
"currency" => "USD",
"long" => "$1,500.00",
"short" => "$1.5K",
"value" => 1500.0
},
%{
"currency" => "USD",
"long" => "$3,000.00",
"short" => "$3.0K",
"value" => 3000.0
}
]
},
%{
"dimensions" => [591_632, "Kärdla"],
"metrics" => [
1,
4,
25.0,
%{
"currency" => "USD",
"long" => "$500.00",
"short" => "$500.0",
"value" => 500.0
},
%{
"currency" => "USD",
"long" => "$500.00",
"short" => "$500.0",
"value" => 500.0
}
]
}
]
end
end
end
|