3

I'm trying to get some info of my Google Analytics account using PHP. I already followed the steps for creating a Service Account in the Google Console API in this answer. I'm using the Google API Client for PHP.

This is the code I've got so far:

<?php
$path_to_src = 'src';

// These files are in /src, upload its contents to your web server
require_once $path_to_src . '/Google_Client.php';               
require_once $path_to_src . '/contrib/Google_AnalyticsService.php';

$path_to_keyfile = '***'; //my private key


// Initialise the Google Client object
$client = new Google_Client();

// Your 'Product name'
$client->setApplicationName('My App Name');

$client->setAssertionCredentials(
    new Google_AssertionCredentials(        
        '**', //gserviceaccount mail
        array('https://www.googleapis.com/auth/analytics.readonly'),
        file_get_contents($path_to_keyfile)
    )
);

// Get this from the Google Console, API Access page
$client->setClientId('***'); // my cliente ID
$client->setAccessType('offline_access');
$analytics = new Google_AnalyticsService($client);

// create service and get data
$service = new Google_AnalyticsService($client);

// We have finished setting up the connection,
// now get some data and output the number of visits this week.

// Your analytics profile id. (Admin -> Profile Settings -> Profile ID)
$analytics_id   = 'ga:****'; // my profile id
$lastWeek       = date('Y-m-d', strtotime('-1 week'));
$today          = date('Y-m-d');

try {
    $results = $analytics->data_ga->get($analytics_id, 
                        $lastWeek, 
                        $today,'ga:visits');
    echo '<b>Number of visits this week:</b> ';
    echo $results['totalsForAllResults']['ga:visits'];
} catch(Exception $e) {
    echo 'There was an error : - ' . $e->getMessage();
}

I've enabled the openssl extension in PHP:

enter image description here

When browsing to the location of the php script, I just get a almost forever loading and the following error:

enter image description here

I'm using PHP 5.4.7:

enter image description here

After debuging the Google API Client code, it looks like the script is breaking at this line:

if (!openssl_sign($data, $signature, $this->privateKey, "sha256"))

Anything below this line does not get called. Looks like the error happens in this line. Is there a incompatibility here, or something?

Community
  • 1
  • 1
Fuhrmann
  • 557
  • 13
  • 35
  • 1
    What line of your code does that last line from the API Client code you mentioned trace back to? – FoolishSeth Nov 25 '12 at 04:25
  • That's the line for authenticating me with my private key. I don't know exactly what code is, but I follow and managed to get there. The `openssl_sign` line is for some reason giving me this `Error 101`. – Fuhrmann Nov 26 '12 at 11:54
  • 1
    If you're sure that's the line producing the error, I'd suggest temporarily adding var_dumps for all the openssl_sign() parameters to see if any of them are set to something strange. You probably shouldn't post your var_dump'd private key on here though. Just note if it's null or something. – FoolishSeth Nov 27 '12 at 04:52
  • Also: is there a password on your key? – FoolishSeth Nov 27 '12 at 04:56
  • There is only the key, no pass. – Fuhrmann Nov 27 '12 at 11:51
  • i think there may be a problem with the way you're calling the last week start date. look at this tutorial. it may be useful. https://developers.google.com/analytics/solutions/articles/hello-analytics-api – Rachel Gallen Nov 26 '12 at 19:43
  • Hi, I'm having an error using the code you have above with: There was an error : - Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }' do you have an idea about this? Thanks – RicardO Aug 05 '13 at 13:47

1 Answers1

2

One thing for starters you should change:

You instantiate the AnalyticsService twice. Take out the one you're not using:

$service = new Google_AnalyticsService($client);

See if that helps your problem at all.

FoolishSeth
  • 3,953
  • 2
  • 19
  • 28