module Mitamirri class LeadsReport # Search params attr_accessor :site attr_accessor :time_period attr_accessor :start_date attr_accessor :end_date # Stat attributes # Initialization def initialize(args = {}) args.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)} self.time_period = args[:time_period] || 'past 6 months' self.start_date = self.dates.first self.end_date = self.dates.last end # Generate an array of dates spanning the appropriate time period. def dates return @dates if @dates @dates = [] case self.time_period.downcase when 'past month' _increment = 4 when 'past 3 months' _increment = 12 when 'past 6 months' _increment = 24 when 'past 12 months' _increment = 52 else 'all time' _increment = 104 end (1.._increment).each{ |i| @dates << (Time.zone.now - i.weeks).beginning_of_week } @dates.reverse! @dates << Time.zone.now @dates end # Graphing objects def leads(args) _leads = [] _leads << Lead.new(:stats => lead_stats(args)) _leads end def lead_stats(args) _lead_stats = [] dates.each do |date| _total_sessions = TrackableSession.search_without_date(args).for_week(date).count _total_conversions = TrackableSession.search_without_date(args).for_week(date).with_conversions.count _conversion_rate = (_total_conversions.to_f / _total_sessions) * 100 _lead_stats << LeadStat.new(:date => date, :leads => _total_conversions, :conversion_rate => sprintf("%0.1f",_conversion_rate).to_f) end _lead_stats end # Inner stat classes class Lead attr_accessor :stats def initialize(args) args.each{ |k,v| self.send("#{k}=", v) } end end class LeadStat attr_accessor :date attr_accessor :leads attr_accessor :conversion_rate def initialize(args) args.each{ |k,v| self.send("#{k}=", v) } end def short_date self.date.to_s(:concise) end end end end