32

I'm trying to fetch data with the Google Analytics API through service accounts.

I have been searching on stackoverflow how to do this and been using the exact same code found on several posts but getting problems to get it working.

Sources:

Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?

Service Applications and Google Analytics API V3: Error 101 (net::ERR_CONNECTION_RESET)

http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php

This is what i got:

require_once('googleAPIGoogle_Client.php');
require_once('googleAPI/contrib/Google_AnalyticsService.php');

const CLIENT_ID = 'xxxxxxxx001.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'xxxxxx001@developer.gserviceaccount.com';


$keyfile = $_SERVER['DOCUMENT_ROOT']."/xxxxxxx284-privatekey.p12";


$client = new Google_Client();
$client->setAccessType('offline');
$client->setApplicationName("cc insights");


$key = file_get_contents($keyfile);
$client->setClientId(CLIENT_ID);

$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/analytics.readonly'),
    $key)
);


$service = new Google_AnalyticsService($client);

$data = $service->data_ga->get("ga:xxxx7777", "2012-01-01", "2013-01-25", "ga:pageviews");
var_dump($data);

when executing i got the following error:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3Axxxx7777&start-date=2012-01-01&end-date=2013-01-25&metrics=ga%3Apageviews: (403) User does not have sufficient permissions for this profile.' in /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php:66 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/service/Google_ServiceResource.php(178): Google_REST::execute(Object(Google_HttpRequest)) #2 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/contrib/Google_AnalyticsService.php(383): Google_ServiceResource->__call('get', Array) #3 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/Google.php(46): Google_DataGaServiceResource->get('g in /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php on line 66

Because i'm using the exact same code as working examples, i think it might be a problem with the API or service account settings?

This is the user that i added to the analytics account : Analytics users

and this is api access accounts: api access

Does anybody know what i'm doing wrong?

Community
  • 1
  • 1
Kozmk12
  • 545
  • 1
  • 5
  • 15

11 Answers11

70

I was getting exactly the same error and this solved it for me:

I was using the account ID instead of the View ID. Switching to the View ID fixed it - it’s always the “View ID” and not the account or property ID (which looks like “UA-xxx”). The View ID is for a specific web property and can be found in the Admin->View (3rd column)->View Settings. It’s an integer with no dashes.

https://stackoverflow.com/a/15789266/1391229

Community
  • 1
  • 1
Joseph Race
  • 1,452
  • 14
  • 11
  • 9
    Easy mistake to make thank you @jarace87. You can find the id you need in the analytics web console under 'Admin' (top right) -> 'VIEW (PROFILE)' (far right column) -> 'View Settings' (first option) -> 'View ID'. That's the one you want. IT IS NOT THE 'UA-xxx' ONE! – Peter Johnson Dec 05 '13 at 15:58
  • This helped a great deal. The docs do mention it's a "view id" but don't tell you where to find that. Thanks! – Jared Eitnier Dec 10 '13 at 20:16
  • 2
    Also ran into this issue today, I think the name of this has changed with the new layout. Go to "Admin" then in the third column to the right (named "View") you will see link to "View Settings" click in here and you can use "View ID" – Ciaran Feb 28 '14 at 18:01
  • Why isnt this answer at the top.? – Usman Shaukat Feb 19 '15 at 16:05
  • I was using the Profile Id instead of the View Id because I had read around I needed to use the Profile Id. After switching to the View Id and using the private key file downloaded after creating a "service account key", for me this error disappeared. – nbro Aug 05 '16 at 14:00
  • omg, you don't know how much you helped me bro! thank you very much. – Harsha Oct 13 '16 at 10:43
10

Had the same issue today, the profileId is hard to find, you can have it in every Url in analytics let's say https://www.google.com/analytics/web/#management/Settings/aXXXXXXwYYYYYYYpZZZZZZZ/ it's the ZZZZZZZ part

jrenouard
  • 407
  • 6
  • 8
6

Same issue. Solved it by using the VIEW id instead of the account Id (UA-XXXXX-1).

Analytics Console > Admin -> View (Profile) -> View Settings -> View ID

AND

Make sure that you add the service account to your list of users in the Google Analytics Console. Simply setting it up in the Credentials, API and Permissions section of the developer console will still not grant it access to your analytics.

Analytics Console > Admin -> Account -> User Management -> "Add permissions for:"
Jason
  • 11,435
  • 24
  • 77
  • 131
4

Got the same error but after a while I recognized that i was using this script with my google chrome (my own email adress) but the Analytics API is running on the companies email. So the solution could be to edit the settings of your google chrome. Just disconnect from the gmail account in your google chrome (settings) and test it again.

Havrin
  • 83
  • 8
  • This worked for me to sign out of Chrome or use an incognito window or different browser. Really weird that it should matter though! – Erik Berger Feb 09 '14 at 00:52
4

Use View ID Not account ID, on Admin tab, 'View Settings' 'View ID'.

Pablo Martinez
  • 168
  • 1
  • 5
3

Helped me a lot after 2 weeks of trying to find solution:

Analytics Console -> Admin -> View (Profile) -> View Settings -> View ID

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
1

On a different application I was getting "Not sufficient permissions" and found my .htaccess file had some file restrictions that caused the problem.

Doug Hockinson
  • 1,007
  • 1
  • 9
  • 10
1

I had the same problem using .net library, after some tinkering, i found the solution:

Go to your google analytics management site & add the service account as a user & grant that account the necessary rights

Lee Gary
  • 2,357
  • 2
  • 22
  • 38
0

might be you are using other Client ID: Client secret: to get another profile id details

Ravi Kumar
  • 1,903
  • 1
  • 11
  • 17
  • open this url http://www.google.com/analytics/ Sign in click on "View Settings" you can see the the "View ID" – Ravi Kumar Sep 14 '13 at 10:54
0

Had the problem.. it turns out the Account ID is hidden in the URL of your analytics account!

https://developers.google.com/analytics/devguides/reporting/core/v3/#user_reports

The URL typically looks like so:

https://www.google.com/analytics/web/?hl=en#management/Settings/a51343283w84330433p87396224/%3Fm.page%3DAccountSettings/

The ID we are looking for follows the letter p.

In the example above, the ID that will work is: 87396224

hellojebus
  • 3,267
  • 1
  • 21
  • 23
0

Please create shared segment...It will work

http://www.periscopix.co.uk/blog/google-analytics-new-feature-shared-segments/

Vishal Bedre
  • 119
  • 1
  • 8