我使用此代码来拉我想要使用GARB。
require 'rubygems'
require 'garb'
require 'csv'
CA_CERT_FILE = "Cthe exact location of your cacert.pem file"
username = "your google analytics email address"
password = "your google analytics password"
web_profile = "UA-the correct number here"
limit = 10000
filter = {:productName.contains => "some product"} # this is your filter
sorter = :date.desC# you can use this part to sort
csvfile = "YourResults.csv"
Garb::Session.login(username, password, :secure => true)
class Report
extend Garb::Model
metrics :itemQuantity
dimensions :productName,
:date,
:customVarName2
end
CSV.open(csvfile, "w") do |csv|
csv << [ "itemQuantity",
"productName",
"date",
"customVarName2"
]
end
1.times do |i| # Be careful, you can cause duplication with this iteration. only do once per 10,000 expected rows
profile = Garb::Management::Profile.all.detect {|p| p.web_property_id == web_profile}
options = {
:start_date => (Date.today - 30),
:end_date => Date.today,
:limit => limit,
:offset => (i*limit) + 1,
:sort => sorter
}
result = Report.results(profile, options)
result = Report.results(profile, :filters => filter)
CSV.open(csvfile, "a") do |csv|
result.each do |r|
csv << ["#{r.item_quantity}",
"#{r.product_name}",
"#{r.date}",
"#{r.custom_var_name2}"
]
end
end
end
要找到cacert.pem文件放在这里http://curl.haxx.se/ca/cacert.pem 保存,作为一个文件,并在代码中引用它。
有一点要注意的是,你将需要请求在pascalcase的指标和维度,但然后把它们放进你的CSV以下划线情况下(为什么,我不知道)。
除此之外,你很乖。完成后请打开CSV文件。
太谢谢你了。这是我的头脑。 – Lee 2011-05-23 21:25:34