Consuming Google Analytics using PHP

Published: 2007-09-06

A while back I posted an article mentioning the possibility of consuming Google Analytics using PHP. The process involved emailing an Analytics report in XML format to a mailbox then use PHP to select the message and parse the XML attachment.  I finally got round to digging up the code and have made it available to download.  I've stripped the code to it's bare basics allowing you to see the process involved.

It's a very simple, 2 method class. The constructer parses an array of config values and setting up the required param (apologies now, there is no validation ensuring all the required config values are present).

The required config values are:

  • 'site_url' - the site who's analytics info you'll be parsing.
  • 'mailbox' - the mailbox you'll be connecting to. 
  • 'mailbox_username' - username for accessing the mailbox.
  • 'mailbox_password' - password for accessing the mailbox.
  • 'gmail_address' - Gmail address of the sending Google account.

To run the code simply insatiate the object and execute the 'update_stats' method. In this example the results are stored inside the object in the results parameter.

$gs = new GoogleStats($config);
$gs->update_stats();
print_r($gs->results);

The XML is extracted to a SimpleXMLElement which allows us to loop through using XPath statements to extract the required data.

Click here to download the code.

More from this section

RECENT ARTICLES

Lightweight blog created on Ruby/GAA/Datastore stack

Put this together a month or two back but never got around to replaci... read more

Using Node JS and Couch DB Stack for Web Dev

With the recent hype surrounding Node.js I thought I'd better get my ... read more

Campaign Monitor API Using PHP and SOAP

Campaign Monitor has a fairly comprehensive API and support docs.&nbs... read more

Google Maps Snippet

Absolute bare basics when it comes the Google Map api but a snippet o... read more

CREDITS

powered by php-wax the php framework