Add visits metric under a feature flag (#2687)

* Add visits metric and make it graphable

* include visits metric in csv export (visitors.csv)

* put visits under a feature flag (CSV export)

* feature flag for displaying visits on the dashboard

* fix formatting

* add visits metric to top stats (fix)

* fix imported_test to expect visits metric included

* fix formatting
ua-device-type
RobertJoonas 4 years ago committed by Uku Taht
parent 6510d93d14
commit 76dc081bbe

@ -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,

@ -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;

@ -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)

@ -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

@ -1129,6 +1129,11 @@ defmodule Plausible.ImportedTest do
"name" => "Total pageviews",
"value" => 1
},
%{
"change" => 100,
"name" => "Visits",
"value" => 1
},
%{
"change" => nil,
"name" => "Bounce rate",

@ -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,,

1 date visitors pageviews visits bounce_rate visit_duration
2 2021-09-20 0 0 0
3 2021-09-21 0 0 0
4 2021-09-22 0 0 0
5 2021-09-23 0 0 0
6 2021-09-24 0 0 0
7 2021-09-25 0 0 0
8 2021-09-26 0 0 0
9 2021-09-27 0 0 0
10 2021-09-28 0 0 0
11 2021-09-29 0 0 0
12 2021-09-30 0 0 0
13 2021-10-01 0 0 0
14 2021-10-02 0 0 0
15 2021-10-03 0 0 0
16 2021-10-04 0 0 0
17 2021-10-05 0 0 0
18 2021-10-06 0 0 0
19 2021-10-07 0 0 0
20 2021-10-08 0 0 0
21 2021-10-09 0 0 0
22 2021-10-10 0 0 0
23 2021-10-11 0 0 0
24 2021-10-12 0 0 0
25 2021-10-13 0 0 0
26 2021-10-14 0 0 0
27 2021-10-15 0 0 0
28 2021-10-16 0 0 0
29 2021-10-17 0 0 0
30 2021-10-18 0 0 0
31 2021-10-19 0 0 0
32 2021-10-20 1 1 0

@ -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

1 date visitors pageviews visits bounce_rate visit_duration
2 2021-09-20 1 1 1 100 0
3 2021-09-21 0 0 0
4 2021-09-22 0 0 0
5 2021-09-23 0 0 0
6 2021-09-24 0 0 0
7 2021-09-25 0 0 0
8 2021-09-26 0 0 0
9 2021-09-27 0 0 0
10 2021-09-28 0 0 0
11 2021-09-29 0 0 0
12 2021-09-30 0 0 0
13 2021-10-01 0 0 0
14 2021-10-02 0 0 0
15 2021-10-03 0 0 0
16 2021-10-04 0 0 0
17 2021-10-05 0 0 0
18 2021-10-06 0 0 0
19 2021-10-07 0 0 0
20 2021-10-08 0 0 0
21 2021-10-09 0 0 0
22 2021-10-10 0 0 0
23 2021-10-11 0 0 0
24 2021-10-12 0 0 0
25 2021-10-13 0 0 0
26 2021-10-14 0 0 0
27 2021-10-15 0 0 0
28 2021-10-16 0 0 0
29 2021-10-17 0 0 0
30 2021-10-18 0 0 0
31 2021-10-19 2 1 2 100 0
32 2021-10-20 1 2 1 0 60

@ -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

1 date visitors pageviews visits bounce_rate visit_duration
2 2021-05-01 1 1 1 100 0
3 2021-06-01 0 0 0
4 2021-07-01 0 0 0
5 2021-08-01 0 0 0
6 2021-09-01 1 1 1 100 0
7 2021-10-01 3 3 3 67 20

@ -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

@ -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

Loading…
Cancel
Save