diff --git a/assets/js/dashboard/stats/graph/top-stats.js b/assets/js/dashboard/stats/graph/top-stats.js
index f04513d85..ce1571844 100644
--- a/assets/js/dashboard/stats/graph/top-stats.js
+++ b/assets/js/dashboard/stats/graph/top-stats.js
@@ -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 (
{this.topStatNumberLong(stat)} {statName}
- {stat.name === 'Current visitors' &&
Last updated s ago
}
+ {stat.name === 'Current visitors' &&
Last updated s ago
}
+ {stat.name === 'Views per visit' && showingImported &&
Based only on native data
}
)
}
diff --git a/lib/plausible/stats/imported.ex b/lib/plausible/stats/imported.ex
index 789a99c38..32458037d 100644
--- a/lib/plausible/stats/imported.ex
+++ b/lib/plausible/stats/imported.ex
@@ -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], %{
diff --git a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs
index b41da6896..ff3e7c8a6 100644
--- a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs
+++ b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs
@@ -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]