Issues with Hello Analytics API tutorial, helloAnalyticsApi.php . Out of date?

621 views
Skip to first unread message

Daniel

unread,
Oct 21, 2012, 2:40:22 PM10/21/12
to google-api...@googlegroups.com
I am trying out the HelloAnalyticsApi.php as part of the Hello Analytics API tutorial. I followed all the steps here (PHP version):


There are several errors that I'm encountering, including:
$analytics = new apiAnalyticsService($client);
runMainDemo
($analytics);
apiAnalyticsService is not recognized, nor is runMainDemo...


In addition, when I run the file helloAnalyticsApi.php from the google-api-php-client (v 0.6.0), as is, there are no errors, but the resulting page is completely blank

Has anyone else noticed issues with the Tutorial as well as the helloAnalyticsApi.php file? My guess is that possibly both are out of date but maybe I'm missing something?

Thanks!
-DC

Daniel

unread,
Oct 21, 2012, 3:30:09 PM10/21/12
to google-api...@googlegroups.com
Here is the Code Summary from the Hello Analytics API tutorial found here: https://developers.google.com/analytics/resources/tutorials/hello-analytics-api

You can see that the require_once files both do not exist in the latest client library:

<?php
require_once
'google-api-php-client/src/apiClient.php';
require_once
'google-api-php-client/src/contrib/apiAnalyticsService.php';

session_start
();

$client
= new apiClient();
$client
->setApplicationName('Hello Analytics API Sample');

// Visit //code.google.com/apis/console?api=analytics to generate your
// client id, client secret, and to register your redirect uri.
$client
->setClientId('insert_your_oauth2_client_id');
$client
->setClientSecret('insert_your_oauth2_client_secret');
$client
->setRedirectUri('insert_your_oauth2_redirect_uri');
$client
->setDeveloperKey('insert_your_developer_key');
$client
->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

// Magic. Returns objects from the Analytics Service instead of associative arrays.
$client
->setUseObjects(true);

if (isset($_GET['code'])) {
  $client
->authenticate();
  $_SESSION
['token'] = $client->getAccessToken();
  $redirect
= 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header
('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
  $client
->setAccessToken($_SESSION['token']);
}

if (!$client->getAccessToken()) {
  $authUrl
= $client->createAuthUrl();
 
print "<a class='login' href='$authUrl'>Connect Me!</a>";

} else {

  $analytics
= new apiAnalyticsService($client);
  runMainDemo
($analytics);
}

// function runMainDemo continued in next section.

?>

Terry Seidler

unread,
Oct 22, 2012, 12:06:50 PM10/22/12
to google-api...@googlegroups.com
Are you using the latest version of the code/ (off svn for example?)

The include files have been renamed to: Google_Client.php and contrib/Google_AnalyticsService.php
$client = new apiClient(); should be: $client = new Google_Client();   (in the latest version of the code).

I've got a little bit of code on Analytics + Service Accounts on my blog (contains only 3 articles and the other two are not very interesting): http://blog.salteh.net/2012/10/google-analytics-api-and-service-accounts/

Good luck!

T

Terry Seidler

unread,
Oct 22, 2012, 12:13:24 PM10/22/12
to google-api...@googlegroups.com
(You're not using service accounts, but you should be able to rename the files and classes using my code. Alternatively, open up the include files and try to figure out what the correct names should be in your case.)

Daniel

unread,
Oct 25, 2012, 12:09:35 AM10/25/12
to google-api...@googlegroups.com
Hi Terry,
  I downloaded the latest release version (0.6) but my issue here is that the tutorial that's supposed to teach how to use the API seems to be using the old version of the code (0.5). I'm hoping someone from Google sees this and makes a note to update the tutorial to be more accurate with the latest release version. It will save others a ton of time updating/renaming files to match up correctly. Is there any way we can update it directly (Wiki-style) or does it have to go through the Google team?

Terry Seidler

unread,
Oct 25, 2012, 2:08:35 AM10/25/12
to google-api...@googlegroups.com
I see! I think they know, someone replied to this thread: https://groups.google.com/forum/#!topic/google-api-php-client/s2vvXm-IzvM

Either way, good luck ;-)

Daniel

unread,
Oct 25, 2012, 1:29:08 PM10/25/12
to google-api...@googlegroups.com
Thanks Terry - I hope they do. I should have written in this forum around 6 months ago when I first noticed the issue :/

WhoSoLovesUs

unread,
Aug 21, 2013, 2:19:16 PM8/21/13
to google-api...@googlegroups.com, dan...@youngfolksconsulting.com
Hi Daniel, did you get this working? I updated the first lines of the HelloAnalyticsApi.php file to:

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';

But I'm still getting a blank page when loading the file

WhoSoLovesUs

unread,
Aug 21, 2013, 2:27:12 PM8/21/13
to google-api...@googlegroups.com, dan...@youngfolksconsulting.com
Whoops I didn't read Terry's post

Got the "Connect Me!" link to appear, but after authorizing I am sent to a 404 error page; URL looks like this:

http://www.mydomainname.com/oauth2callback/?code=4/jkcSfUguWPBEJBKphbkKV0uk4kWA.ojNLWmkAcAAQEnp6UAPFm0EGHT1EgQI

My Redirect URI matches line 14: $client->setRedirectUri('http://www.mydomainname.com/oauth2callback');
Reply all
Reply to author
Forward
0 new messages