Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Paginate referrers (#313)
Uku Taht49f153c38075
diff --git a/assets/js/dashboard/stats/modals/referrers.js b/assets/js/dashboard/stats/modals/referrers.jsindex ad4136304a..97213ad454 100644--- a/assets/js/dashboard/stats/modals/referrers.js+++ b/assets/js/dashboard/stats/modals/referrers.js@@ -12,26 +12,39 @@ class ReferrersModal extends React.Component {super(props)this.state = {loading: true,- query: parseQuery(props.location.search, props.site)+ referrers: [],+ query: parseQuery(props.location.search, props.site),+ page: 1,+ moreResultsAvailable: false}}- componentDidMount() {- if (this.state.query.filters.goal) {- api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/goal/referrers`, this.state.query, {limit: 100})- .then((res) => this.setState({loading: false, referrers: res}))+ loadReferrers() {+ const {query, page, referrers} = this.state++ if (query.filters.goal) {+ api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/goal/referrers`, query, {limit: 100, page: page})+ .then((res) => this.setState({loading: false, referrers: referrers.concat(res), moreResultsAvailable: res.length === 100}))} else {const include = this.showExtra() ? 'bounce_rate,visit_duration' : null- api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers`, this.state.query, { limit: 100, include: include, show_noref: true})- .then((res) => this.setState({loading: false, referrers: res}))+ api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers`, query, {limit: 100, page: page, include: include, show_noref: true})+ .then((res) => this.setState({loading: false, referrers: referrers.concat(res), moreResultsAvailable: res.length === 100}))}}+ componentDidMount() {+ this.loadReferrers()+ }+showExtra() {return this.state.query.period !== 'realtime' && !this.state.query.filters.goal}+ loadMore() {+ this.setState({loading: true, page: this.state.page + 1}, this.loadReferrers.bind(this))+ }+formatBounceRate(page) {if (typeof(page.bounce_rate) === 'number') {return page.bounce_rate + '%'@@ -69,33 +82,16 @@ class ReferrersModal extends React.Component {return this.state.query.period === 'realtime' ? 'Current visitors' : 'Visitors'}- 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>- )- } else if (this.state.referrers) {- return (- <React.Fragment>- <h1 className="text-xl font-bold">Top Sources</h1>-- <div className="my-4 border-b border-gray-300"></div>- <main className="modal__content">- <table className="w-full table-striped table-fixed">- <thead>- <tr>- <th className="p-2 text-xs tracking-wide font-bold text-gray-500" align="left">Referrer</th>- <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">{this.label()}</th>- {this.showExtra() && <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">Visit duration</th>}- </tr>- </thead>- <tbody>- { this.state.referrers.map(this.renderReferrer.bind(this)) }- </tbody>- </table>- </main>- </React.Fragment>+ <div className="w-full text-center my-4">+ <button onClick={this.loadMore.bind(this)} type="button" className="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:shadow-outline-indigo active:bg-indigo-700 transition ease-in-out duration-150">+ Load more+ </button>+ </div>)}}@@ -103,7 +99,27 @@ class ReferrersModal extends React.Component {render() {return (<Modal site={this.props.site}>- { this.renderBody() }+ <h1 className="text-xl font-bold">Top Sources</h1>++ <div className="my-4 border-b border-gray-300"></div>++ <main className="modal__content">+ <table className="w-full table-striped table-fixed">+ <thead>+ <tr>+ <th className="p-2 text-xs tracking-wide font-bold text-gray-500" align="left">Referrer</th>+ <th className="p-2 w-32 text-xs tracking-wide font-bold text-gray-500" align="right">{this.label()}</th>+ {this.showExtra() && <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">Visit duration</th>}+ </tr>+ </thead>+ <tbody>+ { this.state.referrers.map(this.renderReferrer.bind(this)) }+ </tbody>+ </table>+ </main>++ { this.renderLoading() }</Modal>)}diff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.exindex aaceef8574..34c4f1469d 100644--- a/lib/plausible/stats/clickhouse.ex+++ b/lib/plausible/stats/clickhouse.ex@@ -196,13 +196,14 @@ defmodule Plausible.Stats.Clickhouse dores["visitors"]end- def top_referrers_for_goal(site, query, limit \\ 5) do+ def top_referrers_for_goal(site, query, limit, page) doconverted_sessions =from(from e in base_query(site, query),select: %{session_id: e.session_id})+ offset = (page - 1) * limitPlausible.Clickhouse.all(from s in Plausible.ClickhouseSession,join: cs in subquery(converted_sessions),@@ -213,19 +214,22 @@ defmodule Plausible.Stats.Clickhouse dowhere: s.referrer_source != "",group_by: s.referrer_source,order_by: [desc: fragment("count")],- limit: ^limit+ limit: ^limit,+ offset: ^offset)|> 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, show_noref \\ false, include \\ []) do+ def top_referrers(site, query, limit, page, show_noref \\ false, include \\ []) do+ offset = (page - 1) * limitreferrers =from(s in base_session_query(site, query),group_by: s.referrer_source,- order_by: [desc: fragment("count")],- limit: ^limit+ order_by: [desc: fragment("count"), asc: fragment("min(start)")],+ limit: ^limit,+ offset: ^offset)referrers = if show_noref dodiff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.exindex ccefef3139..8750f56365 100644--- a/lib/plausible_web/controllers/api/stats_controller.ex+++ b/lib/plausible_web/controllers/api/stats_controller.ex@@ -130,15 +130,18 @@ defmodule PlausibleWeb.Api.StatsController doquery = 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"])show_noref = params["show_noref"] == "true"- json(conn, Stats.top_referrers(site, query, limit || 9, show_noref, include))+ json(conn, Stats.top_referrers(site, query, limit || 9, page || 1, show_noref, include))enddef referrers_for_goal(conn, params) dosite = conn.assigns[:site]query = Query.from(site.timezone, params)+ limit = if params["limit"], do: String.to_integer(params["limit"])+ page = if params["page"], do: String.to_integer(params["page"])- json(conn, Stats.top_referrers_for_goal(site, query, params["limit"] || 9))+ json(conn, Stats.top_referrers_for_goal(site, query, limit || 9, page || 1))end@google_api Application.fetch_env!(:plausible, :google_api)diff --git a/lib/workers/send_email_report.ex b/lib/workers/send_email_report.exindex 2d4d7771fa..47df0a25ef 100644--- a/lib/workers/send_email_report.ex+++ b/lib/workers/send_email_report.ex@@ -55,7 +55,7 @@ defmodule Plausible.Workers.SendEmailReport dobounce_rate = Stats.bounce_rate(site, query)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_referrers(site, query, 5, [])+ referrers = Stats.top_referrers(site, query, 5, 1, [])pages = Stats.top_pages(site, query, 5, [])user = Plausible.Auth.find_user_by(email: email)login_link = user && Plausible.Sites.is_owner?(user.id, site)diff --git a/test/plausible_web/controllers/api/stats_controller/referrers_test.exs b/test/plausible_web/controllers/api/stats_controller/referrers_test.exsindex f40d3968d9..42a8cb1a73 100644--- a/test/plausible_web/controllers/api/stats_controller/referrers_test.exs+++ b/test/plausible_web/controllers/api/stats_controller/referrers_test.exs@@ -47,8 +47,17 @@ defmodule PlausibleWeb.Api.StatsController.ReferrersTest do%{"name" => "Bing", "count" => 1, "url" => "bing.com"}]end++ test "can paginate the results", %{conn: conn, site: site} do+ conn = get(conn, "/api/stats/#{site.domain}/referrers?period=day&date=2019-01-01&limit=1&page=2")++ assert json_response(conn, 200) == [+ %{"name" => "Bing", "count" => 1, "url" => ""}+ ]+ endend+describe "GET /api/stats/:domain/goal/referrers" dosetup [:create_user, :log_in, :create_site]