Consuming Google Analytics using PHP
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.
RECENT ARTICLES
Import Error: No module named django.core.management
Installing and testing the Django Framework on my new mac I came acro... read more
Quick Note on Setting Up a Mail Server
Part of the basics when setting up a mail server but I needed to put ... read more
Microformats Creator Tool
"Microformats are a way of adding simple markup to human-readable... read more
Recursive Directory Listing
Nothing too fancy but a nice way of viewing a directory tree on your ... read more