Class: Spotlight::Analytics::DashboardComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/spotlight/analytics/dashboard_component.rb

Overview

Display Analytics

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_exhibit:) ⇒ DashboardComponent

Returns a new instance of DashboardComponent.



9
10
11
12
13
14
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 9

def initialize(current_exhibit:)
  super
  @current_exhibit = current_exhibit
  @default_start_date = [min_date, 1.year.ago].max
  @default_end_date = [max_date, Time.zone.today].min
end

Instance Attribute Details

#current_exhibitObject (readonly)

Returns the value of attribute current_exhibit.



7
8
9
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 7

def current_exhibit
  @current_exhibit
end

#datesObject (readonly)

Returns the value of attribute dates.



7
8
9
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 7

def dates
  @dates
end

Instance Method Details

#before_renderObject



16
17
18
19
20
21
22
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 16

def before_render
  flash[:error] = nil
  @dates = { 'start_date' => @default_start_date.to_date.to_s,
             'end_date' => @default_end_date.to_date.to_s }

  validate_dates if params[:start_date] || params[:end_date]
end

#headingObject



36
37
38
39
40
41
42
43
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 36

def heading
  if params[:start_date] || params[:end_date]
    I18n.t('spotlight.dashboards.analytics.reporting_period_heading_dynamic', start_date: formatted_date(dates['start_date']),
                                                                              end_date: formatted_date(dates['end_date']))
  else
    I18n.t('spotlight.dashboards.analytics.reporting_period_heading')
  end
end

#max_dateObject



28
29
30
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 28

def max_date
  Spotlight::Engine.config.ga_date_range['end_date'] || Time.zone.today
end

#min_dateObject



24
25
26
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 24

def min_date
  Spotlight::Engine.config.ga_date_range['start_date'] || Date.new(2015, 8, 14) # This is the minimum date supported by GA
end

#noteObject



32
33
34
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 32

def note
  I18n.t('spotlight.dashboards.analytics.note', default: nil)
end

#page_analyticsObject



53
54
55
56
57
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 53

def page_analytics
  Rails.cache.fetch([current_exhibit, dates['start_date'], dates['end_date'], 'page_analytics'], expires_in: 1.hour) do
    current_exhibit.page_analytics(dates, page_url)
  end
end

#page_urlObject



49
50
51
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 49

def page_url
  @page_url ||= helpers.exhibit_root_path(current_exhibit)
end

#results?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 45

def results?
  page_analytics.totals.to_h.present? || search_analytics.totals.to_h.present?
end

#search_analyticsObject



59
60
61
62
63
# File 'app/components/spotlight/analytics/dashboard_component.rb', line 59

def search_analytics
  Rails.cache.fetch([current_exhibit, dates['start_date'], dates['end_date'], 'search_analytics'], expires_in: 1.hour) do
    current_exhibit.analytics(dates, page_url)
  end
end