Sizetrac

Google Analytics API

Unfortunately Google Analytics does not have an API yet... However I will show you a quick and dirty way to get the data you need. The idea is basically to setup an automated report from within Google Analytics, which will email a CSV file attachment, that you can download and parse with a script. For this example I will use the Zend Mail class because I love Zend Mail and it is free.

So the first step is to schedule the report. For this simple example we will use the default Visitors Report. So login to your Google Analytics account, click Visitors on the main left nav, then click the Email icon up at the top of the report, see screenshot below:

GA Screenshot

Google Analytics Screenshot

Now let's get to the code. First step is to connect to your POP mail server and get the new messages. Don't forget to download the latest version of Zend at http://framework.zend.com.

include ('Zend/Mail/Storage/Pop3.php');

$params = array('host' => "mail.domain.com", 'user' => "info@domain.com", 'password' => "secret");

$mail = new Zend_Mail_Storage_Pop3($params);

$num = $mail->countMessages();

In a production environment, you would want some logic to verify that the number of messages is greater than 0, loop through the messages, etc but for this tutorial I have omitted these items for brevity. So next we need to download the first message and get its parts.

$message = $mail->getMessage(1);
       
$parts = $message->countParts();

Next we spin through the parts, looking for the attachment.

for($b=1; $b<=$parts; $b++){
           
    $part = $message->getPart($b);
           
    $headers = $part->getHeaders();
           
    if (!empty($headers['Content-Disposition']) || !empty($headers['content-disposition'])){

Then we parse the CSV file to an array for simple looping:

$csv = base64_decode($part->getContent());
               
$lines = explode("\n",$csv);

$data = array();
                       
foreach ($lines as $line){
                   
    $data[] = explode(',',$line);               
                   
}

The previous code checks the email account, downloads the CSV attachment and parses it to an array. That is all going to stay the same but the matching below will change depending on your needs. For this simplistic example, I am going to get the number of visits from yesterday.

$yesterday = date('F j',mktime(0, 0, 0, date('m'), date('d')-1, date('Y')));
               
foreach ($data as $row){
                   
    if ($row[0] == '"'.$yesterday){
                       
        $visits = $row[2];
                       
        echo "VISITS: {$visits}";
                       
    }
                           
}

So that's basically it. Of course in production you would probably want to write the Google Analytics data to a database or whatever, but you get the point! Hopefully Google Analytics will get an API soon, but until then, this works like a champ!

  • Digg
  • TwitThis
  • del.icio.us
  • Netvouz
  • description
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank
  • Technorati
  • Facebook
  • Google
  • LinkedIn
  • Live
  • MySpace
  • Ping.fm
  • Yahoo! Buzz
  • E-mail this story to a friend!



Home | Misc | Google Analytics API