Class: GAAPI::Report

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(report) ⇒ Report

Initialize a new Report.

Parameters:

  • report (JSON source, Hash)

    If a Hash, assume it’s a valid report from a Google Analytics query, and use it as the report. Otherwise, attempt to parse it with ‘JSON.parse`. No checking is done on the input to ensure that it’s a valid response from a Google Analytics query, so subsequent calls to the methods on the object returned from ‘Report.new` may fail in spectacular ways.



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

#reportObject (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

#dimensionsObject

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.

Returns:

  • (Boolean)

    True if dimensions were returned.



13
14
15
# File 'lib/gaapi/report.rb', line 13

def dimensions?
  !report["columnHeader"]["dimensions"].nil?
end

#headersObject

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_goldenObject

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

#metricsObject

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_tokenObject

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

#rowsObject

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

#totalsObject

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.

Returns:

  • (Boolean)

    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