Search code examples
ruby-on-rails-4servicegoogle-analytics-api

Google Analytics API and Rails


I'm using google-api-client-0.8.2 and Rails 4.2.3. The service code I'm using is the one at http://www.slatestudio.com/blog/p/how-to-pull-google-analytics-data-from-rails-app. The env variables are all setted and I'm getting the environment variables from

# these from https://www.google.com/analytics/
GA_VIEW_ID: Admin > Select Site > View Tab > View Settings 
GA_APP_NAME: Home > name of folder with code in parenthesis

# these from https://console.developers.google.com/
GA_SERVICE_ACCOUNT_EMAIL: Project > API > Credentials > [email protected]
GA_KEY_FILE_NAME: Same path above > P12 Key

I've also changed

service_account = Google::APIClient::JWTAsserter.new(
  ENV['GA_SERVICE_ACCOUNT_EMAIL'], key)

to

service_account = Google::APIClient::JWTAsserter.new(
  ENV['GA_SERVICE_ACCOUNT_EMAIL'],'https://www.googleapis.com/auth/prediction', key)

Due ArgumentError: wrong number of arguments (2 for 3..4) on jwt_asserter.rb:66:in 'initialize'.

Every time I try to query using #visitors method I receive Insuficient Permission. What am I doing wrong?

EDIT:

I've changed

'https://www.googleapis.com/auth/prediction' -> https://www.googleapis.com/auth/analytics.readonly

And the message changed

User does not have any Google Analytics account


Solution

  • I just needed to give permission to the [email protected]... on the

    Manage > User Management tab