Retrieving Query Results with Legato Pearl

Trying to get query results from Google Analytics using Legato gem (following the previous recommendation, I will go there + some research). It seems to just define my query, however I don't understand how to get the results. Reading readme shows how to create some “building blocks”, but I can’t get an object where I can read the results.

Perhaps 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 through them first). Perhaps the reason is that I do not see anything.

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

thanks

+4
source share
1 answer

So, here is how I found to use it: You create a class that defines the “model” of your request (and, like the return relationship to the active model, you can also concatenate here). In this example:

  • Model / query validation for pageviews and unique pageviews in the ga: pagePathLevel1 dimension.
  • There is an additional filter that you can use, looking for an “index” within pagePathLevel1 (and, as you can see, you can use it or not, add additional filters, combine them, etc.

Also note that the filter and the result simply return a “query” (like ActiveModel :: Relation), where execution is performed by calling something on it, for example, “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 

Finally, I recommend that you first explore the Google Analytics Query Explorer .

Hope this example is useful

+4
source

Source: https://habr.com/ru/post/1485953/


All Articles