Class: Statwhore::Google::Analytics::Profile
- Inherits:
-
Google::Base
- Object
- Google::Base
- Statwhore::Google::Analytics::Profile
- Defined in:
- lib/statwhore/google/analytics/profile.rb
Instance Attribute Summary collapse
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#profile_id ⇒ Object
Returns the value of attribute profile_id.
Class Method Summary collapse
Instance Method Summary collapse
-
#ensure_datetime_in_google_format(time) ⇒ Object
takes a Date, Time or String.
-
#initialize(attrs) ⇒ Profile
constructor
A new instance of Profile.
- #pageviews(options = {}) ⇒ Object
- #pageviews_by_day(options = {}) ⇒ Object
- #report(options = {}) ⇒ Object
- #to_s ⇒ Object
- #visits(options = {}) ⇒ Object
- #visits_by_day(options = {}) ⇒ Object
Constructor Details
#initialize(attrs) ⇒ Profile
Returns a new instance of Profile.
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_id ⇒ Object
Returns the value of attribute account_id.
18 19 20 |
# File 'lib/statwhore/google/analytics/profile.rb', line 18 def account_id @account_id end |
#name ⇒ Object
Returns the value of attribute name.
18 19 20 |
# File 'lib/statwhore/google/analytics/profile.rb', line 18 def name @name end |
#profile_id ⇒ Object
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(account_id, profile_id) find_all(account_id).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(account_id) 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 => 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(={}) (.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(={}) get_serie_by_label(.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(={}) .reverse_merge!({ :report => 'Dashboard', :from => Time.now.utc - 7.days, :to => Time.now.utc, :tab => 0, :format => FORMAT_XML, :compute => 'average', :view => 0 }) [:from] = ensure_datetime_in_google_format([:from]) [:to] = ensure_datetime_in_google_format([:to]) params = { :pdr => "#{options[:from]}-#{options[:to]}", :rpt => "#{options[:report]}Report", :cmp => [:compute], :fmt => [:format], :view => [:view], :tab => [:tab], :id => profile_id, } self.class.get("https://google.com/analytics/reporting/export", :query_hash => params) end |
#to_s ⇒ Object
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(={}) (.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(={}) get_serie_by_label(.merge({:label => 'visits'})) end |