Show favicons for referrers (#39)

This commit is contained in:
Uku Taht 2020-03-03 14:32:32 +02:00 committed by GitHub
parent e21b59fa4c
commit cc5722050a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 14 deletions

View File

@ -44,6 +44,7 @@ class ReferrersModal extends React.Component {
return (
<tr className="text-sm" key={referrer.name}>
<td className="p-2">
<img src={`https://icons.duckduckgo.com/ip3/${referrer.url}.ico`} className="h-4 w-4 mr-2 align-middle" />
<Link className="hover:underline truncate" style={{maxWidth: '80%'}} to={`/${encodeURIComponent(this.props.site.domain)}/referrers/${referrer.name}${window.location.search}`}>{ referrer.name }</Link>
</td>
<td className="p-2 w-32 font-medium" align="right">{numberFormatter(referrer.count)}</td>

View File

@ -39,7 +39,10 @@ export default class Referrers extends React.Component {
<div className="flex items-center justify-between my-1 text-sm" key={referrer.name}>
<div className="w-full h-8" style={{maxWidth: 'calc(100% - 4rem)'}}>
<Bar count={referrer.count} all={this.state.referrers} color="blue" />
<Link className="hover:underline block px-2" style={{marginTop: '-23px'}} to={`/${encodeURIComponent(this.props.site.domain)}/referrers/${referrer.name}${window.location.search}`}>{ referrer.name }</Link>
<Link className="hover:underline block px-2" style={{marginTop: '-23px'}} to={`/${encodeURIComponent(this.props.site.domain)}/referrers/${referrer.name}${window.location.search}`}>
<img src={`https://icons.duckduckgo.com/ip3/${referrer.url}.ico`} className="h-4 w-4 mr-2 align-middle" />
{ referrer.name }
</Link>
</div>
<span className="font-medium">{numberFormatter(referrer.count)}</span>
</div>

View File

@ -154,22 +154,26 @@ defmodule Plausible.Stats do
def top_referrers_for_goal(site, query, limit \\ 5) do
Repo.all(from e in base_query(site, query),
select: %{name: e.initial_referrer_source, count: count(e.user_id, :distinct)},
select: %{name: e.initial_referrer_source, url: min(e.initial_referrer), count: count(e.user_id, :distinct)},
group_by: e.initial_referrer_source,
where: not is_nil(e.initial_referrer_source),
order_by: [desc: 2],
order_by: [desc: 3],
limit: ^limit
)
) |> Enum.map(fn ref ->
Map.update(ref, :url, nil, fn url -> url && URI.parse("http://" <> url).host end)
end)
end
def top_referrers(site, query, limit \\ 5, include \\ []) do
referrers = Repo.all(from e in base_query(site, query),
select: %{name: e.referrer_source, count: count(e.user_id, :distinct)},
select: %{name: e.referrer_source, url: min(e.referrer), count: count(e.user_id, :distinct)},
group_by: e.referrer_source,
where: not is_nil(e.referrer_source),
order_by: [desc: 2],
order_by: [desc: 3],
limit: ^limit
)
) |> Enum.map(fn ref ->
Map.update(ref, :url, nil, fn url -> url && URI.parse("http://" <> url).host end)
end)
if "bounce_rate" in include do
bounce_rates = bounce_rates_by_referrer_source(site, query, Enum.map(referrers, fn ref -> ref[:name] end))

View File

@ -77,7 +77,7 @@ defmodule PlausibleWeb.Api.StatsController do
site = conn.assigns[:site]
query = Stats.Query.from(site.timezone, params)
json(conn, Stats.top_referrers_for_goal(site, query, params["limit"] || 5))
json(conn, Stats.top_referrers_for_goal(site, query, params["limit"] || 9))
end
@google_api Application.fetch_env!(:plausible, :google_api)

View File

@ -14,8 +14,8 @@ defmodule PlausibleWeb.Api.StatsController.ReferrersTest do
conn = get(conn, "/api/stats/#{site.domain}/referrers?period=day&date=2019-01-01")
assert json_response(conn, 200) == [
%{"name" => "Google", "count" => 2},
%{"name" => "Bing", "count" => 1},
%{"name" => "Google", "count" => 2, "url" => nil},
%{"name" => "Bing", "count" => 1, "url" => nil},
]
end
@ -30,8 +30,8 @@ defmodule PlausibleWeb.Api.StatsController.ReferrersTest do
conn = get(conn, "/api/stats/#{site.domain}/referrers?period=day&date=2019-01-01&include=bounce_rate")
assert json_response(conn, 200) == [
%{"name" => "Google", "count" => 2, "bounce_rate" => 50},
%{"name" => "Bing", "count" => 1, "bounce_rate" => nil},
%{"name" => "Google", "count" => 2, "bounce_rate" => 50, "url" => nil},
%{"name" => "Bing", "count" => 1, "bounce_rate" => nil, "url" => nil},
]
end
end
@ -48,7 +48,7 @@ defmodule PlausibleWeb.Api.StatsController.ReferrersTest do
conn = get(conn, "/api/stats/#{site.domain}/goal/referrers?period=day&date=2019-01-01&filters=#{filters}")
assert json_response(conn, 200) == [
%{"name" => "Google", "count" => 2},
%{"name" => "Google", "count" => 2, "url" => nil},
]
end
@ -61,7 +61,7 @@ defmodule PlausibleWeb.Api.StatsController.ReferrersTest do
conn = get(conn, "/api/stats/#{site.domain}/goal/referrers?period=day&date=2019-01-01&filters=#{filters}")
assert json_response(conn, 200) == [
%{"name" => "Google", "count" => 2},
%{"name" => "Google", "count" => 2, "url" => nil},
]
end
end