Class: Bluecap::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/bluecap/handlers/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Report

Initialize a Report handler.

data - A Hash containing options to scope the report by:

:initial_event     - The String event that cohorts shared.
:engagement_event  - The String event to track engagement by.
:attributes        - The Hash attributes of users (optional).
:start_date        - The String start date of the report.
:end_date          - The String end date of the report.

Examples

Bluecap::Report.new(
  initial_event: 'Created Account',
  engagement_event: 'Logged In',
  attributes: {
    country: 'Australia',
    gender: 'Female'
  },
  start_date: '20120401',
  end_date: '20120430'
)


30
31
32
33
34
35
36
# File 'lib/bluecap/handlers/report.rb', line 30

def initialize(data)
  @initial_event = data.fetch(:initial_event)
  @engagement_event = data.fetch(:engagement_event)
  @attributes = data.fetch(:attributes, Hash.new)
  @start_date = Date.parse(data.fetch(:start_date))
  @end_date = Date.parse(data.fetch(:end_date))
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/bluecap/handlers/report.rb', line 7

def attributes
  @attributes
end

#engagement_eventObject (readonly)

Returns the value of attribute engagement_event.



7
8
9
# File 'lib/bluecap/handlers/report.rb', line 7

def engagement_event
  @engagement_event
end

#initial_eventObject (readonly)

Returns the value of attribute initial_event.



7
8
9
# File 'lib/bluecap/handlers/report.rb', line 7

def initial_event
  @initial_event
end

#year_monthObject (readonly)

Returns the value of attribute year_month.



7
8
9
# File 'lib/bluecap/handlers/report.rb', line 7

def year_month
  @year_month
end

Instance Method Details

#handleObject

Generates a report to track engagement of cohorts over time.

Returns the String with report data in JSON format.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bluecap/handlers/report.rb', line 45

def handle
  report = Hash.new
  (@start_date...@end_date).each do |date|
    cohort = Cohort.new(
      :initial_event => @initial_event,
      :attributes => @attributes,
      :date => date,
      :report_id => report_id
    )

    # The start date of the engagement is measured from the day after the
    # initial event.
    engagement = Engagement.new(
      :cohort => cohort,
      :engagement_event => engagement_event,
      :start_date => date + 1,
      :end_date => @end_date,
      :report_id => report_id
    )

    cohort_results = Hash.new
    cohort_results[:total] = cohort.total
    cohort_results[:engagement] = engagement.measure
    report[date.strftime('%Y%m%d')] = cohort_results
  end

  MultiJson.dump(report)
end

#report_idObject



38
39
40
# File 'lib/bluecap/handlers/report.rb', line 38

def report_id
  Bluecap.redis.incr('report.ids')
end