Ruby real time google analytics API

…衆ロ難τιáo~ 提交于 2019-12-12 14:27:28

问题


I am trying to get activeVisitors with the google-api-ruby-client. The client is listed in the real time google analytics API docs here however I see nothing in the docs about using it for real time api.

I see the function discovered_api however I see no list for posisble parameters for the API name.

Example for Regular Analytics API:

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

Does anyone know how to use this client to get real time active visitors?

Here is the code I am trying to use:

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

Error returned:

Missing required parameters: end-date, start-date.

回答1:


Assuming that the ruby client lib uses the discovery service and the method is actually available, instead of:

visitCount = client.execute(:api_method => analytics.data.ga.get, :parameters => {
'ids' => "ga:" + profileID,
'metrics' => "ga:activeVisitors",

try this (change ga to realtime in the api_method):

visitCount = client.execute(:api_method => analytics.data.realtime.get, :parameters => {
'ids' => "ga:" + profileID,
'metrics' => "ga:activeVisitors",



回答2:


If you're a member of the Real-time reporting product forum, this post may be helpful - https://groups.google.com/forum/m/#!topic/google-analytics-realtime-api/zgAsKFBenV8

You might try...

analytics = client.discovered_api('realtime','v3')

Or real-time, or w/o v3.

If that works update your get method too.

Wish I could be more help but there is absolutely no documentation on this.



来源:https://stackoverflow.com/questions/19189610/ruby-real-time-google-analytics-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!