Skip to content

Query Parameters

Tony Pitale edited this page Sep 29, 2013 · 7 revisions

Available Query Parameters

  • :start_date - The date of the period you would like this report to start
  • :end_date - The date to end, inclusive
  • :limit - The maximum number of results to be returned
  • :offset - The starting index
  • :sort - metric/dimension to order by

Using #results for parameters

results = Exit.results(profile, :start_date => 10.days.ago, :end_date => 1.day.ago) # using ActiveSupport

The results collection (actually a Query, for chaining) includes Enumerable

Sorting

# Sorts results in reverse-chronological order.
# Assumes our Exit class has:
#     dimensions :date, :hour
Exit.results(profile, :sort => ['-date', '-hour'])

Working with Collections, and Limiting API Requests

Given the following dimension-less class:

class Visitor
  extend Legato::Model
  metrics :visitors, :percent_new_visits
end

Then the results will contain a single aggregate value representing all days within the specified time range:

results = Visitor.results(profile, :start_date => 8.days.ago, :end_date => 1.day.ago)
results.count
=> 1
results.first
=> #<OpenStruct visitors="100", percentNewVisits="XX.XX">

But, given the following class with date dimension:

class Visitor
  extend Legato::Model
  metrics :visitors, :percent_new_visits
  dimensions :date
end

Then the results collection will contain one item per each day in the specified range:

results = Visitor.results(profile, :start_date => 8.days.ago, :end_date => 1.day.ago)
results.count
=> 8
results.first
=> #<OpenStruct date="YYYYMMDD", visitors="12", percentNewVisits="XX.XX">

And you can loop through the results without making any additional API requests.