Sha256: f35421bc5c9ae88aeeecce914f45d81ffbf94aa490d80e7ef79876764a9cdaee

Contents?: true

Size: 1.27 KB

Versions: 8

Compression:

Stored size: 1.27 KB

Contents

require 'gattica'

module Orange
  class AnalyticsResource < Orange::Resource
    call_me :analytics
    def stack_init
      options[:email] = orange.options['ga_email']
      options[:password] = orange.options['ga_password']
      options[:profile] = orange.options['ga_profile']
    end
    
    def gattica
      return false unless options[:email]
      @gattica ||= Gattica.new(options)
    end
    
    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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
orange-more-0.7.3 lib/orange-more/analytics/resources/analytics_resource.rb
orange-more-0.7.2 lib/orange-more/analytics/resources/analytics_resource.rb
orange-more-0.7.1 lib/orange-more/analytics/resources/analytics_resource.rb
orange-more-0.6.5 lib/orange-more/analytics/resources/analytics_resource.rb
orange-more-0.6.4 lib/orange-more/analytics/resources/analytics_resource.rb
orange-more-0.6.3 lib/orange-more/analytics/resources/analytics_resource.rb
orange-more-0.6.2 lib/orange-more/analytics/resources/analytics_resource.rb
orange-more-0.6.0 lib/orange-more/analytics/resources/analytics_resource.rb