There are many (old) clients available:
- https://github.com/activenetwork/gattica
- https://github.com/tpitale/legato
- https://github.com/vigetlabs/garb
All are abandoned and you should not use them. The Google Analytics API is at v3 and they are all using an earlier version.
Use https://github.com/google/google-api-ruby-client (Google supported).
For server-to-server Analytics API:
- Go to your project at https://code.google.com/apis/console/
- Enable Analytics under Services
- 'Create another client ID' under API Access
- Download the key-file to your project [KEY_FILE]
- Go to you Analytics account and add the API client email address to your account [SERVICE_ACCOUNT_EMAIL]. It should be something like '[email protected]'.
Now get access to the Analytics API in your Ruby/Rails app:
require 'google/api_client'
require 'date'
SERVICE_ACCOUNT_EMAIL = '[email protected]'
client = Google::APIClient.new(:application_name => 'something you like', :application_version => '1')
key_file = File.join('SOME PATH', 'KEY_FILE')
key = Google::APIClient::PKCS12.load_key(key_file, 'notasecret')
service_account = Google::APIClient::JWTAsserter.new(
SERVICE_ACCOUNT_EMAIL,
['https://www.googleapis.com/auth/analytics.readonly', 'https://www.googleapis.com/auth/prediction'],
key)
client.authorization = service_account.authorize
analytics = client.discovered_api('analytics', 'v3')
parameters = {
'accountId': '~all',
'webPropertyId': '~all'
}
result = client.execute(:api_method => analytics.management.profiles.list, parameters: parameters)
profiles = result.data.items
parameters = {
'ids' => "ga:#{profiles.first.id}",
'start-date' => (Date.today - 30).strftime("%Y-%m-%d"),
'end-date' => Date.today.strftime("%Y-%m-%d"),
'metrics' => "ga:avgTimeOnPage",
'filters' => "ga:pagePath=~/"
}
result = client.execute(:api_method => analytics.data.ga.get, :parameters => parameters)
how do I find an accounts web properties?