Skip to content

Instantly share code, notes, and snippets.

@s2t2
Last active December 30, 2015 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s2t2/35994d36dbc71d2cb028 to your computer and use it in GitHub Desktop.
Save s2t2/35994d36dbc71d2cb028 to your computer and use it in GitHub Desktop.
Google Analytics Reporting API Requests via Legato Ruby Gem

Objective 1: Make a single request that returns an array of results, each representing a unique set of daily values.

class UserMeasurebyDay
  extend Legato::Model
  metrics :users, :new_users, :percent_new_sessions
  dimensions :date
end
daily_results = UserMeasurebyDay.results(profile, {:start_date => MIN_DATE, :end_date => MAX_DATE})
puts daily_results.collection
#=> [#<OpenStruct date="20100601", users="X", newUsers="Y", percentNewSessions="A.B">, #<OpenStruct ...>, #<OpenStruct ...>, ... ]
# returns a collection of results, row per unique day "20100601","20100602","20100603", etc...
# objective 1 accomplished

Objective 2: Make a single request that returns an array of results, each representing a unique set of monthly values.

class UserMeasurebyMonth
  extend Legato::Model
  metrics :users, :new_users, :percent_new_sessions
  dimensions :month # what should this really be though??
end
monthly_results = UserMeasurebyMonth.results(profile, {:start_date => MIN_DATE, :end_date => MAX_DATE})
puts monthly_results.collection
#> [#<OpenStruct month="01", users="X", newUsers="Y", percentNewSessions="A.B">, #<OpenStruct ...>, #<OpenStruct ...>, ... ]
# returns a collection of results, row per unique day like ["01","02","03", ... "12"]

How should the dimensions for the UserMeasurebyMonth class be configured in order to acheive objective 2 (row per unique month like ["201001","201002","201003", ... "201312"])?

@s2t2
Copy link
Author

s2t2 commented Oct 11, 2014

Yes. Thank you. I can't believe I missed this in the docs. For anyone interested, I'd recommend using isoYearIsoWeek https://developers.google.com/analytics/devguides/reporting/core/dimsmets#view=detail&group=time&jump=ga_isoyearisoweek

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment