Class: ActiveCohort

Inherits:
Object
  • Object
show all
Defined in:
lib/active_cohort.rb

Overview

Public: Provides a cohort analysis of a set of ActiveRecord objects. Intended to be consumed by domain-specific classes, such AigAnalyst, OrderAnalyst, etc. See the constructor’s documentation for information on the options hash.

Examples

cohort = ActiveCohort.new(some_options_hash)
cohort.generate_report
# => [["", "Week 0", "Week 1", "Week 2", "Week 3", "Week 4", "Week 5"],
     ["1/9", "27.0%", "8.1%", "2.7%", "0.0%", "0.0%", "0.0%"],
     ["1/16", "37.9%", "7.6%", "0.0%", "0.0%", "0.0%"],
     ["1/23", "42.2%", "3.1%", "0.0%", "0.0%"],
     ["1/30", "31.8%", "0.0%", "0.0%"],
     ["2/6", "-", "-"]]

puts cohort.to_csv
# => ,Week 0,Week 1,Week 2,Week 3,Week 4,Week 5
     1/9,27.0%,8.1%,2.7%,0.0%,0.0%,0.0%
     1/16,37.9%,7.6%,0.0%,0.0%,0.0%
     1/23,42.2%,3.1%,0.0%,0.0%
     1/30,31.8%,0.0%,0.0%
     2/6,-,-

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject_collection, activation_lambda, opts = {}) ⇒ ActiveCohort

Public: Initialize a ActiveCohort.

Required params

subject_collection          - An ActiveRecord collection of records to perform a
                              cohort analysis on.
activation_lambda           - A lambda that returns a boolean indicating whether
                              a given record has activated (e.g., converted,
                              signed up, purchased, etc.)
opts                        - A String naming the widget.
  start_at                  - The date at which to begin the analysis.
                              Default: 30 days ago.
  interval                  - A string representation of the interval to run the analysis
                              over (e.g, day, week, etc.) For instance, 'week' would
                              result in a week-over-week analysis.
                              Default: 'day'.
  interval_timestamp_field  - A String representation of the timestamp
                              field on the cohort records to be used to
                              offset between intervals.
                              Default: 'created_at'.


46
47
48
49
50
# File 'lib/active_cohort.rb', line 46

def initialize(subject_collection, activation_lambda, opts={})
  @subject_collection = subject_collection
  @activation_lambda = activation_lambda
  opts.each { |k,v| instance_variable_set("@#{k}", v) }
end

Instance Attribute Details

#activation_lambdaObject

Returns the value of attribute activation_lambda.



24
25
26
# File 'lib/active_cohort.rb', line 24

def activation_lambda
  @activation_lambda
end

#interval_timestamp_fieldObject



68
69
70
# File 'lib/active_cohort.rb', line 68

def interval_timestamp_field
  @interval_timestamp_field || 'created_at'
end

#start_atObject



64
65
66
# File 'lib/active_cohort.rb', line 64

def start_at
  @start_at || 30.days.ago
end

#subject_collectionObject

Returns the value of attribute subject_collection.



24
25
26
# File 'lib/active_cohort.rb', line 24

def subject_collection
  @subject_collection
end

Instance Method Details

#generate_reportObject

Public: Generates a cohort report using params supplied to the instance in the constructor.

Example

cohort.generate_report
# => [["", "Week 0", "Week 1", "Week 2", "Week 3", "Week 4", "Week 5"],
     ["1/9", "27.0%", "8.1%", "2.7%", "0.0%", "0.0%", "0.0%"],
     ["1/16", "37.9%", "7.6%", "0.0%", "0.0%", "0.0%"],
     ["1/23", "42.2%", "3.1%", "0.0%", "0.0%"],
     ["1/30", "31.8%", "0.0%", "0.0%"],
     ["2/6", "-", "-"]]

Returns an Array of values representing the report.



85
86
87
88
89
90
91
92
93
94
# File 'lib/active_cohort.rb', line 85

def generate_report
  validate_required_fields
  @report = []
  @report << header

  (number_of_intervals - 1).times do |row|
    @report << build_row(row)
  end
  @report
end

#intervalObject



52
53
54
# File 'lib/active_cohort.rb', line 52

def interval
  @interval || 'day'
end

#interval=(interval) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/active_cohort.rb', line 56

def interval=(interval)
  unless interval.downcase.in? valid_intervals
    raise "The interval \"#{interval}\" isn't valid.\n" +
          "Use #{valid_intervals.join ', '}"
  end
  @interval = interval.downcase
end

#to_csv(seperator = ',') ⇒ Object

Public: Outputs the cohort report in CSV format. Does not regenerate the report if the instance has already generated it.

Example

puts cohort.to_csv
# => ,Week 0,Week 1,Week 2,Week 3,Week 4,Week 5
     1/9,27.0%,8.1%,2.7%,0.0%,0.0%,0.0%
     1/16,37.9%,7.6%,0.0%,0.0%,0.0%
     1/23,42.2%,3.1%,0.0%,0.0%
     1/30,31.8%,0.0%,0.0%
     2/6,-,-

Returns a String representation of the report with CSV formatting.



109
110
111
112
# File 'lib/active_cohort.rb', line 109

def to_csv(seperator=',')
  report = @report || generate_report
  report.map{ |row| row.join(seperator) }.join("\n")
end