diff --git a/assets/js/dashboard/stats/graph/graph-util.js b/assets/js/dashboard/stats/graph/graph-util.js index 539d90545..8fb3d1fd7 100644 --- a/assets/js/dashboard/stats/graph/graph-util.js +++ b/assets/js/dashboard/stats/graph/graph-util.js @@ -6,6 +6,7 @@ export const METRIC_MAPPING = { 'Unique visitors': 'visitors', 'Visit duration': 'visit_duration', 'Total pageviews': 'pageviews', + 'Visits': 'visits', 'Bounce rate': 'bounce_rate', 'Unique conversions': 'conversions', } @@ -13,6 +14,7 @@ export const METRIC_MAPPING = { export const METRIC_LABELS = { 'visitors': 'Visitors', 'pageviews': 'Pageviews', + 'visits': 'Visits', 'bounce_rate': 'Bounce Rate', 'visit_duration': 'Visit Duration', 'conversions': 'Converted Visitors', @@ -21,6 +23,7 @@ export const METRIC_LABELS = { export const METRIC_FORMATTER = { 'visitors': numberFormatter, 'pageviews': numberFormatter, + 'visits': numberFormatter, 'bounce_rate': (number) => (`${number}%`), 'visit_duration': durationFormatter, 'conversions': numberFormatter, diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.js index 0030f5df3..89f891503 100644 --- a/assets/js/dashboard/stats/graph/visitor-graph.js +++ b/assets/js/dashboard/stats/graph/visitor-graph.js @@ -476,6 +476,7 @@ export default class VisitorGraph extends React.Component { fetchTopStatData() { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/top-stats`, this.props.query) .then((res) => { + res.top_stats = this.maybeRemoveVisitsMetric(res.top_stats) this.setState({ topStatsLoadingState: LoadingState.loaded, topStatData: res }, () => { this.storeTopStatsContainerHeight() this.resetMetric() @@ -484,6 +485,14 @@ export default class VisitorGraph extends React.Component { }) } + maybeRemoveVisitsMetric(top_stats) { + if (this.props.site.flags.visits_metric) { + return top_stats + } else { + return top_stats.filter((stat) => {return stat.name !== "Visits"}) + } + } + renderInner() { const { query, site } = this.props; const { graphData, metric, topStatData, topStatsLoadingState, mainGraphLoadingState } = this.state; diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index e8adff12d..b51927ca3 100644 --- a/lib/plausible_web/controllers/api/stats_controller.ex +++ b/lib/plausible_web/controllers/api/stats_controller.ex @@ -353,9 +353,9 @@ defmodule PlausibleWeb.Api.StatsController do metrics = if query.filters["event:page"] do - [:visitors, :pageviews, :bounce_rate, :time_on_page, :sample_percent] + [:visitors, :pageviews, :bounce_rate, :time_on_page, :visits, :sample_percent] else - [:visitors, :pageviews, :bounce_rate, :visit_duration, :sample_percent] + [:visitors, :pageviews, :bounce_rate, :visit_duration, :visits, :sample_percent] end current_results = Stats.aggregate(site, query, metrics) @@ -365,6 +365,7 @@ defmodule PlausibleWeb.Api.StatsController do [ top_stats_entry(current_results, prev_results, "Unique visitors", :visitors), top_stats_entry(current_results, prev_results, "Total pageviews", :pageviews), + top_stats_entry(current_results, prev_results, "Visits", :visits), top_stats_entry(current_results, prev_results, "Bounce rate", :bounce_rate), top_stats_entry(current_results, prev_results, "Visit duration", :visit_duration), top_stats_entry(current_results, prev_results, "Time on page", :time_on_page) diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex index a4403ea39..7607d75ba 100644 --- a/lib/plausible_web/controllers/stats_controller.ex +++ b/lib/plausible_web/controllers/stats_controller.ex @@ -95,7 +95,17 @@ defmodule PlausibleWeb.StatsController do site = conn.assigns[:site] query = Query.from(site, params) |> Filters.add_prefix() - metrics = [:visitors, :pageviews, :bounce_rate, :visit_duration] + visits_metric_enabled = + FunWithFlags.enabled?(:visits_metric, for: conn.assigns[:current_user]) || + Mix.env() == :test + + metrics = + if visits_metric_enabled && !query.filters["event:goal"] do + [:visitors, :pageviews, :visits, :bounce_rate, :visit_duration] + else + [:visitors, :pageviews, :bounce_rate, :visit_duration] + end + graph = Plausible.Stats.timeseries(site, query, metrics) headers = [:date | metrics] @@ -312,6 +322,7 @@ defmodule PlausibleWeb.StatsController do defp get_flags(user) do %{ custom_dimension_filter: FunWithFlags.enabled?(:custom_dimension_filter, for: user), + visits_metric: FunWithFlags.enabled?(:visits_metric, for: user), comparisons: FunWithFlags.enabled?(:comparisons, for: user) } end diff --git a/test/plausible/imported/imported_test.exs b/test/plausible/imported/imported_test.exs index b30c11ea9..54e36af30 100644 --- a/test/plausible/imported/imported_test.exs +++ b/test/plausible/imported/imported_test.exs @@ -1129,6 +1129,11 @@ defmodule Plausible.ImportedTest do "name" => "Total pageviews", "value" => 1 }, + %{ + "change" => 100, + "name" => "Visits", + "value" => 1 + }, %{ "change" => nil, "name" => "Bounce rate", diff --git a/test/plausible_web/controllers/CSVs/30d-filter-path/visitors.csv b/test/plausible_web/controllers/CSVs/30d-filter-path/visitors.csv index b79ea927f..b0ae49474 100644 --- a/test/plausible_web/controllers/CSVs/30d-filter-path/visitors.csv +++ b/test/plausible_web/controllers/CSVs/30d-filter-path/visitors.csv @@ -1,32 +1,32 @@ -date,visitors,pageviews,bounce_rate,visit_duration -2021-09-20,0,0,, -2021-09-21,0,0,, -2021-09-22,0,0,, -2021-09-23,0,0,, -2021-09-24,0,0,, -2021-09-25,0,0,, -2021-09-26,0,0,, -2021-09-27,0,0,, -2021-09-28,0,0,, -2021-09-29,0,0,, -2021-09-30,0,0,, -2021-10-01,0,0,, -2021-10-02,0,0,, -2021-10-03,0,0,, -2021-10-04,0,0,, -2021-10-05,0,0,, -2021-10-06,0,0,, -2021-10-07,0,0,, -2021-10-08,0,0,, -2021-10-09,0,0,, -2021-10-10,0,0,, -2021-10-11,0,0,, -2021-10-12,0,0,, -2021-10-13,0,0,, -2021-10-14,0,0,, -2021-10-15,0,0,, -2021-10-16,0,0,, -2021-10-17,0,0,, -2021-10-18,0,0,, -2021-10-19,0,0,, -2021-10-20,1,1,, +date,visitors,pageviews,visits,bounce_rate,visit_duration +2021-09-20,0,0,0,, +2021-09-21,0,0,0,, +2021-09-22,0,0,0,, +2021-09-23,0,0,0,, +2021-09-24,0,0,0,, +2021-09-25,0,0,0,, +2021-09-26,0,0,0,, +2021-09-27,0,0,0,, +2021-09-28,0,0,0,, +2021-09-29,0,0,0,, +2021-09-30,0,0,0,, +2021-10-01,0,0,0,, +2021-10-02,0,0,0,, +2021-10-03,0,0,0,, +2021-10-04,0,0,0,, +2021-10-05,0,0,0,, +2021-10-06,0,0,0,, +2021-10-07,0,0,0,, +2021-10-08,0,0,0,, +2021-10-09,0,0,0,, +2021-10-10,0,0,0,, +2021-10-11,0,0,0,, +2021-10-12,0,0,0,, +2021-10-13,0,0,0,, +2021-10-14,0,0,0,, +2021-10-15,0,0,0,, +2021-10-16,0,0,0,, +2021-10-17,0,0,0,, +2021-10-18,0,0,0,, +2021-10-19,0,0,0,, +2021-10-20,1,1,0,, diff --git a/test/plausible_web/controllers/CSVs/30d/visitors.csv b/test/plausible_web/controllers/CSVs/30d/visitors.csv index df94365f9..c23dbb7c2 100644 --- a/test/plausible_web/controllers/CSVs/30d/visitors.csv +++ b/test/plausible_web/controllers/CSVs/30d/visitors.csv @@ -1,32 +1,32 @@ -date,visitors,pageviews,bounce_rate,visit_duration -2021-09-20,1,1,100,0 -2021-09-21,0,0,, -2021-09-22,0,0,, -2021-09-23,0,0,, -2021-09-24,0,0,, -2021-09-25,0,0,, -2021-09-26,0,0,, -2021-09-27,0,0,, -2021-09-28,0,0,, -2021-09-29,0,0,, -2021-09-30,0,0,, -2021-10-01,0,0,, -2021-10-02,0,0,, -2021-10-03,0,0,, -2021-10-04,0,0,, -2021-10-05,0,0,, -2021-10-06,0,0,, -2021-10-07,0,0,, -2021-10-08,0,0,, -2021-10-09,0,0,, -2021-10-10,0,0,, -2021-10-11,0,0,, -2021-10-12,0,0,, -2021-10-13,0,0,, -2021-10-14,0,0,, -2021-10-15,0,0,, -2021-10-16,0,0,, -2021-10-17,0,0,, -2021-10-18,0,0,, -2021-10-19,2,1,100,0 -2021-10-20,1,2,0,60 +date,visitors,pageviews,visits,bounce_rate,visit_duration +2021-09-20,1,1,1,100,0 +2021-09-21,0,0,0,, +2021-09-22,0,0,0,, +2021-09-23,0,0,0,, +2021-09-24,0,0,0,, +2021-09-25,0,0,0,, +2021-09-26,0,0,0,, +2021-09-27,0,0,0,, +2021-09-28,0,0,0,, +2021-09-29,0,0,0,, +2021-09-30,0,0,0,, +2021-10-01,0,0,0,, +2021-10-02,0,0,0,, +2021-10-03,0,0,0,, +2021-10-04,0,0,0,, +2021-10-05,0,0,0,, +2021-10-06,0,0,0,, +2021-10-07,0,0,0,, +2021-10-08,0,0,0,, +2021-10-09,0,0,0,, +2021-10-10,0,0,0,, +2021-10-11,0,0,0,, +2021-10-12,0,0,0,, +2021-10-13,0,0,0,, +2021-10-14,0,0,0,, +2021-10-15,0,0,0,, +2021-10-16,0,0,0,, +2021-10-17,0,0,0,, +2021-10-18,0,0,0,, +2021-10-19,2,1,2,100,0 +2021-10-20,1,2,1,0,60 diff --git a/test/plausible_web/controllers/CSVs/6m/visitors.csv b/test/plausible_web/controllers/CSVs/6m/visitors.csv index ed3d3b5e7..b7c61f1db 100644 --- a/test/plausible_web/controllers/CSVs/6m/visitors.csv +++ b/test/plausible_web/controllers/CSVs/6m/visitors.csv @@ -1,7 +1,7 @@ -date,visitors,pageviews,bounce_rate,visit_duration -2021-05-01,1,1,100,0 -2021-06-01,0,0,, -2021-07-01,0,0,, -2021-08-01,0,0,, -2021-09-01,1,1,100,0 -2021-10-01,3,3,67,20 +date,visitors,pageviews,visits,bounce_rate,visit_duration +2021-05-01,1,1,1,100,0 +2021-06-01,0,0,0,, +2021-07-01,0,0,0,, +2021-08-01,0,0,0,, +2021-09-01,1,1,1,100,0 +2021-10-01,3,3,3,67,20 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 8da56f09b..c19c47803 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 @@ -32,6 +32,20 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do assert %{"name" => "Total pageviews", "value" => 3, "change" => 100} in res["top_stats"] end + test "counts total visits", %{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:01:00]), + build(:pageview, user_id: @user_id, timestamp: ~N[2021-01-01 10:00:00]), + build(:pageview, timestamp: ~N[2021-01-01 15:00:00]) + ]) + + conn = get(conn, "/api/stats/#{site.domain}/top-stats?period=day&date=2021-01-01") + + res = json_response(conn, 200) + assert %{"name" => "Visits", "value" => 3, "change" => 100} in res["top_stats"] + end + test "calculates bounce rate", %{conn: conn, site: site} do populate_stats(site, [ build(:pageview, user_id: @user_id), @@ -294,6 +308,38 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do res = json_response(conn, 200) assert %{"name" => "Unique visitors", "value" => 2, "change" => 100} in res["top_stats"] end + + test "returns number of visits from one specific referral source", %{conn: conn, site: site} do + populate_stats(site, [ + build(:pageview, + user_id: @user_id, + referrer_source: "Google", + timestamp: ~N[2021-01-01 00:00:00] + ), + build(:pageview, + user_id: @user_id, + referrer_source: "Google", + timestamp: ~N[2021-01-01 00:05:00] + ), + build(:pageview, + user_id: @user_id, + referrer_source: "Google", + timestamp: ~N[2021-01-01 05:00:00] + ), + build(:pageview, timestamp: ~N[2021-01-01 00:10:00]) + ]) + + filters = Jason.encode!(%{source: "Google"}) + + conn = + get( + conn, + "/api/stats/#{site.domain}/top-stats?period=day&date=2021-01-01&filters=#{filters}" + ) + + res = json_response(conn, 200) + assert %{"name" => "Visits", "value" => 2, "change" => 100} in res["top_stats"] + end end describe "GET /api/stats/top-stats - filtered for goal" do diff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exs index 23b9a4c0e..dc08d3db2 100644 --- a/test/plausible_web/controllers/stats_controller_test.exs +++ b/test/plausible_web/controllers/stats_controller_test.exs @@ -119,12 +119,12 @@ defmodule PlausibleWeb.StatsControllerTest do |> Enum.map(&String.split(&1, ",")) assert parsed_csv == [ - ["date", "visitors", "pageviews", "bounce_rate", "visit_duration"], - ["2021-09-20", "1", "1", "100", "0"], - ["2021-09-27", "0", "0", "", ""], - ["2021-10-04", "0", "0", "", ""], - ["2021-10-11", "0", "0", "", ""], - ["2021-10-18", "3", "3", "67", "20"], + ["date", "visitors", "pageviews", "visits", "bounce_rate", "visit_duration"], + ["2021-09-20", "1", "1", "1", "100", "0"], + ["2021-09-27", "0", "0", "0", "", ""], + ["2021-10-04", "0", "0", "0", "", ""], + ["2021-10-11", "0", "0", "0", "", ""], + ["2021-10-18", "3", "3", "3", "67", "20"], [""] ] end