Class: Orange::AnalyticsResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/orange-more/analytics/resources/analytics_resource.rb

Instance Method Summary collapse

Instance Method Details

#gatticaObject



12
13
14
15
# File 'lib/orange-more/analytics/resources/analytics_resource.rb', line 12

def gattica
  return false unless options[:email]
  @gattica ||= Gattica.new(options)
end

#pageviews(route, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/orange-more/analytics/resources/analytics_resource.rb', line 17

def pageviews(route, opts = {})
  return "No GA" unless gattica
  r = route.to_s
  # Strip trailing slash if present. GA doesn't like it.
  if r.rindex('/') > 0
    r[r.rindex('/')] = ''
  end
  # authenticate with the API via email/password
  ga = gattica
  accounts = ga.accounts
  ga.profile_id = options[:profile] || accounts.first.profile_id
  views = ""
  data = ga.get({ :start_date => '2009-01-01', 
                  :end_date => Time.now.localtime.strftime("%Y-%m-%d"),
                  :dimensions => ['pagePath'],
                  :metrics => ['pageviews'],
                  :filters => ['pagePath == '+route.to_s[0..-1]]
                  }.merge(opts))
  unless data.points.length == 0
    views = data.points[0].metrics[0][:pageviews]
    views 
  else
    0
  end
end

#stack_initObject



6
7
8
9
10
# File 'lib/orange-more/analytics/resources/analytics_resource.rb', line 6

def stack_init
  options[:email] = orange.options['ga_email']
  options[:password] = orange.options['ga_password']
  options[:profile] = orange.options['ga_profile']
end