google analytics have an api !
juust | 11/05/2009[note: over at ioncannon Carson McDonald made a cool google analytics plugin for wordpress, i use it on this blog, works fine].
An actual google analytics api, and I missed out on it. This api is already a month old and i havent read anything on the blogs about it.
I found it half an hour ago, I havent checked it completely but it looks promising. Here is the first bit, basic authentication with php and curl.
-
$USER_EMAIL=""; // #Insert your Google Account email here
-
$USER_PASS=""; //#Insert your password here
-
-
//array with some general data
-
$data = array(
-
"Email" => $USER_EMAIL,
-
"Passwd" => $USER_PASS,
-
"accountType" => "GOOGLE",
-
"source" => "curl-accountFeed-v1",
-
"service" => "analytics"
-
);
-
-
$friends_url = 'https://www.google.com/accounts/ClientLogin';
-
$curl = curl_init();
-
curl_setopt($curl, CURLOPT_URL, $friends_url);
-
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
-
-
//http-post that contains the array as data
-
curl_setopt($curl, CURLOPT_POST, true);
-
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
-
-
//go shove the https secure connection verification
-
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
-
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
-
-
curl_setopt($curl, CURLOPT_VERBOSE, 1);
-
-
-
$googleAuth = curl_exec($curl);
-
-
//optional : some feedback
-
-
//check if we get an error code from cUrl
-
// echo curl_errno($curl)."<br />";
-
// echo curl_error($curl)."<br />" ;
-
-
-
//print the body of the returned data
-
// print_r($googleAuth);
-
-
//print all the headers
-
// $info = curl_getinfo($curl);
-
// print_r($info);
somewhere in the garbled mess that curl returns is the Authorization token, starts with auth=.
-
$start = strpos($googleAuth, "Auth=") + 5;
-
$Authtoken = substr($googleAuth, $start);
-
-
//echo $Authtoken;
I put that token in the header of the next calls and google assumes I am kosher : time to get the accounts feed :
-
//add the authoritzation token as extra header
-
$headers[] = "Authorization: GoogleLogin auth=".$Authtoken;
-
-
-
$friends_url = 'https://www.google.com/analytics/feeds/accounts/default';
-
-
$curl = curl_init();
-
curl_setopt($curl, CURLOPT_URL, $friends_url);
-
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
-
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
-
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
-
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
-
curl_setopt($curl, CURLOPT_VERBOSE, 1);
-
$googleAccounts = curl_exec($curl);
-
-
//check errors
-
echo curl_errno($curl);
-
echo curl_error($curl) ;
-
print_r($googleAccounts);
And there it is : a whole list with weird codes, my account list :) seems easier than the other gData api’s.
note : the google code curl example does not show the ” auth=” part of the token, they assume you use the entire line “auth=…” as token.
Once I have my spectacular visitor count in a sidebar widget I’ll blog another post on this one.








