Use new iso_codes package

pull/1505/head
Uku Taht 5 years ago
parent b5c662f5e5
commit effe56b3e4

@ -49,10 +49,10 @@ export default function Router({site, loggedIn, currentUserRole}) {
<ExitPagesModal site={site} />
</Route>
<Route path="/:domain/countries">
<ModalTable title="Top countries" site={site} endpoint={`/api/stats/${encodeURIComponent(site.domain)}/countries`} filter={{country: 'code', country_name: 'name'}} keyLabel="Country" />
<ModalTable title="Top countries" site={site} endpoint={`/api/stats/${encodeURIComponent(site.domain)}/countries`} filter={{country: 'code', country_name: 'name'}} keyLabel="Country" renderIcon={(row) => site.cities && renderCountryIcon(row)} />
</Route>
<Route path="/:domain/regions">
<ModalTable title="Top regions" site={site} endpoint={`/api/stats/${encodeURIComponent(site.domain)}/regions`} filter={{region: 'code', region_name: 'name'}} keyLabel="Region" />
<ModalTable title="Top regions" site={site} endpoint={`/api/stats/${encodeURIComponent(site.domain)}/regions`} filter={{region: 'code', region_name: 'name'}} keyLabel="Region" renderIcon={renderRegionIcon} />
</Route>
<Route path="/:domain/cities">
<ModalTable title="Top cities" site={site} endpoint={`/api/stats/${encodeURIComponent(site.domain)}/cities`} filter={{city: 'code', city_name: 'name'}} keyLabel="City" />
@ -65,3 +65,11 @@ export default function Router({site, loggedIn, currentUserRole}) {
</BrowserRouter>
);
}
function renderCountryIcon(country) {
return <span className="mr-1">{country.flag}</span>
}
function renderRegionIcon(region) {
return <span className="mr-1">{region.country_flag}</span>
}

@ -14,6 +14,10 @@ function Countries({query, site}) {
})
}
function renderIcon(country) {
return site.cities && <span className="mr-1">{country.flag}</span>
}
return (
<ListReport
fetchData={fetchData}
@ -21,6 +25,7 @@ function Countries({query, site}) {
keyLabel="Country"
detailsLink={sitePath(site, '/countries')}
query={query}
renderIcon={renderIcon}
color="bg-orange-50"
/>
)
@ -31,6 +36,10 @@ function Regions({query, site}) {
return api.get(apiPath(site, '/regions'), query, {country_name: query.filters.country, limit: 9})
}
function renderIcon(region) {
return <span className="mr-1">{region.country_flag}</span>
}
return (
<ListReport
fetchData={fetchData}
@ -38,6 +47,7 @@ function Regions({query, site}) {
keyLabel="Region"
detailsLink={sitePath(site, '/regions')}
query={query}
renderIcon={renderIcon}
color="bg-orange-50"
/>
)

@ -1,6 +1,5 @@
import React from "react";
import { Link, withRouter } from 'react-router-dom'
import Datamap from 'datamaps'
import Modal from './modal'
import * as api from '../../api'
@ -32,20 +31,18 @@ class ModalTable extends React.Component {
query.set(key, tableItem[valueKey])
}))
const allCountries = Datamap.prototype.worldTopo.objects.world.geometries;
const thisCountry = allCountries.find((c) => c.id === tableItem.name) || {properties: {name: tableItem.name}};
const countryFullName = thisCountry.properties.name
return (
<tr className="text-sm dark:text-gray-200" key={tableItem.name}>
<td className="p-2">
<Link className="hover:underline" to={{search: query.toString(), pathname: `/${encodeURIComponent(this.props.site.domain)}`}}>
{countryFullName}
{this.props.renderIcon && this.props.renderIcon(tableItem)}
{this.props.renderIcon && ' '}
{tableItem.name}
</Link>
</td>
<td className="p-2 w-32 font-medium" align="right">
{numberFormatter(tableItem.visitors)}
{tableItem.percentage &&
{tableItem.percentage >= 0 &&
<span className="inline-block text-xs w-8 text-right">({tableItem.percentage}%)</span> }
</td>
</tr>

@ -494,8 +494,8 @@ defmodule PlausibleWeb.Api.StatsController do
countries =
countries
|> Enum.map(fn country ->
iso3166 = Stats.CountryName.from_iso3166(country["code"])
Map.put(country, "name", iso3166)
country_iso_info = ISOCodes.ISO3166_1.get(country["code"])
Map.put(country, "name", country_iso_info.name)
end)
if Map.has_key?(query.filters, "event:goal") do
@ -507,10 +507,14 @@ defmodule PlausibleWeb.Api.StatsController do
end
else
countries =
Enum.map(countries, fn country ->
name = Stats.CountryName.from_iso3166(country["code"])
code = Stats.CountryName.to_alpha3(country["code"])
Map.merge(country, %{"name" => name, "code" => code})
Enum.map(countries, fn row ->
country = ISOCodes.ISO3166_1.get(row["code"])
Map.merge(row, %{
"name" => country.name,
"flag" => country.flag,
"code" => country.alpha_3
})
end)
json(conn, countries)
@ -526,8 +530,11 @@ defmodule PlausibleWeb.Api.StatsController do
Stats.breakdown(site, query, "visit:region", ["visitors"], pagination)
|> transform_keys(%{"region" => "code"})
|> Enum.map(fn region ->
name = Stats.CountryName.from_iso3166_2(region["code"])
Map.put(region, "name", name)
country_code = region["code"] |> String.split("-") |> List.first()
region_entry = ISOCodes.ISO3166_2.get(region["code"])
country_entry = ISOCodes.ISO3166_1.get(country_code)
Map.merge(region, %{"name" => region_entry.name, "country_flag" => country_entry.flag})
end)
json(conn, countries)

@ -85,6 +85,7 @@ defmodule Plausible.MixProject do
{:sshex, "2.2.1"},
{:geolix, "~> 1.0"},
{:clickhouse_ecto, git: "https://github.com/plausible/clickhouse_ecto.git"},
{:iso_codes, git: "https://github.com/plausible/iso_codes"},
{:geolix_adapter_mmdb2, "~> 0.5.0"},
{:cachex, "~> 3.4"},
{:dialyxir, "~> 1.0", only: [:dev, :test], runtime: false},

@ -49,6 +49,7 @@
"httpoison": {:hex, :httpoison, "1.8.0", "6b85dea15820b7804ef607ff78406ab449dd78bed923a49c7160e1886e987a3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "28089eaa98cf90c66265b6b5ad87c59a3729bea2e74e9d08f9b51eb9729b3c3a"},
"hut": {:hex, :hut, "1.3.0", "71f2f054e657c03f959cf1acc43f436ea87580696528ca2a55c8afb1b06c85e7", [:"erlang.mk", :rebar, :rebar3], [], "hexpm", "7e15d28555d8a1f2b5a3a931ec120af0753e4853a4c66053db354f35bf9ab563"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"iso_codes": {:git, "https://github.com/plausible/iso_codes", "413312b01cbdbd9a8283deb94f91bca12830eba4", []},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"jumper": {:hex, :jumper, "1.0.1", "3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff", [:mix], [], "hexpm", "318c59078ac220e966d27af3646026db9b5a5e6703cb2aa3e26bcfaba65b7433"},
"kaffy": {:hex, :kaffy, "0.9.0", "bef34c9729f6a3af4d0dea8eede8bcb9e11371a83ac9a8b393991bce81839517", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.11", [hex: :phoenix_html, repo: "hexpm", optional: false]}], "hexpm", "d18ff57b8e68feb433aed11e71510cd357abc7034e75358af5deff7d0d4c6ed3"},

@ -24,12 +24,14 @@ defmodule PlausibleWeb.Api.StatsController.CountriesTest do
%{
"code" => "EST",
"name" => "Estonia",
"flag" => "🇪🇪",
"visitors" => 2,
"percentage" => 67
},
%{
"code" => "GBR",
"name" => "United Kingdom",
"flag" => "🇬🇧",
"visitors" => 1,
"percentage" => 33
}
@ -62,6 +64,7 @@ defmodule PlausibleWeb.Api.StatsController.CountriesTest do
%{
"code" => "GBR",
"name" => "United Kingdom",
"flag" => "🇬🇧",
"total_visitors" => 1,
"visitors" => 1,
"conversion_rate" => 100.0
@ -69,6 +72,7 @@ defmodule PlausibleWeb.Api.StatsController.CountriesTest do
%{
"code" => "EST",
"name" => "Estonia",
"flag" => "🇪🇪",
"total_visitors" => 2,
"visitors" => 1,
"conversion_rate" => 50.0

Loading…
Cancel
Save