From 123c69efbe7b76168c74ec717eeba908b885a9ba Mon Sep 17 00:00:00 2001 From: Shinohara Teruki Date: Sun, 26 Mar 2017 20:33:18 +0900 Subject: [PATCH] add realtime analytics sample --- samples/cli/lib/samples/analytics.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/samples/cli/lib/samples/analytics.rb b/samples/cli/lib/samples/analytics.rb index 8d73779ce..eb98e2d39 100644 --- a/samples/cli/lib/samples/analytics.rb +++ b/samples/cli/lib/samples/analytics.rb @@ -58,5 +58,23 @@ module Samples print_table(data) end + desc 'show_realtime_visits PROFILE_ID', 'Show realtime visists for the given analytics profile ID' + def show_realtime_visits(profile_id) + analytics = Analytics::AnalyticsService.new + analytics.authorization = user_credentials_for(Analytics::AUTH_ANALYTICS) + + dimensions = %w(rt:medium rt:pagePath) + metrics = %w(rt:activeUsers) + sort = %w(rt:medium rt:pagePath) + result = analytics.get_realtime_data("ga:#{profile_id}", + metrics.join(','), + dimensions: dimensions.join(','), + sort: sort.join(',')) + + data = [] + data.push(result.column_headers.map { |h| h.name }) + data.push(*result.rows) + print_table(data) + end end end