Google Analytics API for Codeigniter

Posted on: January 20, 2011 Posted by: JJ Comments: 0

Google Analytics API for Codeigniter

So… I’ve been trying get Google Analytics traffic data. Quick and dirty way was to alter the gapi.class.php provided by Google (http://code.google.com/p/gapi-google-analytics-php-interface/).

Here’s the alteration (starting at around Line 44):

[php]
/**
* Constructor function for all new gapi instances
*
* Set up authenticate with Google and get auth_token
*
* @param Array $auth (email, password, token)
* @param String $token
* @return gapi
*/
public function __construct($auth)
{
if(isset($auth[‘token’]) && $auth[‘token’] !== null)
{
$this->auth_token = $token;
}
else
{
$this->authenticateUser($auth[’email’],$auth[‘password’]);
}
}
[/php]

To use it within your controller, paste below code to your method.

[php]
$report_id = YOURREPORTID;
$start_date = ‘YYYY-MM-DD’;
$end_date = ‘YYYY-MM-DD’;

$this->load->library(‘gapi’, array(’email’=>’youremail@yourdomain.com’, ‘password’=>’*******’));
$this->gapi->requestReportData($report_id, array(‘date’), array(‘pageviews’, ‘visits’, ‘newVisits’), ‘date’, ”, $start_date, $end_date, 1, 366);
[/php]