@ -6,6 +6,36 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
describe " GET /api/stats/top-stats - default " do
setup [ :create_user , :log_in , :create_new_site ]
test " returns graph_metric key for graphable top stats " , %{ conn : conn , site : site } do
[ visitors , visits , pageviews , views_per_visit , bounce_rate , visit_duration ] =
conn
|> get ( " /api/stats/ #{ site . domain } /top-stats " )
|> json_response ( 200 )
|> Map . get ( " top_stats " )
assert %{ " graph_metric " = > " visitors " } = visitors
assert %{ " graph_metric " = > " visits " } = visits
assert %{ " graph_metric " = > " pageviews " } = pageviews
assert %{ " graph_metric " = > " views_per_visit " } = views_per_visit
assert %{ " graph_metric " = > " bounce_rate " } = bounce_rate
assert %{ " graph_metric " = > " visit_duration " } = visit_duration
end
test " returns graph_metric key for graphable top stats in realtime mode " , %{
conn : conn ,
site : site
} do
[ current_visitors , unique_visitors , pageviews ] =
conn
|> get ( " /api/stats/ #{ site . domain } /top-stats?period=realtime " )
|> json_response ( 200 )
|> Map . get ( " top_stats " )
refute Map . has_key? ( current_visitors , " graph_metric " )
assert %{ " graph_metric " = > " visitors " } = unique_visitors
assert %{ " graph_metric " = > " pageviews " } = pageviews
end
test " counts distinct user ids " , %{ conn : conn , site : site } do
populate_stats ( site , [
build ( :pageview , user_id : @user_id ) ,
@ -19,7 +49,8 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
assert %{
" name " = > " Unique visitors " ,
" value " = > 2
" value " = > 2 ,
" graph_metric " = > " visitors "
} in res [ " top_stats " ]
end
@ -36,7 +67,8 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
assert %{
" name " = > " Total pageviews " ,
" value " = > 3
" value " = > 3 ,
" graph_metric " = > " pageviews "
} in res [ " top_stats " ]
end
@ -59,12 +91,16 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert res [ " top_stats " ] == [
%{ " name " = > " Unique visitors " , " value " = > 2 } ,
%{ " name " = > " Total visits " , " value " = > 2 } ,
%{ " name " = > " Total pageviews " , " value " = > 2 } ,
%{ " name " = > " Views per visit " , " value " = > 2.0 } ,
%{ " name " = > " Bounce rate " , " value " = > 0 } ,
%{ " name " = > " Visit duration " , " value " = > 120 }
%{ " name " = > " Unique visitors " , " value " = > 2 , " graph_metric " = > " visitors " } ,
%{ " name " = > " Total visits " , " value " = > 2 , " graph_metric " = > " visits " } ,
%{ " name " = > " Total pageviews " , " value " = > 2 , " graph_metric " = > " pageviews " } ,
%{
" name " = > " Views per visit " ,
" value " = > 2.0 ,
" graph_metric " = > " views_per_visit "
} ,
%{ " name " = > " Bounce rate " , " value " = > 0 , " graph_metric " = > " bounce_rate " } ,
%{ " name " = > " Visit duration " , " value " = > 120 , " graph_metric " = > " visit_duration " }
]
end
@ -80,7 +116,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Views per visit " , " value " = > 1.33 } in res [ " top_stats " ]
assert %{ " name " = > " Views per visit " , " value " = > 1.33 , " graph_metric " = > " views_per_visit " } in res [
" top_stats "
]
end
test " calculates bounce rate " , %{ conn : conn , site : site } do
@ -94,7 +132,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Bounce rate " , " value " = > 50 } in res [ " top_stats " ]
assert %{ " name " = > " Bounce rate " , " value " = > 50 , " graph_metric " = > " bounce_rate " } in res [
" top_stats "
]
end
test " calculates average visit duration " , %{ conn : conn , site : site } do
@ -116,7 +156,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Visit duration " , " value " = > 450 } in res [ " top_stats " ]
assert %{ " name " = > " Visit duration " , " value " = > 450 , " graph_metric " = > " visit_duration " } in res [
" top_stats "
]
end
test " calculates time on page instead when filtered for page " , %{ conn : conn , site : site } do
@ -339,7 +381,7 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
filters = Jason . encode! ( %{ page : " / " } )
path = " /api/stats/ #{ site . domain } /top-stats?period=day&date=2021-01-01&filters= #{ filters } "
assert %{ " name " = > " Bounce rate " , " value " = > 0 } ==
assert %{ " name " = > " Bounce rate " , " value " = > 0 , " graph_metric " = > " bounce_rate " } ==
conn
|> get ( path )
|> json_response ( 200 )
@ -435,9 +477,17 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Bounce rate " , " value " = > 0 } in res [ " top_stats " ]
assert %{ " name " = > " Views per visit " , " value " = > 0.0 } in res [ " top_stats " ]
assert %{ " name " = > " Visit duration " , " value " = > 0 } in res [ " top_stats " ]
assert %{ " name " = > " Bounce rate " , " value " = > 0 , " graph_metric " = > " bounce_rate " } in res [
" top_stats "
]
assert %{ " name " = > " Views per visit " , " value " = > 0.0 , " graph_metric " = > " views_per_visit " } in res [
" top_stats "
]
assert %{ " name " = > " Visit duration " , " value " = > 0 , " graph_metric " = > " visit_duration " } in res [
" top_stats "
]
end
test " merges imported data into all top stat metrics " , %{
@ -468,12 +518,16 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert res [ " top_stats " ] == [
%{ " name " = > " Unique visitors " , " value " = > 3 } ,
%{ " name " = > " Total visits " , " value " = > 3 } ,
%{ " name " = > " Total pageviews " , " value " = > 4 } ,
%{ " name " = > " Views per visit " , " value " = > 1.33 } ,
%{ " name " = > " Bounce rate " , " value " = > 33 } ,
%{ " name " = > " Visit duration " , " value " = > 303 }
%{ " name " = > " Unique visitors " , " value " = > 3 , " graph_metric " = > " visitors " } ,
%{ " name " = > " Total visits " , " value " = > 3 , " graph_metric " = > " visits " } ,
%{ " name " = > " Total pageviews " , " value " = > 4 , " graph_metric " = > " pageviews " } ,
%{
" name " = > " Views per visit " ,
" value " = > 1.33 ,
" graph_metric " = > " views_per_visit "
} ,
%{ " name " = > " Bounce rate " , " value " = > 33 , " graph_metric " = > " bounce_rate " } ,
%{ " name " = > " Visit duration " , " value " = > 303 , " graph_metric " = > " visit_duration " }
]
end
end
@ -504,7 +558,12 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
conn = get ( conn , " /api/stats/ #{ site . domain } /top-stats?period=realtime " )
res = json_response ( conn , 200 )
assert %{ " name " = > " Unique visitors (last 30 min) " , " value " = > 2 } in res [ " top_stats " ]
assert %{
" name " = > " Unique visitors (last 30 min) " ,
" value " = > 2 ,
" graph_metric " = > " visitors "
} in res [ " top_stats " ]
end
test " shows pageviews (last 30 minutes) " , %{ conn : conn , site : site } do
@ -518,7 +577,10 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
conn = get ( conn , " /api/stats/ #{ site . domain } /top-stats?period=realtime " )
res = json_response ( conn , 200 )
assert %{ " name " = > " Pageviews (last 30 min) " , " value " = > 3 } in res [ " top_stats " ]
assert %{ " name " = > " Pageviews (last 30 min) " , " value " = > 3 , " graph_metric " = > " pageviews " } in res [
" top_stats "
]
end
test " shows current visitors (last 5 min) with goal filter " , %{ conn : conn , site : site } do
@ -554,14 +616,77 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
conn = get ( conn , " /api/stats/ #{ site . domain } /top-stats?period=realtime&filters= #{ filters } " )
res = json_response ( conn , 200 )
assert %{ " name " = > " Unique conversions (last 30 min) " , " value " = > 2 } in res [ " top_stats " ]
assert %{ " name " = > " Total conversions (last 30 min) " , " value " = > 4 } in res [ " top_stats " ]
assert %{
" name " = > " Unique conversions (last 30 min) " ,
" value " = > 2 ,
" graph_metric " = > " visitors "
} in res [ " top_stats " ]
assert %{
" name " = > " Total conversions (last 30 min) " ,
" value " = > 4 ,
" graph_metric " = > " events "
} in res [ " top_stats " ]
end
end
describe " GET /api/stats/top-stats - filters " do
setup [ :create_user , :log_in , :create_new_site ]
test " returns graph_metric key for graphable top stats with a page filter " , %{
conn : conn ,
site : site
} do
filters = Jason . encode! ( %{ page : " /A " } )
[ visitors , visits , pageviews , bounce_rate , time_on_page ] =
conn
|> get ( " /api/stats/ #{ site . domain } /top-stats?filters= #{ filters } " )
|> json_response ( 200 )
|> Map . get ( " top_stats " )
assert %{ " graph_metric " = > " visitors " } = visitors
assert %{ " graph_metric " = > " visits " } = visits
assert %{ " graph_metric " = > " pageviews " } = pageviews
assert %{ " graph_metric " = > " bounce_rate " } = bounce_rate
refute Map . has_key? ( time_on_page , " graph_metric " )
end
test " returns graph_metric key for graphable top stats with a goal filter " , %{
conn : conn ,
site : site
} do
filters = Jason . encode! ( %{ goal : " Signup " } )
[ unique_visitors , unique_conversions , total_conversions , cr ] =
conn
|> get ( " /api/stats/ #{ site . domain } /top-stats?filters= #{ filters } " )
|> json_response ( 200 )
|> Map . get ( " top_stats " )
refute Map . has_key? ( unique_visitors , " graph_metric " )
assert %{ " graph_metric " = > " visitors " } = unique_conversions
assert %{ " graph_metric " = > " events " } = total_conversions
assert %{ " graph_metric " = > " conversion_rate " } = cr
end
test " returns graph_metric key for graphable top stats with a goal filter in realtime mode " ,
%{ conn : conn , site : site } do
filters = Jason . encode! ( %{ goal : " Signup " } )
[ current_visitors , unique_conversions , total_conversions ] =
conn
|> get ( " /api/stats/ #{ site . domain } /top-stats?period=realtime&filters= #{ filters } " )
|> json_response ( 200 )
|> Map . get ( " top_stats " )
refute Map . has_key? ( current_visitors , " graph_metric " )
assert %{ " graph_metric " = > " visitors " } = unique_conversions
assert %{ " graph_metric " = > " events " } = total_conversions
end
test " returns only visitors from a country based on alpha2 code " , %{ conn : conn , site : site } do
populate_stats ( site , [
build ( :pageview , country_code : " US " ) ,
@ -579,7 +704,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Unique visitors " , " value " = > 2 } in res [ " top_stats " ]
assert %{ " name " = > " Unique visitors " , " value " = > 2 , " graph_metric " = > " visitors " } in res [
" top_stats "
]
end
test " page glob filter " , %{ conn : conn , site : site } do
@ -599,7 +726,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Unique visitors " , " value " = > 2 } in res [ " top_stats " ]
assert %{ " name " = > " Unique visitors " , " value " = > 2 , " graph_metric " = > " visitors " } in res [
" top_stats "
]
end
test " contains (~) filter " , %{ conn : conn , site : site } do
@ -619,7 +748,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Unique visitors " , " value " = > 2 } in res [ " top_stats " ]
assert %{ " name " = > " Unique visitors " , " value " = > 2 , " graph_metric " = > " visitors " } in res [
" top_stats "
]
end
test " returns only visitors with specific screen size " , %{ conn : conn , site : site } do
@ -639,7 +770,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Unique visitors " , " value " = > 2 } in res [ " top_stats " ]
assert %{ " name " = > " Unique visitors " , " value " = > 2 , " graph_metric " = > " visitors " } in res [
" top_stats "
]
end
test " returns only visitors with specific screen size for a given hostname " , %{
@ -670,8 +803,13 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res =
json_response ( conn , 200 )
assert %{ " name " = > " Unique visitors " , " value " = > 3 } in res [ " top_stats " ]
assert %{ " name " = > " Total visits " , " value " = > 3 } in res [ " top_stats " ]
assert %{ " name " = > " Unique visitors " , " value " = > 3 , " graph_metric " = > " visitors " } in res [
" top_stats "
]
assert %{ " name " = > " Total visits " , " value " = > 3 , " graph_metric " = > " visits " } in res [
" top_stats "
]
end
test " returns only visitors with specific browser " , %{ conn : conn , site : site } do
@ -691,7 +829,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Unique visitors " , " value " = > 2 } in res [ " top_stats " ]
assert %{ " name " = > " Unique visitors " , " value " = > 2 , " graph_metric " = > " visitors " } in res [
" top_stats "
]
end
test " returns only visitors with specific operating system " , %{ conn : conn , site : site } do
@ -711,7 +851,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Unique visitors " , " value " = > 2 } in res [ " top_stats " ]
assert %{ " name " = > " Unique visitors " , " value " = > 2 , " graph_metric " = > " visitors " } in res [
" top_stats "
]
end
test " returns number of visits from one specific referral source " , %{ conn : conn , site : site } do
@ -744,7 +886,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Total visits " , " value " = > 2 } in res [ " top_stats " ]
assert %{ " name " = > " Total visits " , " value " = > 2 , " graph_metric " = > " visits " } in res [
" top_stats "
]
end
test " does not return the views_per_visit metric when a page filter is applied " , %{
@ -789,8 +933,13 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Unique visitors " , " value " = > 2 } in res [ " top_stats " ]
assert %{ " name " = > " Total pageviews " , " value " = > 2 } in res [ " top_stats " ]
assert %{ " name " = > " Unique visitors " , " value " = > 2 , " graph_metric " = > " visitors " } in res [
" top_stats "
]
assert %{ " name " = > " Total pageviews " , " value " = > 2 , " graph_metric " = > " pageviews " } in res [
" top_stats "
]
end
test " hostname glob filter " , %{ conn : conn , site : site } do
@ -814,8 +963,13 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res =
json_response ( conn , 200 )
assert %{ " name " = > " Unique visitors " , " value " = > 4 } in res [ " top_stats " ]
assert %{ " name " = > " Total pageviews " , " value " = > 6 } in res [ " top_stats " ]
assert %{ " name " = > " Unique visitors " , " value " = > 4 , " graph_metric " = > " visitors " } in res [
" top_stats "
]
assert %{ " name " = > " Total pageviews " , " value " = > 6 , " graph_metric " = > " pageviews " } in res [
" top_stats "
]
end
test " hostname glob subdomain filter " , %{ conn : conn , site : site } do
@ -833,7 +987,6 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
] )
filters = Jason . encode! ( %{ hostname : " *.example.com " } )
# filters = Jason.encode!(%{page: "/blog/*"})
conn =
get (
@ -843,8 +996,13 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Unique visitors " , " value " = > 3 } in res [ " top_stats " ]
assert %{ " name " = > " Total pageviews " , " value " = > 4 } in res [ " top_stats " ]
assert %{ " name " = > " Unique visitors " , " value " = > 3 , " graph_metric " = > " visitors " } in res [
" top_stats "
]
assert %{ " name " = > " Total pageviews " , " value " = > 4 , " graph_metric " = > " pageviews " } in res [
" top_stats "
]
end
end
@ -890,7 +1048,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Unique conversions " , " value " = > 1 } in res [ " top_stats " ]
assert %{ " name " = > " Unique conversions " , " value " = > 1 , " graph_metric " = > " visitors " } in res [
" top_stats "
]
end
test " returns conversion rate " , %{ conn : conn , site : site } do
@ -911,7 +1071,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Conversion rate " , " value " = > 33.3 } in res [ " top_stats " ]
assert %{ " name " = > " Conversion rate " , " value " = > 33.3 , " graph_metric " = > " conversion_rate " } in res [
" top_stats "
]
end
test " returns conversion rate without change when comparison mode disallowed " , %{
@ -935,7 +1097,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Conversion rate " , " value " = > 33.3 } in res [ " top_stats " ]
assert %{ " name " = > " Conversion rate " , " value " = > 33.3 , " graph_metric " = > " conversion_rate " } in res [
" top_stats "
]
end
@tag :full_build_only
@ -972,12 +1136,14 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
assert %{
" name " = > " Average revenue " ,
" value " = > %{ " long " = > " $1,659.50 " , " short " = > " $1.7K " }
" value " = > %{ " long " = > " $1,659.50 " , " short " = > " $1.7K " } ,
" graph_metric " = > " average_revenue "
} in top_stats
assert %{
" name " = > " Total revenue " ,
" value " = > %{ " long " = > " $3,319.00 " , " short " = > " $3.3K " }
" value " = > %{ " long " = > " $3,319.00 " , " short " = > " $3.3K " } ,
" graph_metric " = > " total_revenue "
} in top_stats
end
@ -1029,12 +1195,14 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
assert %{
" name " = > " Average revenue " ,
" value " = > %{ " long " = > " $1,659.50 " , " short " = > " $1.7K " }
" value " = > %{ " long " = > " $1,659.50 " , " short " = > " $1.7K " } ,
" graph_metric " = > " average_revenue "
} in top_stats
assert %{
" name " = > " Total revenue " ,
" value " = > %{ " long " = > " $6,638.00 " , " short " = > " $6.6K " }
" value " = > %{ " long " = > " $6,638.00 " , " short " = > " $6.6K " } ,
" graph_metric " = > " total_revenue "
} in top_stats
end
@ -1101,7 +1269,7 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
describe " GET /api/stats/top-stats - with comparisons " do
setup [ :create_user , :log_in , :create_new_site , :add_imported_data ]
test " d efaults to previous period when comparison is not se t" , %{ site : site , conn : conn } do
test " d oes not return comparisons by defaul t" , %{ site : site , conn : conn } do
populate_stats ( site , [
build ( :pageview , timestamp : ~N[ 2020-12-31 00:00:00 ] ) ,
build ( :pageview , timestamp : ~N[ 2020-12-31 00:00:00 ] ) ,
@ -1114,7 +1282,9 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " name " = > " Total visits " , " value " = > 3 } in res [ " top_stats " ]
assert %{ " name " = > " Total visits " , " value " = > 3 , " graph_metric " = > " visits " } in res [
" top_stats "
]
end
test " returns comparison data when mode is custom " , %{ site : site , conn : conn } do
@ -1136,7 +1306,13 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response ( conn , 200 )
assert %{ " change " = > 0 , " comparison_value " = > 3 , " name " = > " Total visits " , " value " = > 3 } in res [
assert %{
" change " = > 0 ,
" comparison_value " = > 3 ,
" name " = > " Total visits " ,
" value " = > 3 ,
" graph_metric " = > " visits "
} in res [
" top_stats "
]
end
@ -1195,7 +1371,13 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
assert %{ " top_stats " = > top_stats , " with_imported " = > true } = json_response ( conn , 200 )
assert %{ " change " = > 100 , " comparison_value " = > 2 , " name " = > " Total visits " , " value " = > 4 } in top_stats
assert %{
" change " = > 100 ,
" comparison_value " = > 2 ,
" name " = > " Total visits " ,
" value " = > 4 ,
" graph_metric " = > " visits "
} in top_stats
end
test " does not compare imported data when with_imported is set to false " , %{
@ -1230,7 +1412,13 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
assert %{ " top_stats " = > top_stats , " with_imported " = > false } = json_response ( conn , 200 )
assert %{ " change " = > 100 , " comparison_value " = > 0 , " name " = > " Total visits " , " value " = > 4 } in top_stats
assert %{
" change " = > 100 ,
" comparison_value " = > 0 ,
" name " = > " Total visits " ,
" value " = > 4 ,
" graph_metric " = > " visits "
} in top_stats
end
test " compares conversion rates " , %{ conn : conn , site : site } do
@ -1258,7 +1446,8 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
" change " = > - 33.4 ,
" comparison_value " = > 66.7 ,
" name " = > " Conversion rate " ,
" value " = > 33.3
" value " = > 33.3 ,
" graph_metric " = > " conversion_rate "
} in res [ " top_stats " ]
end
end