Skip to content

Instantly share code, notes, and snippets.

@hrysd
Created January 8, 2013 01:29
Show Gist options
  • Select an option

  • Save hrysd/4480286 to your computer and use it in GitHub Desktop.

Select an option

Save hrysd/4480286 to your computer and use it in GitHub Desktop.
define_method かっこいい。
require 'garb'
require 'active_support/time'
class Visitor
extend Garb::Model
metrics :visitors, :percent_new_visits
end
module Reporter
class << self
%w(day week month).each do |period|
define_method("between_a_#{period}") do |time|
Garb::Session.login(ENV['ACCOUNT'], ENV['PASSWORD'])
profile = Garb::Management::Profile.all.detect { |p| p.web_property_id == ENV['WPID'] }
analytics = profile.visitor(
start_date: time.send(:"beginning_of_#{period}"),
end_date: time.send(:"end_of_#{period}")
)
result = {}
analytics.each do |r|
result[:visitors] = r.visitors
result[:percent_new_visits] = r.percent_new_visits
end
result
end
end
end
end
source :rubygems
gem 'garb'
gem 'active_support'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment