module Mitamirri class IntersiteTrafficReport # Search params attr_accessor :site attr_accessor :time_period attr_accessor :visit_kind # 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.sessions end def destination_histogram @destination_histogram ||= TrackableSession.clickthroughs_histogram(self.sessions.to_hash) end def destination_sites self.destination_histogram.keys end def sessions @sessions ||= TrackableSession.search( :site => self.site, :time_period => self.time_period, :visit_kind => self.visit_kind, :action_kind => 'clickthrough' ) 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 _increment = 52 end (1.._increment).each{ |i| @dates << (Time.zone.now - i.weeks).end_of_week } @dates.reverse! @dates << Time.zone.now.end_of_week @dates end # Graphing data def total_visits(args) [Visit.new(:stats => visit_stats(args))] end def total_visit_stats(args) _visit_stats = [] dates.each do |date| _visit_stats << VisitStat.new(:date => date, :clickthroughs => TrackableSession.search_without_date(args).for_week(date).with_clickthroughs.count) end _visit_stats end def destinations(args) _destinations = [] self.destination_sites.each do |site| _destinations << Destination.new(:site => site, :clickthroughs => TrackableSession.search(args).with_clickthroughs.count) end _destinations end def visit_stats(args) _visit_stats = [] dates.each do |date| _visit_stats << VisitStat.new(:date => date, :clickthroughs => TrackableSession.search_without_date(args).for_week(date).with_clickthroughs.count) end _visit_stats end # Inner stat classes class Destination attr_accessor :site attr_accessor :clickthroughs def initialize(args) args.each{ |k,v| self.send("#{k}=", v) } end end class Visit attr_accessor :site attr_accessor :stats def initialize(args) args.each{ |k,v| self.send("#{k}=", v) } end end class VisitStat attr_accessor :date attr_accessor :clickthroughs def initialize(args) args.each{ |k,v| self.send("#{k}=", v) } end def short_date self.date.to_s(:concise) end end end end