我使用此代碼來拉我想要使用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