16

Is Google API Ruby client the best option?

I have a site example.com with users and I want them to see their google analytics stats on example.com, how can I do it ?

I can see the example but I'm not able to figure out how to begin.

Jonathan Soifer
  • 2,387
  • 5
  • 23
  • 47
iCyborg
  • 4,422
  • 14
  • 48
  • 79
  • 1
    Legato has _not ever_ been abandoned, and is a better way to build maintainable queries against the GA API. I've asked the author of that Gist to correct his erroneous notes. Legato has _always_ supported GA API Version 3. https://github.com/tpitale/legato/commit/0def82f9bdb9cf259d4d91d5bd2f17759231bb29 – Tony Pitale Jan 15 '14 at 18:32

1 Answers1

30

I also use the google-api-ruby-client gem and set it up about the same way that is outlined in the link you provided (https://gist.github.com/joost/5344705).

Just follow the steps outlined in the link to set up a Google Analytics client:

# you need to set this according to your situation/needs
SERVICE_ACCOUNT_EMAIL_ADDRESS = '...' # looks like 12345@developer.gserviceaccount.com
PATH_TO_KEY_FILE              = '...' # the path to the downloaded .p12 key file
PROFILE                       = '...' # your GA profile id, looks like 'ga:12345'


require 'google/api_client'

# set up a client instance
client  = Google::APIClient.new

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_ADDRESS,
  :signing_key          => Google::APIClient::PKCS12.load_key(PATH_TO_KEY_FILE, 'notasecret')
).tap { |auth| auth.fetch_access_token! }

api_method = client.discovered_api('analytics','v3').data.ga.get


# make queries
result = client.execute(:api_method => api_method, :parameters => {
  'ids'        => PROFILE,
  'start-date' => Date.new(1970,1,1).to_s,
  'end-date'   => Date.today.to_s,
  'dimensions' => 'ga:pagePath',
  'metrics'    => 'ga:pageviews',
  'filters'    => 'ga:pagePath==/url/to/user'
})

puts result.data.rows.inspect

To display statistics for a user's page in your app, you have to adjust the metrics and filters parameters when making the query. The query above for example will return a result object containing all pageviews for the page with url example.com/url/to/user.


Caveat: this answer was written a long time ago and Google released a new, incompatible version of the gem. Please consult https://github.com/google/google-api-ruby-client/blob/master/MIGRATING.md

severin
  • 9,689
  • 1
  • 36
  • 39
  • Saved my day.. Thanks @severin. In the make query section, PROILE should be spelled as PROFILE. Not a big deal though. – Kumar Jul 29 '14 at 23:32
  • @severin what is "PROFILE = '...' # your GA profile id, looks like 'ga:12345' "? from where did you took that id? is that analytics account id.? if so do we need to prefix with it 'ga:'.? – Aparichith Nov 12 '14 at 06:54
  • @severin when i ran "api_method = client.discovered_api('analytics','v3').data.ga.get" its leading to unknown keyword: interval from /home/brahmos/.rvm/gems/ruby-2.0.0-p576/gems/google-api-client-0.7.1/lib/google/api_client.rb:595:in `execute!' – Aparichith Nov 12 '14 at 07:42
  • @h.APP.y you find the profile id by navigating to "Admin", then go to "View settings" for your view; there you should see a "View ID": this is the profile id (Google renamed this from profile id to view id), e.g. "12345". You then have to set PROFILE to "ga:12345". – severin Nov 12 '14 at 16:26
  • @severin got it. Thank you. i was tiered by searching. – Aparichith Nov 13 '14 at 04:33
  • It returns an empty array. Can you help me. – Gowtham Gopalakrishnan Jun 27 '15 at 07:26
  • I solved it. I used Account ID instead of ID in the View settings. – Gowtham Gopalakrishnan Jun 27 '15 at 07:35
  • LoadError in DashboardController#show cannot load such file -- google/api_client What am i doing wrong? – Bengala Sep 27 '16 at 18:48
  • @Bengala my answer works with the google-api-client gem versions up to version 0.8. If you installed version 0.9 of the gem then you have to adapt it. Consult https://github.com/google/google-api-ruby-client/blob/master/MIGRATING.md – severin Sep 28 '16 at 09:47