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: API, Google Analytics, PHP
Comments (12)
- Xiang Lee said...
-
thanks! i'll try.
- ChimeraLover said...
-
Very nice - easy to follow, simple, and working. Thanks for the knowledge!
- La Compagnia del Cavatappi said...
-
For me doesn't work. I get BadAuthentication error, I checked email and password and are right.
- 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. - 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.
- tinyk said...
-
Thanks, this was really helpful!
- 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 - 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.
- Lily Grace said...
-
Hi
I want to ask where to get this?
Zend/Loader.php
is this in the server? - Alex said...
-
Lily - Download it here:
http://framework.zend.com/ - inetsolution said...
-
Its highly informative. I would be visiting your blog hereafter regularly to gather valuable information.
professional os commerce development company - merlie said...
-
hi! i like the designs. check out the source of the template.
Thank you!
More templates easy to download
Subscribe to: Posts (Atom)
