Reputation: 6574
<?php
require('gapi.class.php');
$ga = new gapi(_GAE_,_GAP_);
$dimensions = array('source');
$metrics = array('visits');
$ga->requestReportData(_GAA_, $dimensions, $metrics,'-visits');
$gaResults = $ga->getResults();
$i=1;
foreach($gaResults as $result)
{
printf("%-4d %-40s %5d\n",
$i++,
$result->getSource(),
$result->getVisits());
}
echo "\n-----------------------------------------\n";
echo "Total Results : {$ga->getTotalResults()}";
?>
This code is from a GAPI tutorial, and I keep getting these errors or something on failed login, yet all my information is correct.
Fatal error: Uncaught exception 'Exception' with message 'GAPI: Failed to request report data. Error: "GDatainsufficientPermissionsUser does not have sufficient permissions for this profile."' in public_html/admin/gapi.class.php:218 Stack trace: #0
public_html/php/templates/pages/ga_advanced.php(11): gapi->requestReportData('52537078', Array, Array, '-visits') #1
public_html/php/php_includes/easyCMSv2.php(43): include('/home/a3822536/...') #2
public_html/php/templates/pages/general_google_analytics.php(6): CMS->template('../php/template...') #3
public_html/admin/gapi.class.php on line 218
Does anyone have any experience with this that could possibly explain what is wrong? I used the Account ID with out the UA
and -(n)
as well as it with it. EX: UA-1238124hf-y
but nothing is working.
Upvotes: 2
Views: 2880
Reputation: 5371
What worked in my case was I needed to use the php code in
And go to the security settings of your google account and enable less secure
Make sure you re-enable it back when you are done.
Upvotes: 0
Reputation: 477
you are using the ACCOUNT id
instead of the PROFILE id
in your request.
the Profile id can be found when you login to GA click on the link to look at the stats of a specific profile (website) and in the URL you will see something like this:
https://www.google.com/analytics/web/?#report/visitors-overview/axxxxxwxxxxxxxpXXXXXX/
the numbers after the "p" at the end of the URL is the Profile id
Upvotes: 2
Reputation: 6574
So after doing some digging I found the fix for this, you actually have to edit the GAPI.class php code. You can read more here
Change line 32
to
const account_data_url = 'https://www.googleapis.com/analytics/v2.4/management/accounts/~all/webproperties/~all/profiles';
Change lines 267
and 268
to
$account_root_parameters['generator'] = strval($xml->generator);
$account_root_parameters['generatorVersion'] = strval($xml->generator->attributes());
Change line 270
to
$open_search_results = $xml->children('http://a9.com/-/spec/opensearch/1.1/');
Change lines 322
and 323
to
$report_root_parameters['generator'] = strval($xml->generator);
$report_root_parameters['generatorVersion'] = strval($xml->generator->attributes());
Change line 325
to
$open_search_results = $xml->children('http://a9.com/-/spec/opensearch/1.1/');
Hope that helps any other users looking and not finding a clear cut documentation.
Upvotes: 0