Using PHP with Google Analytics API

Posted by Nethab at 5:35 PM

Since Google Analytics just released their API as a public beta, I decided to try and get it working with the PHP Zend GData library. I've seen several implementations using cURL, but the GData library seems ready-made for it, so I figured why not save some hassle.

Logging In

The first thing you have to do is login to the API service.

$email = "<email@domain.com>"; $pass = "<password>"; require_once("Zend/Loader.php"); Zend_Loader::loadClass('Zend_Gdata'); Zend_Loader::loadClass('Zend_Gdata_Query'); Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); $client = Zend_Gdata_ClientLogin::getHttpClient($email, $pass, "analytics"); $gdClient = new Zend_Gdata($client);

List of Accounts/Profiles

Now that we've logged in, we need to get a list of available Accounts/Profiles. Specifically we need to get the "profile ID" of the website we want to report on.

try { $results = $gdClient->getFeed("https://www.google.com/analytics/feeds/accounts/$email"); foreach ($results as $entry) { echo "Profile: " . $entry->title . " - Id: " . $entry->extensionElements[0] . "\n"; } } catch (Zend_Exception $e) { echo "Caught exception: " . get_class($e) . "\n"; echo "Message: " . $e->getMessage() . "\n"; }

Fetching the Data

Now that we've found our profile ID, we should decide which data we want to collect, and request it from Google's servers. I find it easiest to return it as CSV.

try { $dimensions = array("ga:region", "ga:city", "ga:latitude", "ga:longitude"); $metrics = array("ga:visits", "ga:pageviews"); $reportURL = "https://www.google.com/analytics/feeds/data?ids=<profile ID>&" . "dimensions=" . @implode(",", $dimensions) . "&" . "metrics=" . @implode(",", $metrics) . "&" . "start-date=<YYYY-MM-DD>&" . "end-date=<YYYY-MM-DD>"; $results = $gdClient->getFeed($reportURL); $titleRow = 1; // To output a row of column labels foreach ($results as $rep) { if ($titleRow) { foreach ($rep->extensionElements as $elem) { $titles[] = $elem->extensionAttributes["name"]["value"]; } echo implode(",", $titles) . "\n"; $titleRow = 0; } foreach ($rep->extensionElements as $elem) { $row[] = $elem->extensionAttributes["value"]["value"]; } echo implode(",", $row) . "\n"; } } catch (Zend_Exception $e) { echo "Caught exception: " . get_class($e) . "\n"; echo "Message: " . $e->getMessage() . "\n"; }


Labels: , ,


Comments (12)

Xiang Lee said...  

thanks! i'll try.

April 24, 2009 7:13 PM

ChimeraLover said...  

Very nice - easy to follow, simple, and working. Thanks for the knowledge!

April 25, 2009 5:39 PM

La Compagnia del Cavatappi said...  

For me doesn't work. I get BadAuthentication error, I checked email and password and are right.

April 28, 2009 8:15 AM

madhavi said...  

hi thanks for the post. i tried it but its not working.. got an blank page as output. r u storing the the data in csv file?? or echoing the whole data to browser.

can u plz help me how can i view the data.

Thanks.

May 1, 2009 5:47 AM

Nethab said...  

I see the data echoed out directly to the browser. But its just as easy to take the output and pipe it to something else like the Google Visualization API to make graphs or Google Maps API to show a list of visitors on a map.

May 1, 2009 3:09 PM

tinyk said...  

Thanks, this was really helpful!

May 7, 2009 12:38 PM

mandarina said...  

Hi! i'd like to know ho to use the authentication code that i receive with this php, in a javascript code for analytics that i have.
I don't know where to put the authentication token.
Thanks

May 8, 2009 1:33 AM

Zuhair said...  

BadAuthentication error occurs when using a non-google email for analytics, try signing up for gmail and use that to get around this issue.

May 20, 2009 6:12 PM

Lily Grace said...  

Hi

I want to ask where to get this?

Zend/Loader.php

is this in the server?

June 17, 2009 1:22 AM

Alex said...  

Lily - Download it here:
http://framework.zend.com/

July 21, 2009 7:30 AM

inetsolution said...  

Its highly informative. I would be visiting your blog hereafter regularly to gather valuable information.


professional os commerce development company

September 13, 2009 11:29 PM

merlie said...  

hi! i like the designs. check out the source of the template.
Thank you!
More templates easy to download

September 15, 2009 10:51 PM

Post a Comment

Subscribe to: Posts (Atom)