I implemented a following rake task:
namespace :ga do
task :test do
require 'google/api_client'
client = Google::APIClient.new(
application_name: 'Sample Google Analytics',
application_version: '1.0.0'
)
key = Google::APIClient::KeyUtils.load_from_pkcs12(
'path/to/secret.p12',
'notasecret'
)
analytics = client.discovered_api('analytics', 'v3')
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: '[email protected]',
signing_key: key
)
client.authorization.fetch_access_token!
result = client.execute(
api_method: analytics.data.ga.get,
parameters:
{
ids: 'ga:[8 digit property number]',
'start-date' => '2014-07-01',
'end-date' => '2014-07-30',
metrics: 'ga:visitors,ga:visits,ga:pageviews',
dimensions: 'ga:pagePath,ga:pageTitle',
sort: '-ga:pageviews'
}
)
body = JSON.parse(result.response.body)
puts body
end
end
And exec:
$ bundle exec rake ga:test
{"error"=>{"errors"=>[{"domain"=>"global", "reason"=>"insufficientPermissions", "message"=>"User does not have sufficient permissions for this profile."}], "code"=>403, "message"=>"User does not have sufficient permissions for this profile."}}
Why did the results say "User does not have sufficient permissions for this profile." ?
ah.. I misused id. sorry.
ids: 'ga:[8 digit property number]',
I used ids as XXXX of UA-XXXX-Y. Now I use View ID (Admin > VIEW > View Settings) and it works.
I had the same issue, so right [8 digit number] you can take from url, visiting your site analitic main page, there will be 8 digests after "p" letter, i hightlight them

Thus it will be looked as ids: 'ga:92936084'
Wish it helps guys come from Search Engins :+1:
I am facing the same problem. But I don't understand what you guys are talking about
Most helpful comment
ah.. I misused id. sorry.
I used ids as XXXX of UA-XXXX-Y. Now I use View ID (Admin > VIEW > View Settings) and it works.