class GoogleAnalyticsFeeds::DataFeed

@api public

Constants

BASE_URI

Attributes

params[R]

Public Class Methods

new() click to toggle source
# File lib/google_analytics_feeds.rb, line 245
def initialize
  @params = {}
end

Public Instance Methods

clone() click to toggle source

@api private

Calls superclass method
# File lib/google_analytics_feeds.rb, line 374
def clone
  obj = super
  obj.instance_variable_set(:@params, @params.clone)
  obj
end
dates(start_date, end_date) click to toggle source

Sets the start and end date for retrieved results @return [GoogleAnalyticsFeeds::DataFeed] a cloned DataFeed.

# File lib/google_analytics_feeds.rb, line 287
def dates(start_date, end_date)
  clone_and_set {|params|
    params['start-date'] = start_date.strftime("%Y-%m-%d")
    params['end-date'] = end_date.strftime("%Y-%m-%d")
  }
end
dimensions(*names) click to toggle source

Sets the dimensions for a query.

A query doesn’t have to have any dimensions; Google Analytics limits you to 7 dimensions per-query at time of writing.

@param names [*Symbol] the ruby-style names of the dimensions. @return [GoogleAnalyticsFeeds::DataFeed] a cloned DataFeed.

# File lib/google_analytics_feeds.rb, line 279
def dimensions(*names)
  clone_and_set {|params|
    params['dimensions'] = names.map {|v| symbol_to_name(v) }.join(',')
  }
end
filters(&block) click to toggle source

Filter the result set, based on the results of a block.

All the block methods follow the form operator(name, value). Supported operators include: eql, not_eql, lt, lte, gt, gte, contains, not_contains, match and not_match - hopefully all self-explainatory.

Example:

query.
  filter {
    eql(:dimension, "value")
    gte(:metric, 3)
  }

@return [GoogleAnalyticsFeeds::DataFeed]

# File lib/google_analytics_feeds.rb, line 328
def filters(&block)
  clone_and_set {|params|
    params['filters'] = FilterBuilder.new.build(&block)
  }
end
max_results(i) click to toggle source

Sets the maximum number of results retrieved.

Google Analytics has its own maximum as well. @return [GoogleAnalyticsFeeds::DataFeed]

# File lib/google_analytics_feeds.rb, line 306
def max_results(i)
  clone_and_set {|params|
    params['max-results'] = i.to_s
  }
end
metrics(*vals) click to toggle source

Sets the metrics for a query.

A query must have at least 1 metric for GA to consider it valid. GA also imposes a maximum (as of writing 10 metrics) per query.

@param names [*Symbol] the ruby-style names of the dimensions. @return [GoogleAnalyticsFeeds::DataFeed] a cloned DataFeed.

# File lib/google_analytics_feeds.rb, line 266
def metrics(*vals)
  clone_and_set {|params|
    params['metrics'] = vals.map {|v| symbol_to_name(v) }.join(',')
  }
end
profile(id) click to toggle source

Sets the profile id from which this report should be based.

@return [GoogleAnalyticsFeeds::DataFeed] a cloned DataFeed.

# File lib/google_analytics_feeds.rb, line 252
def profile(id)
  clone_and_set {|params|
    params['ids'] = symbol_to_name(id)
  }
end
retrieve(session_token, connection) click to toggle source

@api private

# File lib/google_analytics_feeds.rb, line 366
def retrieve(session_token, connection)
  connection.get(uri) do |request|
    request.headers['Authorization'] = 
      "GoogleLogin auth=#{session_token}"
  end
end
segment(&block) click to toggle source

Use a dynamic advanced segment.

Block methods follow the same style as for filters. Named advanced segments are not yet supported.

@return [GoogleAnalyticsFeeds::DataFeed]

# File lib/google_analytics_feeds.rb, line 340
def segment(&block)
  clone_and_set {|params|
    params['segment'] = "dynamic::" + FilterBuilder.new.build(&block)
  }
end
sort(column, direction) click to toggle source

Sorts the result set by a column.

Direction can be :asc or :desc.

# File lib/google_analytics_feeds.rb, line 349
def sort(column, direction)
  clone_and_set {|params|
    c = symbol_to_name(column)
    params['sort'] = (direction == :desc ? "-#{c}" : c)
  }
end
start_index(i) click to toggle source

Sets the start index for retrieved results @return [GoogleAnalyticsFeeds::DataFeed]

# File lib/google_analytics_feeds.rb, line 296
def start_index(i)
  clone_and_set {|params|
    params['start-index'] = i.to_s
  }
end
to_s()
Alias for: uri
uri() click to toggle source

Returns the URI string needed to retrieve this report.

# File lib/google_analytics_feeds.rb, line 357
def uri
  uri = Addressable::URI.parse(BASE_URI)
  uri.query_values = @params
  uri.to_s.gsub("%40", "@")
end
Also aliased as: to_s

Private Instance Methods

clone_and_set() { |params| ... } click to toggle source
# File lib/google_analytics_feeds.rb, line 386
def clone_and_set
  obj = clone
  yield obj.params
  obj
end