开发者

Accessing Garb Gem Results

I am playing with the Garb gem for ruby but I'm having trouble accessing the results.

Here is my code.

Garb::Session.login('user@demo.com', '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)

And if I use debug on the view I can see the results but whatever I try I cant access the results.

Here is the debug result

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

ie

I have tried making it to an array as well with no luck.

Have you used this gem before? If so how do I access those results.


The Garb::ResultSet is an enumerator, so you can use any of the enumerator methods on it (each, map, etc.). The individual results are OpenStructs, so you can access them directly.

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


I use this code to pull what i want using 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

To find the cacert.pem file go here http://curl.haxx.se/ca/cacert.pem save that as a file and reference it in the code.

One thing to note is that you will need to request the metrics and dimensions in pascalcase but then put them into your CSV with the underscore case (why, i do not know).

Other than that you're good as gold. Just open the CSV file when you're done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜