使用Legato gem获取查询结果 [英] Getting query results with Legato gem

查看:88
本文介绍了使用Legato gem获取查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图使用Legato gem从Google Analytics获得查询结果(遵循先前的建议,我去那里+进行一些研究)。似乎很容易定义我的查询,但我无法理解如何实际得到结果。阅读自述文件展示了如何构建一些构建块,但是我没有得到一个可以从中读取结果的对象。

可能我错过了某些东西,而查询结果应直接写入模型(DB),这不是我想要的(我想先查看它们)。也许这就是我没有看到任何东西的原因。

所以有人可以分享一个例子如何阅读结果吗?例如,只是将浏览量打印到屏幕上。



谢谢 解决方案

所以这里是我发现使用它的方式:
您可以创建一个定义查询模型的类(和Active-Model返回关系一样,您也可以在此处进行连接)。在这个例子中:




  • 浏览器& ga:pagePathLevel1维度中的独特网页浏览。

  • 有一个可选的过滤器,您可以在pagePathLevel1中查找'index'(正如您所看到的,您可以选择使用它还是不是,添加更多的过滤器,连接它们等等。
  • 同时请注意,过滤器和结果只是返回一个查询 (就像ActiveModel :: Relation一样),执行通过调用它来完成,比如'each'或者'to_a'等。

    code> class Pageviews
    扩展Legato :: Model

    指标:综合浏览量::uniquePageviews
    维度:pagePathLevel1

    过滤器(:by_index_in_path_level_1) {| page_path_level1 | contains(:pagePathLevel1,'index')}

    def self.query(profile,start_date,end_date)
    Pageviews.results(profile,
    :start_date = > start_date,
    :end_date => end_date

    #仅供参考,排序下降g通过浏览量完成::sort => '-pageviews'
    end

    def self.query_index(profile,start_date,end_date)
    Pageviews.by_index_in_path_level_1.results(profile,
    :start_date => start_date ,
    :end_date => end_date

    结束
    结束

    一旦准备就绪,您可以执行如下操作:

      Pageviews.query(profile,start_date,end_date)每个做|结果| 
    #只需打印浏览量&独特的浏览量,例如
    puts result.try(:pageviews)
    puts result.try(:uniquePageviews)
    最终

    最后,我建议您首先使用 Google Analytics(分析)查询浏览器



    我希望你能找到这个例子有用


    Trying to get query results from Google Analytics using Legato gem (following a previous recommendation I go there + some research). Seems to be simple to define my query, however I fail to understand how to actually get the results. Reading the readme shows how to build some "building blocks", but I fail to get an object where I can read the results from.

    Maybe I missed something, and the query results should be written directly to the model (DB), which is not what I want (I wanted to go over them first). Maybe that's the reason I'm not seeing anything.

    So can someone please share an example how to read the results? For example, just print the pageviews to the screen.

    Thanks

    解决方案

    So here's the way I've found to use it: You create a class that defines the "model" of your query (and like Active-Model returning relations, you can also concatenate here). In this example:

    • The model/query check for pageviews & unique-pageviews within the ga:pagePathLevel1 dimension.
    • There is an optional filter you can use, looking for 'index' within the pagePathLevel1 (and as you can see, you can chose to use it or not, add more filters, concatenate them, etc.

    Also note that the filter & result just return a "query" (just like ActiveModel::Relation), where the execution is done by invoking something on it, like 'each' or 'to_a', etc.

    class Pageviews
      extend Legato::Model
    
      metrics :pageviews, :uniquePageviews
      dimensions :pagePathLevel1
    
      filter(:by_index_in_path_level_1) {|page_path_level1| contains(:pagePathLevel1, 'index')}
    
      def self.query(profile, start_date, end_date)
        Pageviews.results(profile,
                          :start_date => start_date,
                          :end_date => end_date
        )
        # Just for reference, sorting descending by pageviews is done by:   :sort => '-pageviews'
      end
    
      def self.query_index(profile, start_date, end_date)
        Pageviews.by_index_in_path_level_1.results(profile,
                                                   :start_date => start_date,
                                                   :end_date => end_date
        )
      end
    end
    

    Once this is ready, you can do something like:

    Pageviews.query(profile, start_date, end_date).each do |result|
        # Just print the pageviews & unique-pageviews, for example
        puts result.try(:pageviews)
        puts result.try(:uniquePageviews)
    end
    

    Lastly, I recommand that you first explore with Google Analytics Query Explorer.

    I hope you can find this example helpful

    这篇关于使用Legato gem获取查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆