Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Add unique visitors to pages modal

Uku Tahtb254e943bca2
diff --git a/assets/js/dashboard/stats/modals/pages.js b/assets/js/dashboard/stats/modals/pages.js
index 82a295bb2f..e651e3ae22 100644
--- a/assets/js/dashboard/stats/modals/pages.js
+++ b/assets/js/dashboard/stats/modals/pages.js
@@ -16,13 +16,13 @@ class PagesModal extends React.Component {
}
componentDidMount() {
- const include = this.showBounceRate() ? 'bounce_rate' : null
+ const include = this.showExtra() ? 'bounce_rate,unique_visitors' : null
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/pages`, this.state.query, {limit: 100, include: include})
.then((res) => this.setState({loading: false, pages: res}))
}
- showBounceRate() {
+ showExtra() {
return this.state.query.period !== 'realtime' && !this.state.query.filters.goal
}
@@ -39,7 +39,8 @@ class PagesModal extends React.Component {
<tr className="text-sm" key={page.name}>
<td className="p-2 truncate">{page.name}</td>
<td className="p-2 w-32 font-medium" align="right">{numberFormatter(page.count)}</td>
- {this.showBounceRate() && <td className="p-2 w-32 font-medium" align="right">{this.formatBounceRate(page)}</td> }
+ {this.showExtra() && <td className="p-2 w-32 font-medium" align="right">{numberFormatter(page.unique_visitors)}</td> }
+ {this.showExtra() && <td className="p-2 w-32 font-medium" align="right">{this.formatBounceRate(page)}</td> }
</tr>
)
}
@@ -65,7 +66,8 @@ class PagesModal extends React.Component {
<tr>
<th className="p-2 text-xs tracking-wide font-bold text-gray-500" align="left">Page url</th>
<th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">{ this.label() }</th>
- {this.showBounceRate() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">Bounce rate</th>}
+ {this.showExtra() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">Unique visitors</th>}
+ {this.showExtra() && <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">Bounce rate</th>}
</tr>
</thead>
<tbody>
diff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.ex
index 90525dd724..7a8ac4625c 100644
--- a/lib/plausible/stats/clickhouse.ex
+++ b/lib/plausible/stats/clickhouse.ex
@@ -391,14 +391,23 @@ defmodule Plausible.Stats.Clickhouse do
end
def top_pages(site, query, limit, include) do
- pages =
- Clickhouse.all(
- from e in base_query(site, query),
- select: {fragment("? as name", e.pathname), fragment("count(?) as count", e.pathname)},
- group_by: e.pathname,
- order_by: [desc: fragment("count")],
- limit: ^limit
+ q = from(
+ e in base_query(site, query),
+ group_by: e.pathname,
+ order_by: [desc: fragment("count")],
+ limit: ^limit
+ )
+
+ q = if "unique_visitors" in include do
+ from(
+ e in q,
+ select: {fragment("? as name", e.pathname), fragment("count(?) as count", e.pathname), fragment("uniq(?) as unique_visitors", e.user_id)}
)
+ else
+ from(e in q, select: {fragment("? as name", e.pathname), fragment("count(?) as count", e.pathname)})
+ end
+
+ pages = Clickhouse.all(q)
if "bounce_rate" in include do
bounce_rates = bounce_rates_by_page_url(site, query)
diff --git a/test/plausible_web/controllers/api/stats_controller/pages_test.exs b/test/plausible_web/controllers/api/stats_controller/pages_test.exs
index 7fb7759b37..087967775d 100644
--- a/test/plausible_web/controllers/api/stats_controller/pages_test.exs
+++ b/test/plausible_web/controllers/api/stats_controller/pages_test.exs
@@ -16,18 +16,18 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do
]
end
- test "calculates bounce rate for pages", %{conn: conn, site: site} do
+ test "calculates bounce rate and unique visitors for pages", %{conn: conn, site: site} do
conn =
get(
conn,
- "/api/stats/#{site.domain}/pages?period=day&date=2019-01-01&include=bounce_rate"
+ "/api/stats/#{site.domain}/pages?period=day&date=2019-01-01&include=bounce_rate,unique_visitors"
)
assert json_response(conn, 200) == [
- %{"count" => 2, "name" => "/", "bounce_rate" => 33.0},
- %{"bounce_rate" => nil, "count" => 2, "name" => "/register"},
- %{"bounce_rate" => nil, "count" => 1, "name" => "/contact"},
- %{"bounce_rate" => nil, "count" => 1, "name" => "/irrelevant"}
+ %{"bounce_rate" => 33.0, "count" => 2, "unique_visitors" => 2, "name" => "/"},
+ %{"bounce_rate" => nil, "count" => 2, "unique_visitors" => 2, "name" => "/register"},
+ %{"bounce_rate" => nil, "count" => 1, "unique_visitors" => 1, "name" => "/contact"},
+ %{"bounce_rate" => nil, "count" => 1, "unique_visitors" => 1, "name" => "/irrelevant"}
]
end