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