0

I have a dashing dashboard set up which I have showing the "ga:activeVisitors' metric. This works fine and I've no problems there. My question is how do I show a different stat that my ga is capturing for example... On one of my GA dashboards I have a widget showing a real time value of another metric. How would I reference that instead of the ga:activeVisitors metric?

my code is as follows:

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
Nexus490
  • 319
  • 1
  • 4
  • 14

2 Answers2

0

To access real time data you need to use the Real-time api.

the real time api has a different set of dimensions and metrics then the core reporting api you will need to use those. which it looks like you are ga:activeVisitors

The Real Time Reporting API is currently available as a developer preview in limited beta. If you're interested in signing up, request access to the beta.

it normally takes 24 hours to get access you will not hear anything from google you should just try and make a request.

I am not a ruby programmer but your code: analytics.data.realtime.get looks correct to me. Are you getting any errors?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • No I'm not getting any errors my problem is simply grabbing the correct stat I want from one of my dashboards. The API seems to be great for getting at default stats, but not so for custom ones that have been built. – Nexus490 Dec 03 '15 at 13:50
  • Your not going to get anything custom off the website. Your going to have to build it all manually. – Linda Lawton - DaImTo Dec 03 '15 at 14:46
-2

On the following website you will find GA Queries Examples:

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

Maybe it helps you?

maazza
  • 7,016
  • 15
  • 63
  • 96
  • And with the Query-Explorer you can test your queries before you use them in the dashing Google Analytics Widget: https://ga-dev-tools.appspot.com/query-explorer/ – user6010089 Mar 02 '16 at 21:19
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Bhargav Rao Mar 02 '16 at 21:26