class GoogleAnalyticsFeeds::DataFeed
@api public
Constants
- BASE_URI
Attributes
Public Class Methods
# File lib/google_analytics_feeds.rb, line 245 def initialize @params = {} end
Public Instance Methods
@api private
# File lib/google_analytics_feeds.rb, line 374 def clone obj = super obj.instance_variable_set(:@params, @params.clone) obj end
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
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
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
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
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
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
@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
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
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
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
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
Private Instance Methods
# File lib/google_analytics_feeds.rb, line 386 def clone_and_set obj = clone yield obj.params obj end