PHP class for using the Google Analytics API
Posted April 23rd, 2009 in PHP (Updated April 28th, 2009)
Just quickly following up my post from earlier on today where I looked at how to log into the Google Analytics API using PHP and CURL using Username/Password Authentication I wrapped the login code up into a class with methods for logging in and then calling API functions. I'll look at parsing the XML result data at a later stage.
Update April 28th 2009: I've started a series about this. The full class, which I will continue to update over the next few days, can be downloaded here. The class presented below is an earlier version of it. Original post continues...
Here's the class:
class analytics_api {
protected $auth;
public function login($email, $password) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'accountType' => 'GOOGLE',
'Email' => $email,
'Passwd' => $password,
'service' => 'analytics',
'source' => ''
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$this->auth = '';
if($info['http_code'] == 200) {
preg_match('/Auth=(.*)/', $output, $matches);
if(isset($matches[1])) {
$this->auth = $matches[1];
}
}
return $this->auth != '';
}
public function call($url) {
$headers = array("Authorization: GoogleLogin auth=$this->auth");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if($info['http_code'] == 200) {
return $output;
}
else {
return false;
}
}
}
And here's an example using it, calling https://www.google.com/analytics/feeds/accounts/default which gets a list of all the accounts that are available to view data for.
$api = new analytics_api();
if($api->login('email@example.com', 'my-secret-password')) {
$xml = $api->call('https://www.google.com/analytics/feeds/accounts/default');
// ... process the xml ...
}
else {
// login failed
}
The class could easily be extended to parse the XML for common functions and have a function for getting the list of accounts into an array (or similar) and so on for other functions.
Update April 28th 2009: I've started a series about this and created a PHP class. The full class, which I will continue to update over the next few days, can be downloaded here.
Related posts:
- The Google Analytics API and PHP: A series (Tuesday, April 28th 2009)
- Converting Google Analytics API XML data into an array with PHP (Tuesday, April 28th 2009)
- Load the Google Analytics API accounts list with PHP (Monday, April 27th 2009)
- Get the ID for the Google Analytics API IDS parameter (Sunday, April 26th 2009)
- Log into the Google Analytics API using PHP and CURL using Username/Password Authentication (Thursday, April 23rd 2009)
Subscribe / Follow / Email / Bookmark / Share
Use the buttons below to subscribe to my RSS feed to be notified next time something is posted, share this post with others, or subscribe by email to have my posts sent in a daily email, follow me on Twitter or follow me on Facebook.
At least one new post is usually made every day. See my posting schedule for more details.
