google analytics have an api !

[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.

  1. $USER_EMAIL=""; // #Insert your Google Account email here
  2. $USER_PASS=""; //#Insert your password here
  3.  
  4. //array with some general data
  5. $data = array(
  6.   "Email" => $USER_EMAIL,
  7.   "Passwd" => $USER_PASS,
  8.   "accountType" => "GOOGLE",
  9.   "source" => "curl-accountFeed-v1",
  10.   "service" => "analytics"
  11. );
  12.  
  13. $friends_url = 'https://www.google.com/accounts/ClientLogin';
  14. $curl = curl_init();
  15. curl_setopt($curl, CURLOPT_URL, $friends_url);
  16. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
  17. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  18.  
  19. //http-post that contains the array as data
  20. curl_setopt($curl, CURLOPT_POST, true);
  21. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  22.  
  23. //go shove the https secure connection verification
  24. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  25. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  26.  
  27. curl_setopt($curl, CURLOPT_VERBOSE, 1);
  28.    
  29.  
  30. $googleAuth = curl_exec($curl);
  31.  
  32. //optional : some feedback
  33.  
  34. //check if we get an error code from cUrl
  35. //    echo curl_errno($curl)."<br />";
  36. //    echo  curl_error($curl)."<br />" ;
  37.  
  38.  
  39. //print the body of the returned data
  40. //    print_r($googleAuth);
  41.  
  42. //print all the headers
  43. //    $info = curl_getinfo($curl);
  44. //    print_r($info);

somewhere in the garbled mess that curl returns is the Authorization token, starts with auth=.

  1. $start = strpos($googleAuth, "Auth=") + 5;
  2. $Authtoken = substr($googleAuth, $start);
  3.  
  4. //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 :

  1. //add the authoritzation token as extra header
  2. $headers[] = "Authorization: GoogleLogin auth=".$Authtoken;
  3.  
  4.  
  5. $friends_url = 'https://www.google.com/analytics/feeds/accounts/default';
  6.  
  7.  $curl = curl_init();
  8.  curl_setopt($curl, CURLOPT_URL, $friends_url);
  9.  curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
  10.  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  11.  curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  12.  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  13.  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  14.  curl_setopt($curl, CURLOPT_VERBOSE, 1);
  15.  $googleAccounts = curl_exec($curl);
  16.  
  17. //check errors
  18. echo curl_errno($curl);
  19. echo curl_error($curl) ;
  20. 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.