Class: Workarea::Admin::Dashboards::MarketingViewModel

Inherits:
ApplicationViewModel
  • Object
show all
Includes:
InsightsGraphs
Defined in:
app/view_models/workarea/admin/dashboards/marketing_view_model.rb

Instance Method Summary collapse

Methods included from InsightsGraphs

#ends_at, #previous_ends_at, #previous_starts_at, #starts_at

Instance Method Details

#created_at_in_time_zoneObject



64
65
66
# File 'app/view_models/workarea/admin/dashboards/marketing_view_model.rb', line 64

def created_at_in_time_zone
  { 'date' => '$created_at', 'timezone' => Time.zone.tzinfo.name }
end

#email_signupsObject



15
16
17
18
19
# File 'app/view_models/workarea/admin/dashboards/marketing_view_model.rb', line 15

def email_signups
  @email_signups ||= Email::
    .by_date(starts_at: starts_at.beginning_of_day, ends_at: ends_at.end_of_day)
    .count
end

#email_signups_graph_dataObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/view_models/workarea/admin/dashboards/marketing_view_model.rb', line 32

def email_signups_graph_data
  @email_signups_graph_data ||=
    begin
      query = Email::.collection.aggregate(
        [
          {
            '$match' => {
              'created_at' => {
                '$gte' => starts_at.beginning_of_day.utc,
                '$lte' => ends_at.end_of_day.utc
              }
            }
          },
          {
            '$group' => {
              '_id' => {
                'day' => { '$dayOfMonth' => created_at_in_time_zone },
                'month' => { '$month' => created_at_in_time_zone },
                'year' => { '$year' => created_at_in_time_zone }
              },
              'created_at' => { '$first' => '$created_at' },
              'count' => { '$sum' => 1 }
            }
          },
          { '$sort' => { 'created_at' => 1 } }
        ]
      )

      find_graph_data(query.to_a, :count)
    end
end

#email_signups_percent_changeObject



21
22
23
24
25
26
27
28
29
30
# File 'app/view_models/workarea/admin/dashboards/marketing_view_model.rb', line 21

def email_signups_percent_change
  @email_signups_percent_change ||=
    begin
      first = Email::
        .by_date(starts_at: previous_starts_at.beginning_of_day, ends_at: previous_ends_at.end_of_day)
        .count

      calculate_percent_change(first, email_signups)
    end
end

#insightsObject



68
69
70
71
72
# File 'app/view_models/workarea/admin/dashboards/marketing_view_model.rb', line 68

def insights
  @insights ||= InsightViewModel.wrap(
    Workarea::Insights::Base.by_dashboard('marketing').page(options[:page])
  )
end

#traffic_referrer_graph_dataObject



9
10
11
12
13
# File 'app/view_models/workarea/admin/dashboards/marketing_view_model.rb', line 9

def traffic_referrer_graph_data
  traffic_referrer_report.results.take(5).reduce({}) do |memo, result|
    memo.merge(result['source'] => result['revenue'])
  end
end