Class: Statwhore::Google::Analytics::Profile

Inherits:
Google::Base
  • Object
show all
Defined in:
lib/statwhore/google/analytics/profile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Profile

Returns a new instance of Profile.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
# File 'lib/statwhore/google/analytics/profile.rb', line 20

def initialize(attrs)
  raise ArgumentError, ":profile_id is required" unless attrs.has_key?(:profile_id)
  @account_id = attrs[:account_id]  if attrs.has_key?(:account_id)
  @name       = attrs[:name]        if attrs.has_key?(:name)
  @profile_id = attrs[:profile_id]  if attrs.has_key?(:profile_id)
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



18
19
20
# File 'lib/statwhore/google/analytics/profile.rb', line 18

def 
  @account_id
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/statwhore/google/analytics/profile.rb', line 18

def name
  @name
end

#profile_idObject

Returns the value of attribute profile_id.



18
19
20
# File 'lib/statwhore/google/analytics/profile.rb', line 18

def profile_id
  @profile_id
end

Class Method Details

.find(account_id, profile_id) ⇒ Object



14
15
16
# File 'lib/statwhore/google/analytics/profile.rb', line 14

def self.find(, profile_id)
  find_all().detect { |p| p.profile_id.to_s == profile_id.to_s }
end

.find_all(account_id) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/statwhore/google/analytics/profile.rb', line 5

def self.find_all()
  doc = Hpricot::XML(get("https://www.google.com:443/analytics/home/admin?scid=#{account_id}"))
  (doc/'select[@name=profile_list] option').inject([]) do |profiles, option|
    profile_id = option['value'].to_i
    profiles << Profile.new(:account_id => , :profile_id => profile_id, :name => option.inner_html) if profile_id > 0
    profiles
  end
end

Instance Method Details

#ensure_datetime_in_google_format(time) ⇒ Object

takes a Date, Time or String



69
70
71
# File 'lib/statwhore/google/analytics/profile.rb', line 69

def ensure_datetime_in_google_format(time)
  time.is_a?(Time) || time.is_a?(Date) ? time.strftime('%Y%m%d') : time
end

#pageviews(options = {}) ⇒ Object



52
53
54
# File 'lib/statwhore/google/analytics/profile.rb', line 52

def pageviews(options={})
  get_item_summary_by_message(options.merge(:message => 'pageviews'))
end

#pageviews_by_day(options = {}) ⇒ Object



56
57
58
# File 'lib/statwhore/google/analytics/profile.rb', line 56

def pageviews_by_day(options={})
  get_serie_by_label(options.merge({:label => 'pageviews'}))
end

#report(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/statwhore/google/analytics/profile.rb', line 27

def report(options={})
  options.reverse_merge!({
    :report  => 'Dashboard',
    :from    => Time.now.utc - 7.days,
    :to      => Time.now.utc,
    :tab     => 0,
    :format  => FORMAT_XML,
    :compute => 'average',
    :view    => 0
  })
  options[:from] = ensure_datetime_in_google_format(options[:from])
  options[:to]   = ensure_datetime_in_google_format(options[:to])
  
  params = {
    :pdr  => "#{options[:from]}-#{options[:to]}",
    :rpt  => "#{options[:report]}Report",
    :cmp  => options[:compute],
    :fmt  => options[:format],
    :view => options[:view],
    :tab  => options[:tab],
    :id   => profile_id,
  }
  self.class.get("https://google.com/analytics/reporting/export", :query_hash => params)
end

#to_sObject



73
74
75
# File 'lib/statwhore/google/analytics/profile.rb', line 73

def to_s
  "#{name} (#{profile_id})"
end

#visits(options = {}) ⇒ Object



60
61
62
# File 'lib/statwhore/google/analytics/profile.rb', line 60

def visits(options={})
  get_item_summary_by_message(options.merge(:message => 'visits'))
end

#visits_by_day(options = {}) ⇒ Object



64
65
66
# File 'lib/statwhore/google/analytics/profile.rb', line 64

def visits_by_day(options={})
  get_serie_by_label(options.merge({:label => 'visits'}))
end