Adds "Load More" functionality to pages modal (#480)

* Initial Feature Commit

* Changelog
pull/452/head^2
Vignesh Joglekar 6 years ago committed by GitHub
parent e98558fc2d
commit d3f7c94ebe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
- Collect and display browser & OS versions plausible/analytics#397
- Simple notifications around traffic spikes plausible/analytics#453
- Dark theme option/system setting follow plausible/analytics#467
- "Load More" capability to pages modal plausible/analytics#480
### Changed
- Use alpine as base image to decrease Docker image size plausible/analytics#353

@ -12,23 +12,35 @@ class PagesModal extends React.Component {
super(props)
this.state = {
loading: true,
query: parseQuery(props.location.search, props.site)
query: parseQuery(props.location.search, props.site),
pages: [],
page: 1,
moreResultsAvailable: false
}
}
componentDidMount() {
this.loadPages();
}
loadPages() {
const include = this.showBounceRate() ? 'bounce_rate' : null
const {query, page, pages} = this.state;
const {filters} = this.state.query
const {filters} = query
if (filters.source || filters.referrer) {
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/entry-pages`, this.state.query, {limit: 100, include: include})
.then((res) => this.setState({loading: false, pages: res}))
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/entry-pages`, query, {limit: 100, page, include})
.then((res) => this.setState((state) => ({loading: false, pages: state.pages.concat(res), moreResultsAvailable: res.length === 100})))
} else {
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}))
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/pages`, query, {limit: 100, page, include})
.then((res) => this.setState((state) => ({loading: false, pages: state.pages.concat(res), moreResultsAvailable: res.length === 100})))
}
}
loadMore() {
this.setState({loading: true, page: this.state.page + 1}, this.loadPages.bind(this))
}
showBounceRate() {
return this.state.query.period !== 'realtime' && !this.state.query.filters.goal
}
@ -71,12 +83,22 @@ class PagesModal extends React.Component {
return (filters.source || filters.referrer) ? 'Entry Pages' : 'Top Pages'
}
renderBody() {
renderLoading() {
if (this.state.loading) {
return <div className="loading my-16 mx-auto"><div></div></div>
} else if (this.state.moreResultsAvailable) {
return (
<div className="loading mt-32 mx-auto"><div></div></div>
<div className="w-full text-center my-4">
<button onClick={this.loadMore.bind(this)} type="button" className="button">
Load more
</button>
</div>
)
} else if (this.state.pages) {
}
}
renderBody() {
if (this.state.pages) {
return (
<React.Fragment>
<h1 className="text-xl font-bold dark:text-gray-100">{this.title()}</h1>
@ -106,6 +128,7 @@ class PagesModal extends React.Component {
return (
<Modal site={this.props.site}>
{ this.renderBody() }
{ this.renderLoading() }
</Modal>
)
}

@ -456,13 +456,16 @@ defmodule Plausible.Stats.Clickhouse do
)
end
def entry_pages(site, query, limit, include) do
def entry_pages(site, query, limit, page \\ 1, include) do
offset = (page - 1) * limit
q =
from(
s in base_session_query(site, query),
group_by: s.entry_page,
order_by: [desc: fragment("count")],
limit: ^limit,
offset: ^offset,
select: %{
name: s.entry_page,
count: fragment("uniq(?) as count", s.user_id)
@ -487,12 +490,15 @@ defmodule Plausible.Stats.Clickhouse do
end
end
def top_pages(site, %Query{period: "realtime"} = query, limit, _include) do
def top_pages(site, %Query{period: "realtime"} = query, limit, page, _include) do
offset = (page - 1) * limit
ClickhouseRepo.all(
from s in base_session_query(site, query),
group_by: s.exit_page,
order_by: [desc: fragment("count")],
limit: ^limit,
offset: ^offset,
select: %{
name: fragment("? as name", s.exit_page),
count: fragment("uniq(?) as count", s.user_id)
@ -500,13 +506,16 @@ defmodule Plausible.Stats.Clickhouse do
)
end
def top_pages(site, query, limit, include) do
def top_pages(site, query, limit, page, include) do
offset = (page - 1) * limit
q =
from(
e in base_query(site, query),
group_by: e.pathname,
order_by: [desc: fragment("count")],
limit: ^limit,
offset: ^offset,
select: %{
name: fragment("? as name", e.pathname),
count: fragment("uniq(?) as count", e.user_id),

@ -212,8 +212,9 @@ defmodule PlausibleWeb.Api.StatsController do
query = Query.from(site.timezone, params)
include = if params["include"], do: String.split(params["include"], ","), else: []
limit = if params["limit"], do: String.to_integer(params["limit"])
page = if params["page"], do: String.to_integer(params["page"])
json(conn, Stats.top_pages(site, query, limit || 9, include))
json(conn, Stats.top_pages(site, query, limit || 9, page || 1, include))
end
def entry_pages(conn, params) do
@ -221,8 +222,9 @@ defmodule PlausibleWeb.Api.StatsController do
query = Query.from(site.timezone, params)
include = if params["include"], do: String.split(params["include"], ","), else: []
limit = if params["limit"], do: String.to_integer(params["limit"])
page = if params["page"], do: String.to_integer(params["page"])
json(conn, Stats.entry_pages(site, query, limit || 9, include))
json(conn, Stats.entry_pages(site, query, limit || 9, page || 1, include))
end
def countries(conn, params) do

@ -56,7 +56,7 @@ defmodule Plausible.Workers.SendEmailReport do
prev_bounce_rate = Stats.bounce_rate(site, Query.shift_back(query))
change_bounce_rate = if prev_bounce_rate > 0, do: bounce_rate - prev_bounce_rate
referrers = Stats.top_sources(site, query, 5, 1, [])
pages = Stats.top_pages(site, query, 5, [])
pages = Stats.top_pages(site, query, 5, 1, [])
user = Plausible.Auth.find_user_by(email: email)
login_link = user && Plausible.Sites.is_owner?(user.id, site)

Loading…
Cancel
Save