Class: GAAPI::Report
- Inherits:
-
Object
- Object
- GAAPI::Report
- Defined in:
- lib/gaapi/report.rb
Overview
A single report from a query to Google Analytics, with convenient methods to access the dimensions and metrics returned.
Instance Attribute Summary collapse
-
#report ⇒ Object
readonly
The report as a Ruby Hash, with String keys.
Instance Method Summary collapse
-
#dimensions ⇒ Object
An array of the dimensions, in the order that they appear in the report.
-
#dimensions? ⇒ Boolean
True if dimensions were returned.
-
#headers ⇒ Object
An array of the dimensions first and then the metrics, in the order that they appear in the report.
-
#initialize(report) ⇒ Report
constructor
Initialize a new Report.
-
#is_data_golden ⇒ Object
Return if the data is golden, meaning it won’t change if the query is re-run at a later time.
-
#metric_type(i) ⇒ Object
The metric type of the i’th metric in the report.
-
#metrics ⇒ Object
An array of the metric names, in the order that they appear in the report.
-
#next_page_token ⇒ Object
Return the nextPageToken, if any, indicating that the query exceeded the maximum number of rows allowed in a single response, and that the client has to ask for the rest of the data.
-
#rows ⇒ Object
The data rows in the report.
-
#totals ⇒ Object
The totals in the report, if there were any.
-
#totals? ⇒ Boolean
True if there totals were returned from the query.
Constructor Details
#initialize(report) ⇒ Report
Initialize a new Report.
30 31 32 |
# File 'lib/gaapi/report.rb', line 30 def initialize(report) @report = report.is_a?(Hash) ? report : JSON.parse(report) end |
Instance Attribute Details
#report ⇒ Object (readonly)
The report as a Ruby Hash, with String keys. It’s typically much more convenient to use the ‘#rows` method, and the methods on `Row` on each instance of a Row.
36 37 38 |
# File 'lib/gaapi/report.rb', line 36 def report @report end |
Instance Method Details
#dimensions ⇒ Object
An array of the dimensions, in the order that they appear in the report.
8 9 10 |
# File 'lib/gaapi/report.rb', line 8 def dimensions report["columnHeader"]["dimensions"] || [] end |
#dimensions? ⇒ Boolean
Returns True if dimensions were returned.
13 14 15 |
# File 'lib/gaapi/report.rb', line 13 def dimensions? !report["columnHeader"]["dimensions"].nil? end |
#headers ⇒ Object
An array of the dimensions first and then the metrics, in the order that they appear in the report.
19 20 21 |
# File 'lib/gaapi/report.rb', line 19 def headers dimensions + metrics end |
#is_data_golden ⇒ Object
Return if the data is golden, meaning it won’t change if the query is re-run at a later time. The is a lag between the end of a date period and when Google Analytics has completely consolidated all the tracking data.
41 42 43 |
# File 'lib/gaapi/report.rb', line 41 def is_data_golden # rubocop:disable Naming/PredicateName report["data"]["isDataGolden"] end |
#metric_type(i) ⇒ Object
The metric type of the i’th metric in the report.
46 47 48 |
# File 'lib/gaapi/report.rb', line 46 def metric_type(i) report["columnHeader"]["metricHeader"]["metricHeaderEntries"][i]["type"] end |
#metrics ⇒ Object
An array of the metric names, in the order that they appear in the report.
51 52 53 |
# File 'lib/gaapi/report.rb', line 51 def metrics report["columnHeader"]["metricHeader"]["metricHeaderEntries"].map { |metric| metric["name"] } end |
#next_page_token ⇒ Object
Return the nextPageToken, if any, indicating that the query exceeded the maximum number of rows allowed in a single response, and that the client has to ask for the rest of the data.
58 59 60 |
# File 'lib/gaapi/report.rb', line 58 def next_page_token report["nextPageToken"] end |
#rows ⇒ Object
The data rows in the report.
63 64 65 |
# File 'lib/gaapi/report.rb', line 63 def rows (report["data"]["rows"] || []).map { |row| Row.new(self, row) } end |
#totals ⇒ Object
The totals in the report, if there were any.
68 69 70 |
# File 'lib/gaapi/report.rb', line 68 def totals Array.new(dimensions.size) + report["data"]["totals"][0]["values"] end |
#totals? ⇒ Boolean
Returns True if there totals were returned from the query.
73 74 75 |
# File 'lib/gaapi/report.rb', line 73 def totals? report["data"]["totals"] end |