Class: Analytics::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/ruby_cms/templates/services/analytics/report.rb

Constant Summary collapse

DEFAULT_CACHE_DURATION_SECONDS =
600
10
DEFAULT_MAX_TOP_VISITORS =
10
DEFAULT_MAX_REFERRERS =
10
DEFAULT_MAX_LANDING_PAGES =
10
DEFAULT_MAX_UTM_SOURCES =
10
DEFAULT_HIGH_VOLUME_THRESHOLD =
1000
DEFAULT_RAPID_REQUEST_THRESHOLD =
50
DEFAULT_RECENT_PAGE_VIEWS_LIMIT =
25
DEFAULT_PAGE_DETAILS_LIMIT =
100
DEFAULT_VISITOR_DETAILS_LIMIT =
100
DEFAULT_MAX_EXIT_PAGES =
10
DEFAULT_MAX_CONVERSIONS =
10
EVENT_PAGE_VIEW =

Supported Ahoy event names. Use these constants when calling ahoy.track in the host app. page_view: tracked automatically via RubyCms::PageTracking (page_name:, request_path:) conversion: tracked by the host app, e.g. ahoy.track "conversion", goal: "contact_form"

"page_view"
EVENT_CONVERSION =
"conversion"

Instance Method Summary collapse

Constructor Details

#initialize(start_date:, end_date:, period: nil) ⇒ Report

Returns a new instance of Report.



25
26
27
28
29
30
31
# File 'lib/generators/ruby_cms/templates/services/analytics/report.rb', line 25

def initialize(start_date:, end_date:, period: nil)
  @start_date = start_date.to_date.beginning_of_day
  @end_date = end_date.to_date.end_of_day
  @period = period.presence || RubyCms::Settings.get(:analytics_default_period,
                                                     default: "week").to_s
  @range = @start_date..@end_date
end

Instance Method Details

#dashboard_statsObject



33
34
35
36
37
# File 'lib/generators/ruby_cms/templates/services/analytics/report.rb', line 33

def dashboard_stats
  Rails.cache.fetch(cache_key("dashboard"), expires_in: cache_duration) do
    dashboard_stats_payload
  end
end

#page_stats(page_name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/generators/ruby_cms/templates/services/analytics/report.rb', line 39

def page_stats(page_name)
  scoped = page_view_events.where(page_name:)

  {
    page_views: scoped.order(time: :desc).limit(page_details_limit),
    stats: {
      total_views: scoped.count,
      unique_visitors: scoped.joins(:visit).distinct.count("ahoy_visits.visitor_token"),
      avg_views_per_day: (scoped.count.to_f / days_in_range).round(2)
    }
  }
end

#visitor_stats(ip_address) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/generators/ruby_cms/templates/services/analytics/report.rb', line 52

def visitor_stats(ip_address)
  visitor_visits = visits.where(ip: ip_address)
  visitor_events = page_view_events.joins(:visit).where(ahoy_visits: { ip: ip_address })

  {
    visitor_views: visitor_events.order(time: :desc).limit(visitor_details_limit),
    stats: {
      total_views: visitor_events.count,
      unique_pages: visitor_events.where.not(page_name: [ nil, "" ]).distinct.count(:page_name),
      first_visit: visitor_visits.minimum(:started_at),
      last_visit: visitor_visits.maximum(:started_at)
    }
  }
end