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
Bits in the news...
In a bid to make sense of the many random bookmarks I stick into deli... read more
Post Code Search Using Google API
Recently I've had a requirement to map locations based on radius from... read more
Upgrading alltradeart's subscription model
alltradeart went for a year without any change to the subscription m... read more
Making Life Easier
The best utilities are the ones you don't even realise are there, the... read more