@@ -25,33 +25,7 @@ def initialize(start_date:, end_date:, period: nil)
2525
2626 def dashboard_stats
2727 Rails . cache . fetch ( cache_key ( "dashboard" ) , expires_in : cache_duration ) do
28- total_views = page_view_events . count
29- total_sessions = visits . distinct . count ( :visit_token )
30- unique_visitors = visits . distinct . count ( :visitor_token )
31-
32- {
33- total_page_views : total_views ,
34- unique_visitors : unique_visitors ,
35- total_sessions : total_sessions ,
36- pages_per_session : total_sessions . positive? ? ( total_views . to_f / total_sessions ) . round ( 1 ) : 0 ,
37- bounce_rate : compute_bounce_rate ,
38- new_visitor_percentage : compute_new_visitor_percentage ,
39- avg_daily_views : days_in_range . positive? ? ( total_views . to_f / days_in_range ) . round ( 0 ) . to_i : 0 ,
40- popular_pages : popular_pages_data ,
41- top_visitors : top_visitors_data ,
42- hourly_activity : hourly_activity_data ,
43- daily_activity : daily_activity_data ,
44- daily_visitors : daily_visitors_data ,
45- top_referrers : referrer_data ,
46- landing_pages : landing_pages_data ,
47- utm_sources : utm_sources_data ,
48- browser_stats : visits . where . not ( browser : [ nil , "" ] ) . group ( :browser ) . count ,
49- device_stats : visits . where . not ( device_type : [ nil , "" ] ) . group ( :device_type ) . count ,
50- os_stats : visits . where . not ( os : [ nil , "" ] ) . group ( :os ) . count ,
51- suspicious_activity : suspicious_activity_data ,
52- recent_page_views : page_view_events . order ( time : :desc ) . limit ( recent_page_views_limit ) ,
53- extra_cards : extra_cards_data
54- }
28+ dashboard_stats_payload
5529 end
5630 end
5731
@@ -352,7 +326,7 @@ def compute_bounce_rate
352326 return 0 unless total . positive?
353327
354328 event_counts = page_view_events . group ( :visit_id ) . count
355- single_page = event_counts . count { |_ , c | c == 1 }
329+ single_page = event_counts . count { |_ , c | c == 1 }
356330 ( ( single_page . to_f / total ) * 100 ) . round ( 1 )
357331 rescue StandardError
358332 0
@@ -363,9 +337,9 @@ def compute_new_visitor_percentage
363337 return 0 unless total . positive?
364338
365339 returning_tokens = Ahoy ::Visit
366- . where ( " started_at < ?" , @start_date )
367- . distinct
368- . pluck ( :visitor_token )
340+ . where ( started_at : ... @start_date )
341+ . distinct
342+ . pluck ( :visitor_token )
369343
370344 new_count = visits . where . not ( visitor_token : returning_tokens ) . distinct . count ( :visitor_token )
371345 ( ( new_count . to_f / total ) * 100 ) . round ( 0 ) . to_i
@@ -391,6 +365,36 @@ def visitor_details_limit
391365 RubyCms ::Settings . get ( :analytics_visitor_details_limit ,
392366 default : DEFAULT_VISITOR_DETAILS_LIMIT ) . to_i
393367 end
368+
369+ def dashboard_stats_payload
370+ total_views = page_view_events . count
371+ total_sessions = visits . distinct . count ( :visit_token )
372+ unique_visitors = visits . distinct . count ( :visitor_token )
373+
374+ {
375+ total_page_views : total_views ,
376+ unique_visitors : unique_visitors ,
377+ total_sessions : total_sessions ,
378+ pages_per_session : total_sessions . positive? ? ( total_views . to_f / total_sessions ) . round ( 1 ) : 0 ,
379+ bounce_rate : compute_bounce_rate ,
380+ new_visitor_percentage : compute_new_visitor_percentage ,
381+ avg_daily_views : days_in_range . positive? ? ( total_views . to_f / days_in_range ) . round ( 0 ) . to_i : 0 ,
382+ popular_pages : popular_pages_data ,
383+ top_visitors : top_visitors_data ,
384+ hourly_activity : hourly_activity_data ,
385+ daily_activity : daily_activity_data ,
386+ daily_visitors : daily_visitors_data ,
387+ top_referrers : referrer_data ,
388+ landing_pages : landing_pages_data ,
389+ utm_sources : utm_sources_data ,
390+ browser_stats : visits . where . not ( browser : [ nil , "" ] ) . group ( :browser ) . count ,
391+ device_stats : visits . where . not ( device_type : [ nil , "" ] ) . group ( :device_type ) . count ,
392+ os_stats : visits . where . not ( os : [ nil , "" ] ) . group ( :os ) . count ,
393+ suspicious_activity : suspicious_activity_data ,
394+ recent_page_views : page_view_events . order ( time : :desc ) . limit ( recent_page_views_limit ) ,
395+ extra_cards : extra_cards_data
396+ }
397+ end
394398 end
395399 end
396400end
0 commit comments