Bug fix - show views_per_visit when stats include imported data (#2738)

* show views_per_visit with imported data

* use optional chaining instead
This commit is contained in:
RobertJoonas 2023-03-10 10:28:01 +02:00 committed by GitHub
parent 4eca1aef70
commit ad3edbfb9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 1 deletions

View File

@ -44,10 +44,14 @@ export default class TopStats extends React.Component {
let statName = stat.name.toLowerCase()
statName = stat.value === 1 ? statName.slice(0, -1) : statName
const { topStatData, lastLoadTimestamp } = this.props
const showingImported = topStatData?.imported_source && topStatData?.with_imported
return (
<div>
<div className="whitespace-nowrap">{this.topStatNumberLong(stat)} {statName}</div>
{stat.name === 'Current visitors' && <p className="font-normal text-xs">Last updated <SecondsSinceLastLoad lastLoadTimestamp={this.props.lastLoadTimestamp}/>s ago</p>}
{stat.name === 'Current visitors' && <p className="font-normal text-xs">Last updated <SecondsSinceLastLoad lastLoadTimestamp={lastLoadTimestamp}/>s ago</p>}
{stat.name === 'Views per visit' && showingImported && <p className="font-normal text-xs whitespace-nowrap">Based only on native data</p>}
</div>
)
}

View File

@ -452,6 +452,14 @@ defmodule Plausible.Stats.Imported do
|> select_joined_metrics(rest)
end
defp select_joined_metrics(q, [:views_per_visit | rest]) do
q
|> select_merge([s, _i], %{
views_per_visit: s.views_per_visit
})
|> select_joined_metrics(rest)
end
defp select_joined_metrics(q, [:bounce_rate | rest]) do
q
|> select_merge([s, i], %{

View File

@ -125,6 +125,47 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
end
end
describe "GET /api/stats/top-stats - with imported data" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data]
test "merges imported data into all top stat metrics except views_per_visit", %{
conn: conn,
site: site
} do
populate_stats(site, [
build(:pageview,
user_id: @user_id,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:pageview,
user_id: @user_id,
timestamp: ~N[2021-01-01 00:15:00]
),
build(:pageview,
timestamp: ~N[2021-01-01 00:15:00]
),
build(:imported_visitors, date: ~D[2021-01-01])
])
conn =
get(
conn,
"/api/stats/#{site.domain}/top-stats?period=day&date=2021-01-01&with_imported=true"
)
res = json_response(conn, 200)
assert res["top_stats"] == [
%{"name" => "Unique visitors", "value" => 3, "change" => 100},
%{"name" => "Total visits", "value" => 3, "change" => 100},
%{"name" => "Total pageviews", "value" => 4, "change" => 100},
%{"name" => "Views per visit", "value" => 1.5, "change" => 100},
%{"name" => "Bounce rate", "value" => 33, "change" => nil},
%{"name" => "Visit duration", "value" => 303, "change" => 100}
]
end
end
describe "GET /api/stats/top-stats - realtime" do
setup [:create_user, :log_in, :create_new_site]