Created
November 16, 2010 05:50
-
-
Save jchris/701494 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'garb' | |
# couchrest is only required because it makes the dates to_json into | |
# something javascript can parse. | |
require 'couchrest' | |
class OpenStruct | |
def to_json | |
table.to_json | |
end | |
end | |
COUCHDB_URI = "http://jchris.couchone.com/ga-stats/_bulk_docs" | |
# save JSON to CouchDB | |
def save_json json_str | |
uri = URI.parse(COUCHDB_URI) | |
http = Net::HTTP.new(uri.host, uri.port) | |
post = Net::HTTP::Post.new(uri.path) | |
post.content_type = 'application/json' | |
post.body = json_str | |
http.request post do |resp| | |
unless resp.code == "201" | |
raise "post failed #{resp.inspect}" | |
end | |
puts "success" | |
end | |
end | |
# Google Analytics login | |
username = "[email protected]" | |
password = "x" | |
Garb::Session.login(username, password) | |
site = Garb::Profile.first('UA-15057807-1') | |
# this is the report we want | |
class VisitsByPage | |
extend Garb::Resource | |
metrics :visits, :pageviews, :bounces | |
dimensions :page_path | |
sort :pageviews | |
end | |
# todo loop over times from today backwards to the first readings | |
Day = 24 * 3600 | |
End = Time.now | |
opts = { | |
:start_date => End - Day, | |
:end_date => End | |
} | |
# TODO: get metrics for finer-grained slices of time (eg daily not monthly) | |
puts "getting VisitsByPage" | |
results = VisitsByPage.results(site, opts) | |
json = {:docs => results.map{|r|r.range = opts; r}}.to_json | |
# json = {:docs => [{"ok"=>"test"}]}.to_json | |
puts "saving VisitsByPage to #{COUCHDB_URI}" | |
save_json(json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment