Class: Analytics::Report

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

Constant Summary collapse

METRICS =
[ 
  :visitors, :new_visits, :percent_new_visits,
  :visits, :bounces, :pageviews
]
DIMENSIONS =
[
  :continent, :city, :country, :date, :year, 
  :month, :week, :day, :hour
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, access_token, renderer = Analytics::Renderer::Plain) ⇒ Report

Returns a new instance of Report.



16
17
18
19
20
# File 'lib/analytics/report.rb', line 16

def initialize(id, access_token, renderer = Analytics::Renderer::Plain)
  @id = id
  @access_token = access_token
  @renderer = renderer
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (private)



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/analytics/report.rb', line 45

def method_missing(m, *args, &block)
  args.flatten!
  if METRICS.include? m.to_sym
    get(args[0],args[1], [m.to_sym])
  elsif m.to_s.split('_by_').length.eql? 2
    lhs, rhs = split_by(m, 'by')
    get(args[0],args[1], split_by(lhs), split_by(rhs))
  elsif split_by(m).length.eql? 2
    get(args[0],args[1], split_by(m), [])
  else
    super
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



14
15
16
# File 'lib/analytics/report.rb', line 14

def access_token
  @access_token
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/analytics/report.rb', line 14

def id
  @id
end

Instance Method Details

#get(start_date, end_date, metrics, dimensions = []) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/analytics/report.rb', line 22

def get(start_date, end_date, metrics, dimensions = [])
  response = Analytics::Request.new(Analytics::BASE_URL, get_url(start_date, end_date, metrics, dimensions), @access_token).response
  return nil unless response["rows"]
  response["rows"].map do |row|
    @renderer.render(row, metrics, dimensions)
  end 
end