Class: GAAPI::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/gaapi/row.rb

Overview

A single row from a report from a query to Google Analytics.

In addition to the methods listed, ‘Row` provides methods to access the value of a dimension or metric by the name of the dimension or metric. The name is the snake case version of the Google Analytics dimension or metric name, minus the `ga:` prefix. For example, you can access the metric `ga:sessionDuration` by writing `row.session_duration`.

In the case of the metrics, the value returned is the appropriate Ruby type for the metric. For example, an INTEGER metric is returned as a Ruby integer.

Instance Method Summary collapse

Constructor Details

#initialize(report, row) ⇒ Row

Returns a new instance of Row.



22
23
24
25
# File 'lib/gaapi/row.rb', line 22

def initialize(report, row)
  @report = report
  @row = row
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Define and call methods to return the value of the dimensions and metrics in the report. The name is the snake case version of the Google Analytics dimension or metric name, minus the ‘ga:` prefix. For example, you can access the metric `ga:sessionDuration` by writing `row.session_duration`.

In the case of the metrics, the value returned is the appropriate Ruby type for the metric. For example, an INTEGER metric is returned as a Ruby integer.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gaapi/row.rb', line 37

def method_missing(method, *args)
  if (i = dimension_method_names.find_index(method))
    define_singleton_method(method) do
      dimensions[i]
    end
    send(method)
  elsif (i = metric_method_names.find_index(method))
    define_singleton_method(method) do
      convert_metric(i)
    end
    send(method)
  else
    super
  end
end

Instance Method Details

#dimensionsObject

An array of the dimension values, in the order that they appear in the dimension headers.



18
19
20
# File 'lib/gaapi/row.rb', line 18

def dimensions
  row["dimensions"] || []
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.



30
31
32
# File 'lib/gaapi/row.rb', line 30

def is_data_golden
  report.is_data_golden
end

#metricsObject

An array of the metric values, in the order that they appear in the metric headers. These are the raw values as returned by the Google Analytics query, in other words, they’re strings.



56
57
58
59
60
61
# File 'lib/gaapi/row.rb', line 56

def metrics
  # NOTE: There is one entry in the `row["metrics"]` array for each date range.
  # Since currently we only support one date range, this following index is
  # always 0.
  row["metrics"][0]["values"]
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.



66
67
68
69
# File 'lib/gaapi/row.rb', line 66

def next_page_token
  # puts "Next Page Token report: #{report.report}"
  report.next_page_token
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/gaapi/row.rb', line 71

def respond_to_missing?
  true
end

#to_aObject

Return the data from the row as an Array, ordered by:

  • Headers first, in the order that they appear in the Report#headers array.

  • Metrics next, in the order that they appear in the Report#metrics array.



78
79
80
# File 'lib/gaapi/row.rb', line 78

def to_a
  dimensions + metrics
end