Class: GoogleAnalyticsFeeds::DataFeed

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

Constant Summary collapse

BASE_URI =
"https://www.googleapis.com/analytics/v2.4/data"

Instance Method Summary collapse

Constructor Details

#initializeDataFeed

Returns a new instance of DataFeed.



245
246
247
# File 'lib/google_analytics_feeds.rb', line 245

def initialize
  @params = {}
end

Instance Method Details

#dates(start_date, end_date) ⇒ GoogleAnalyticsFeeds::DataFeed

Sets the start and end date for retrieved results

Returns:



287
288
289
290
291
292
# 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) ⇒ GoogleAnalyticsFeeds::DataFeed

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.

Parameters:

  • names (*Symbol)

    the ruby-style names of the dimensions.

Returns:



279
280
281
282
283
# 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) ⇒ GoogleAnalyticsFeeds::DataFeed

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)
  }


328
329
330
331
332
# 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) ⇒ GoogleAnalyticsFeeds::DataFeed

Sets the maximum number of results retrieved.

Google Analytics has its own maximum as well.



306
307
308
309
310
# 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) ⇒ GoogleAnalyticsFeeds::DataFeed

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.

Parameters:

  • names (*Symbol)

    the ruby-style names of the dimensions.

Returns:



266
267
268
269
270
# 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) ⇒ GoogleAnalyticsFeeds::DataFeed

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

Returns:



252
253
254
255
256
# File 'lib/google_analytics_feeds.rb', line 252

def profile(id)
  clone_and_set {|params|
    params['ids'] = symbol_to_name(id)
  }
end

#segment(&block) ⇒ GoogleAnalyticsFeeds::DataFeed

Use a dynamic advanced segment.

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



340
341
342
343
344
# 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) ⇒ Object

Sorts the result set by a column.

Direction can be :asc or :desc.



349
350
351
352
353
354
# 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) ⇒ GoogleAnalyticsFeeds::DataFeed

Sets the start index for retrieved results



296
297
298
299
300
# File 'lib/google_analytics_feeds.rb', line 296

def start_index(i)
  clone_and_set {|params|
    params['start-index'] = i.to_s
  }
end

#uriObject Also known as: to_s

Returns the URI string needed to retrieve this report.



357
358
359
360
361
# 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