2011-05-13 54 views
1

我與Garb寶石紅寶石打不過我無法訪問的結果。Accesing裝束寶石結果

這是我的代碼。

Garb::Session.login('[email protected]', 'password') 
profile = Garb::Management::Profile.all.detect {|profile| profile.web_property_id == 'UA-XXXXX-X'} 
@ga = profile.stats(:start_date => (Date.today - 1), :end_date => Date.today) 

如果我在視圖上使用調試,我可以看到結果,但無論我嘗試我不能訪​​問結果。

下面是調試結果

--- !ruby/object:Garb::ResultSet 
     results: 
     - !ruby/object:OpenStruct 
      table: 
      :exits: "7820" 
      :pageviews: "24171" 
     sampled: false 
     total_results: 1 

  • @ ga.results.table.exits
  • @ ga.exits
  • @ ga.table.exits

我已經嘗試過將它製作成一個數組,並且沒有運氣。

你以前用過這顆寶石嗎?如果是的話,我如何獲得這些結果。

感謝您的時間

回答

4

Garb::ResultSet是一個枚舉,所以你可以使用任何的枚舉方法就可以了(eachmap等)。個別結果OpenStruct S,這樣你就可以直接訪問它們。

@ga.map &:exits # returns an array of all exits 
+0

太謝謝你了。這是我的頭腦。 – Lee 2011-05-23 21:25:34

0

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