Parsing Google Analytics data with PHP: A Series
Posted March 13th, 2009 in PHP (Updated May 30th, 2009)
At the start of 2009 I added a "Recent Posts" section to the sidebar navigation of this blog and decided I'd update it using data from Google Analytics. Unfortunately Analytics doesn't have an API to get data but does have a function for sending data by email on regular basis either as a TSV, CSV, XML or PDF attachment.
Update May 2009: There is now a Google Analytics API and it's much easier to use than the methods presented here. The posts in this series are still useful however as many of them show how to connect to mail servers, extract attachments from emails etc.
Read my Google Analytics API and PHP series for more details about the API and to download the PHP class I have created for accessing it.
End of update
Therefore a somewhat spordaic series of posts began appearing on this blog showing how to get Google Analytics data by email and use PHP's IMAP functions to read through a mailbox and extract data from the attachments.
A full list of articles from this series is presented below. Included were a number of supplementary posts about how to do various additional things with PHP IMAP.
The series
To kick things off I looked at how to get Google Analytics to email reports manually (i.e. one-off) and automatically (i.e. on a daily etc basis without user intervention):
I've been using the PHP IMAP functions during this series so needed to look at how to connect to an IMAP server, download emails and extract the attachments. These were covered in the following posts:
- Using PHP IMAP functions to download email
- Open a mailbox other than the INBOX with PHP IMAP
- PHP IMAP: Looping through messages to find a specific subject
- Extracting attachments from an email message using PHP IMAP functions
- Function to extract email attachments using PHP IMAP
- Downloading Google Analytics data attachments with PHP IMAP
- Deleting messages with PHP IMAP
- Deleting messages with PHP IMAP and Gmail
- Find unread messages using PHP IMAP
Finally the series looked at how to then parse the attachments into a data array which could then be used to populate a database etc:
- PHP's str_getcsv function
- Parsing Google Analytics data with PHP - CSV files
- Parsing Google Analytics data with PHP - TSV files
- Parsing Google Analytics data with PHP - XML files
I'll personally be downloading email from a Gmail account, so posted about what you need to do to be able to download mail from Gmail with IMAP and then downloading messages with PHP:
And in case of the "Fatal error: Call to undefined function imap_open()" error I posted how to install the PHP IMAP extension on CentOS/RHEL/Fedora and Debian:
Share or Bookmark
Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.
Subscribe or Follow
Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.

