Sha256: 346dfe6d93dc2f221f3ccb515c7d3aaceeb2c8002c55219493398dd3d32a528b

Contents?: true

Size: 1.21 KB

Versions: 7

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'json'
require 'date'

module GoogleAnalyticsV4Api
  class ReportResponseRow

    attr_accessor :data

    def initialize(row, raw_dimension_headers, raw_metric_headers)
      # "dimensionHeaders"=>[{"name"=>"pagePath"}, {"name"=>"countryId"}],
      # "metricHeaders"=>[{"name"=>"sessions", "type"=>"TYPE_INTEGER"}, {"name"=>"screenPageViews", "type"=>"TYPE_INTEGER"}],
      # a row: {"dimensionValues"=>[{"value"=>"/events"}, {"value"=>"CL"}], "metricValues"=>[{"value"=>"62"}, {"value"=>"82"}]}
      @raw_row = row
      @data = {}
      raw_dimension_headers.each_with_index do |dimension_header, index|
        @data[dimension_header["name"]] = row["dimensionValues"][index]["value"]
      end
      raw_metric_headers.each_with_index do |metric_header, index|
        @data[metric_header["name"]] = cast(row["metricValues"][index]["value"], metric_header["type"])
      end
    end

    def cast(value, type)
      words = type.split("_")
      return value unless words[0] == 'TYPE'
      return value.to_i if words[1] == "INTEGER"
      return value.to_f if words[1].in? %w(FLOAT SECONDS MILLISECONDS MINUTES HOURS STANDARD CURRENCY FEET MILES METERS KILOMETERS)

      value
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
google_analytics_v4_api-0.0.9 lib/google_analytics_v4_api/report_response_row.rb
google_analytics_v4_api-0.0.8 lib/google_analytics_v4_api/report_response_row.rb
google_analytics_v4_api-0.0.7 lib/google_analytics_v4_api/report_response_row.rb
google_analytics_v4_api-0.0.6 lib/google_analytics_v4_api/report_response_row.rb
google_analytics_v4_api-0.0.5 lib/google_analytics_v4_api/report_response_row.rb
google_analytics_v4_api-0.0.4 lib/google_analytics_v4_api/report_response_row.rb
google_analytics_v4_api-0.0.3 lib/google_analytics_v4_api/report_response_row.rb