访问 Garb Gem 结果

发布于 2024-11-07 17:40:40 字数 969 浏览 0 评论 0原文

我正在使用 ruby​​ 的 Garb gem,但我无法访问结果。

这是我的代码。

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

我也尝试将其放入数组,但没有成功。

您以前使用过这个宝石吗?如果是这样,我如何访问这些结果。

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

Here is my code.

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)

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2024-11-14 17:40:40

Garb::ResultSet 是一个枚举器,因此您可以在其上使用任何枚举器方法(eachmap 等)。各个结果是 OpenStruct,因此您可以直接访问它们。

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

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
夜清冷一曲。 2024-11-14 17:40:40

我使用这段代码来使用 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。质子交换膜
将其保存为文件并在代码中引用它。

需要注意的一件事是,您需要在 pascalcase 中请求指标和维度,然后使用下划线将它们放入 CSV 中(为什么,我不知道)。

除此之外,你就像金子一样。完成后只需打开 CSV 文件即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文