Call to undefined method Google_Client in Google Analytics while using HelloAnalyticsApi
The problem you are having is that you have the wrong client lib. The Hello Analytics API tutorial was created using the old lib on Code.google - google-api-php-client Not the newer version on github.
Update: Because of the fact that the tutorial still hasn't been updated I have made a tutorial that may help. Google Oauth2 php. The code below is ripped directly from it. The tutorial will be kept up to date you may want to check that for any changes.
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("{devkey}");
$client->setClientId('{clientid}.apps.googleusercontent.com');
$client->setClientSecret('{clientsecret}');
$client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
//For loging out.
if ($_GET['logout'] == "1") {
unset($_SESSION['token']);
}
// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
// Step 1: The user has not authenticated we give them a link to login
if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
$client->setAccessToken($_SESSION['token']);
$service = new Google_Service_Analytics($client);
// request user accounts
$accounts = $service->management_accountSummaries->listManagementAccountSummaries();
foreach ($accounts->getItems() as $item) {
echo "Account: ",$item['name'], " " , $item['id'], "<br /> \n";
foreach($item->getWebProperties() as $wp) {
echo ' WebProperty: ' ,$wp['name'], " " , $wp['id'], "<br /> \n";
$views = $wp->getProfiles();
if (!is_null($views)) {
foreach($wp->getProfiles() as $view) {
// echo ' View: ' ,$view['name'], " " , $view['id'], "<br /> \n";
}
}
}
} // closes account summaries
}
print "<br><br><br>";
print "Access from google: " . $_SESSION['token'];
?>
deepak bhardwaj
Updated on June 04, 2022Comments
-
deepak bhardwaj almost 2 years
I am using Core reporting API for reporting. I have installed Google PHP API client master on my localhost server and made a file HelloAnalyticsAPi.php in src folder Where I include
Google/Client.php , Google/Service/Analytics.php
files. And use the below details
$client->setClientId('XXXXXXXXXXX.apps.googleusercontent.com'); $client->setClientSecret('XXXXXXXXXXX'); $client->setRedirectUri('http://localhost/analytics/src/HelloAnalyticsApi.php'); $client->setDeveloperKey('XXXXXXXXXXX'); $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly')); $client->setUseObjects(true);
I have fatal error on setUseObjects. Error is
Fatal error: Call to undefined method Google_Client::setUseObjects()
. I have done some authorization on my google analytics backend also.Please let me know the whole process for getting report on my server. Because I am not able to understand the developers guide of google analytics which they have given.
-
deepak bhardwaj about 10 yearsSo which lib should I use?
-
DaImTo about 10 yearsI would use the new one. There are some samples with it. Side Note:The GA devs of the problem with that tutorial. They are working on new ones. But I cant tell you when they will be live.
-
jerrygarciuh over 9 years7 months later and the old tutorial is still there and the code still generates this error on fresh install from GitHub. Lame
-
DaImTo over 9 years@jerrygarciuh yes i know, I have a tutorial that should help daimto.com/google-oauth2-php
-
jerrygarciuh over 9 yearsThanks @DaImTo I managed to get it working by reading several SO posts. I'll take a look at the link too. Appreciate it!
-
dsignr over 9 yearsThanks so much. As of December 2, 2014 I can confirm this is working!
-
Taylan over 9 yearsSo, what is the equivalent of
setUseObjects
function?