This is the first part of a 2 part article on how to build a custom reporting tool with the Google Analytics Data Export API and CodeIgniter (plus a few additional open source libraries).
Overview
The purpose of our tool will be to connect to the Google Analytics Data Export API, return information for any given Analytics account, and convert that information into an auto generated PDF report. The reporting tool will be built on top of version 2.02 Reactor of the CodeIgniter PHP framework (http://codeigniter.com/). CodeIgniter requires PHP 5.1.6 or greater. Documentation for CodeIgniter itself is available from the CodeIgniter website and is also included with the application. In order to facilitate maintenance the standard CodeIgniter file structure will be adhered to. This means that the important components of the application will reside in the following locations:
- application/controllers/ (all controller files)
- application/views/ (all view files)
- application/helpers/ (custom helper files)
- application/libraries/ (external libraries used in the application)
In addition we will add the following structure to our application:
- application/db/ (sql file for application)
- themes/css/ (css files for application)
- themes/js/ (js files for application)
- themes/images/ (images used for app theme)
- images/ (content images not related to theming)
- reports/ (where the generated reports are saved)
These are the configuration and core files we will need to modify or create:
- application/core/MY_Controller.php
- application/config/ion_auth.php
- application/config/config.php
- application/config/constants.php
- application/config/autoload.php
External Libraries
The application will require the following external open source PHP libraries:
- Ion Auth (https://github.com/benedmunds/CodeIgniter-Ion-Auth)
- GAPI Google Analytics API PHP Interface (http://code.google.com/p/gapi-google-analytics-php-interface/)
- gChart PHP (http://code.google.com/p/gchartphp/)
- TCPDF (modified configuration – http://www.tcpdf.org/)
Additionally, on the front end jQuery and jQuery UI will be used.
General Application Flow
The application will follow the standard CodeIgniter MVC implementation. The user will first be presented by a login screen that handles authentication using the Ion Auth library. The user then signs in to a Google Account which connects to the Google Data Export API using the open source GAPI PHP library. We will name the main report generation controller select_report.php and place it in application/controllers/. We will use AJAX calls to create the various sections of the report and we will place those methods in application/views/ and name that file select_report_ajax.php. Most of our other controllers will serve the purpose connecting with the Google API and returning portions of data to these files.
PDF Report Generation
The generation of PDF reports will be handled by the TCPDF open source PHP library. Chart rendering will be handled by the Google Visualization API through the open source gChart PHP wrapper. Once the AJAX calls return each portion of the report selected by the user, we will pass this HTML to the ajax_print_pdf.php controller which processes the markup and converts it into a PDF report. These reports will be saved in the /reports directory and the user’s browser will then be redirected to the current report using javascript.
The code will follow in part 2 of this article.