首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby实时google分析API

Ruby实时google分析API
EN

Stack Overflow用户
提问于 2013-10-04 19:51:03
回答 2查看 2.3K关注 0票数 3

我正试图用google ruby客户端获取activeVisitors。客户端列在实时google文档这里中,但我在文档中没有看到将其用于实时api的任何内容。

我看到函数discovered_api,但是我没有看到API名称的位置参数列表。

普通分析器API示例:

代码语言:javascript
复制
# Get the analytics API
analytics = client.discovered_api('analytics','v3')

有谁知道如何使用这个客户来获得实时活跃的访问者吗?

下面是我试图使用的代码:

代码语言:javascript
复制
require 'google/api_client'
require 'date'

# Update these to match your own apps credentials
service_account_email = 'xxxxxxxxxxxxx@developer.gserviceaccount.com' # Email of service account
key_file = '/path/to/key/privatekey.p12' # File containing your private key
key_secret = 'notasecret' # Password to unlock private key
profileID = '111111111' # Analytics profile ID.

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

# 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 '1m', :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
visitCount = client.execute(:api_method => analytics.data.ga.get, :parameters => {
'ids' => "ga:" + profileID,
'metrics' => "ga:activeVisitors",
})

# Update the dashboard
send_event('current_visitors', { current: visitCount.data.rows[0][0] })
end

返回的错误:

代码语言:javascript
复制
Missing required parameters: end-date, start-date.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-08 06:06:01

假设ruby客户端库使用发现服务,并且该方法实际上是可用的,而不是:

代码语言:javascript
复制
visitCount = client.execute(:api_method => analytics.data.ga.get, :parameters => {
'ids' => "ga:" + profileID,
'metrics' => "ga:activeVisitors",

尝试这样做(在api_method中将ga更改为实时):

代码语言:javascript
复制
visitCount = client.execute(:api_method => analytics.data.realtime.get, :parameters => {
'ids' => "ga:" + profileID,
'metrics' => "ga:activeVisitors",
票数 5
EN

Stack Overflow用户

发布于 2013-10-05 04:43:35

如果您是实时报告产品论坛的成员,这篇文章可能会有帮助- https://groups.google.com/forum/m/#!topic/google-analytics-realtime-api/zgAsKFBenV8

你可以试试..。

分析=client.discovered_api(“实时”,“v3”)

或者是实时的,或者w/o v3。

如果这样做有效,那么也要更新get方法。

希望我能得到更多的帮助,但绝对没有这方面的文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19189610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档