Getting query results with Legato gem

拥有回忆 提交于 2019-12-03 16:24:47

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!