在虚拟仪表板上引用自定义Google Analytics(分析)指标

时间:2015-12-03 13:22:42

标签: ruby google-analytics google-analytics-api dashing

我设置了一个仪表板,我已经显示了" ga:activeVisitors'度量。这很好,我没有问题。我的问题是如何显示我的ga正在捕获的不同统计数据......在我的一个GA仪表板上,我有一个小部件显示另一个指标的实时值。我如何引用而不是ga:activeVisitors指标?

我的代码如下:

require 'google/api_client'
require 'date'

# Update these to match your own apps credentials
service_account_email = '[email]' # Email of service account key_file = '[key file path]' # File containing your private key key_secret = 'secret' # Password to unlock private key profile_id = 'profile' # Analytics profile ID.

# Get the Google API client
client = Google::APIClient.new(
:application_name => '[app name]',
:application_version => '0.01'
)

visitors = []

# Load your credentials for the service account
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri =>  'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/analytics.readonly',
:issuer => service_account_email,
:signing_key => key)

# Start the scheduler
SCHEDULER.every '1s', :first_in => 0 do

# Request a token for our service account
client.authorization.fetch_access_token!

# Get the analytics API
analytics = client.discovered_api('analytics','v3')

# Execute the query
response = client.execute(:api_method => analytics.data.realtime.get,   :parameters => {
'ids' => "ga:" + profile_id,
'metrics' => "ga:activeVisitors",
})

visitors << { x: Time.now.to_i, y: response.data.rows }

# Update the dashboard
send_event('visitor_count_real_time', points: visitors)
end

2 个答案:

答案 0 :(得分:0)

要访问实时数据,您需要使用Real-time api

实时api有一组不同的dimensions and metrics然后核心报告api你需要使用它们。它看起来像你ga:activeVisitors

  

Real Time Reporting API目前作为开发人员提供   在限量版中预览。如果您有兴趣注册,request access到测试版。

通常需要24小时才能获得访问权限,您将无法听到Google提供的任何内容,您应该尝试提出请求。

我不是红宝石程序员,但你的代码:analytics.data.realtime.get对我来说是正确的。你有任何错误吗?

答案 1 :(得分:-2)

在以下网站上,您可以找到GA查询示例:

https://developers.google.com/analytics/devguides/reporting/core/v3/common-queries

也许对你有所帮助?